Skip to content

Commit e6bdfa2

Browse files
authored
Merge pull request #9206 from LawnGnome/unify-cdn-invalidation
Unify CDN invalidation into a single method on `Environment`
2 parents 058a406 + c812242 commit e6bdfa2

File tree

5 files changed

+24
-56
lines changed

5 files changed

+24
-56
lines changed

src/worker/environment.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::team_repo::TeamRepo;
55
use crate::typosquat;
66
use crate::util::diesel::Conn;
77
use crate::Emails;
8+
use anyhow::Context;
89
use crates_io_index::{Repository, RepositoryConfig};
910
use derive_builder::Builder;
1011
use diesel_async::pooled_connection::deadpool::Pool;
@@ -71,6 +72,19 @@ impl Environment {
7172
self.fastly.as_ref()
7273
}
7374

75+
/// Invalidate a file in all registered CDNs.
76+
pub(crate) async fn invalidate_cdns(&self, path: &str) -> anyhow::Result<()> {
77+
if let Some(cloudfront) = self.cloudfront() {
78+
cloudfront.invalidate(path).await.context("CloudFront")?;
79+
}
80+
81+
if let Some(fastly) = self.fastly() {
82+
fastly.invalidate(path).await.context("Fastly")?;
83+
}
84+
85+
Ok(())
86+
}
87+
7488
/// Returns the typosquatting cache, initialising it if required.
7589
pub(crate) fn typosquat_cache(
7690
&self,

src/worker/jobs/dump_db.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@ impl BackgroundJob for DumpDb {
4848
info!("Database dump tarball uploaded");
4949

5050
info!("Invalidating CDN caches…");
51-
if let Some(cloudfront) = env.cloudfront() {
52-
if let Err(error) = cloudfront.invalidate(TAR_PATH).await {
53-
warn!("Failed to invalidate CloudFront cache: {}", error);
54-
}
55-
}
56-
57-
if let Some(fastly) = env.fastly() {
58-
if let Err(error) = fastly.invalidate(TAR_PATH).await {
59-
warn!("Failed to invalidate Fastly cache: {}", error);
60-
}
51+
if let Err(error) = env.invalidate_cdns(TAR_PATH).await {
52+
warn!("Failed to invalidate CDN caches: {error}");
6153
}
6254

6355
info!("Uploading zip file…");
@@ -67,16 +59,8 @@ impl BackgroundJob for DumpDb {
6759
info!("Database dump zip file uploaded");
6860

6961
info!("Invalidating CDN caches…");
70-
if let Some(cloudfront) = env.cloudfront() {
71-
if let Err(error) = cloudfront.invalidate(ZIP_PATH).await {
72-
warn!("Failed to invalidate CloudFront cache: {}", error);
73-
}
74-
}
75-
76-
if let Some(fastly) = env.fastly() {
77-
if let Err(error) = fastly.invalidate(ZIP_PATH).await {
78-
warn!("Failed to invalidate Fastly cache: {}", error);
79-
}
62+
if let Err(error) = env.invalidate_cdns(ZIP_PATH).await {
63+
warn!("Failed to invalidate CDN caches: {error}");
8064
}
8165

8266
Ok(())

src/worker/jobs/rss/sync_crate_feed.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,8 @@ impl BackgroundJob for SyncCrateFeed {
8686
ctx.storage.upload_feed(&feed_id, &channel).await?;
8787

8888
let path = object_store::path::Path::from(&feed_id);
89-
if let Some(cloudfront) = ctx.cloudfront() {
90-
info!(%path, "Invalidating CloudFront cache…");
91-
cloudfront.invalidate(path.as_ref()).await?;
92-
} else {
93-
info!("Skipping CloudFront cache invalidation (CloudFront not configured)");
94-
}
95-
96-
if let Some(fastly) = ctx.fastly() {
97-
info!(%path, "Invalidating Fastly cache…");
98-
fastly.invalidate(path.as_ref()).await?;
99-
} else {
100-
info!("Skipping Fastly cache invalidation (Fastly not configured)");
89+
if let Err(error) = ctx.invalidate_cdns(path.as_ref()).await {
90+
warn!("Failed to invalidate CDN caches: {error}");
10191
}
10292

10393
info!("Finished syncing updates feed");

src/worker/jobs/rss/sync_crates_feed.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,8 @@ impl BackgroundJob for SyncCratesFeed {
7070
ctx.storage.upload_feed(&feed_id, &channel).await?;
7171

7272
let path = object_store::path::Path::from(&feed_id);
73-
if let Some(cloudfront) = ctx.cloudfront() {
74-
info!(%path, "Invalidating CloudFront cache…");
75-
cloudfront.invalidate(path.as_ref()).await?;
76-
} else {
77-
info!("Skipping CloudFront cache invalidation (CloudFront not configured)");
78-
}
79-
80-
if let Some(fastly) = ctx.fastly() {
81-
info!(%path, "Invalidating Fastly cache…");
82-
fastly.invalidate(path.as_ref()).await?;
83-
} else {
84-
info!("Skipping Fastly cache invalidation (Fastly not configured)");
73+
if let Err(error) = ctx.invalidate_cdns(path.as_ref()).await {
74+
warn!("Failed to invalidate CDN caches: {error}");
8575
}
8676

8777
info!("Finished syncing crates feed");

src/worker/jobs/rss/sync_updates_feed.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,8 @@ impl BackgroundJob for SyncUpdatesFeed {
7070
ctx.storage.upload_feed(&feed_id, &channel).await?;
7171

7272
let path = object_store::path::Path::from(&feed_id);
73-
if let Some(cloudfront) = ctx.cloudfront() {
74-
info!(%path, "Invalidating CloudFront cache…");
75-
cloudfront.invalidate(path.as_ref()).await?;
76-
} else {
77-
info!("Skipping CloudFront cache invalidation (CloudFront not configured)");
78-
}
79-
80-
if let Some(fastly) = ctx.fastly() {
81-
info!(%path, "Invalidating Fastly cache…");
82-
fastly.invalidate(path.as_ref()).await?;
83-
} else {
84-
info!("Skipping Fastly cache invalidation (Fastly not configured)");
73+
if let Err(error) = ctx.invalidate_cdns(path.as_ref()).await {
74+
warn!("Failed to invalidate CDN caches: {error}");
8575
}
8676

8777
info!("Finished syncing updates feed");

0 commit comments

Comments
 (0)