Skip to content

Commit a298a69

Browse files
committed
worker: add a job to invalidate paths on our CDNs
1 parent dd93623 commit a298a69

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/worker/jobs/invalidate_cdns.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::sync::Arc;
2+
3+
use anyhow::Context;
4+
use crates_io_worker::BackgroundJob;
5+
6+
use crate::worker::Environment;
7+
8+
/// A background job that invalidates the given paths on all CDNs in use on crates.io.
9+
#[derive(Deserialize, Serialize)]
10+
pub struct InvalidateCdns {
11+
paths: Vec<String>,
12+
}
13+
14+
impl InvalidateCdns {
15+
pub fn new<I>(paths: I) -> Self
16+
where
17+
I: Iterator,
18+
I::Item: ToString,
19+
{
20+
Self {
21+
paths: paths.map(|path| path.to_string()).collect(),
22+
}
23+
}
24+
}
25+
26+
impl BackgroundJob for InvalidateCdns {
27+
const JOB_NAME: &'static str = "invalidate_cdns";
28+
29+
type Context = Arc<Environment>;
30+
31+
async fn run(&self, ctx: Self::Context) -> anyhow::Result<()> {
32+
// Fastly doesn't provide an API to purge multiple paths at once, except through the use of
33+
// surrogate keys. We can't use surrogate keys right now because they require a
34+
// Fastly-specific header, and not all of our traffic goes through Fastly.
35+
//
36+
// For now, we won't parallelise: most crate deletions are for new crates with one (or very
37+
// few) versions, so the actual number of paths being invalidated is likely to be small, and
38+
// this is all happening from either a background job or admin command anyway.
39+
if let Some(fastly) = ctx.fastly() {
40+
for path in self.paths.iter() {
41+
fastly
42+
.invalidate(path)
43+
.await
44+
.with_context(|| format!("Failed to invalidate path on Fastly CDN: {path}"))?;
45+
}
46+
}
47+
48+
if let Some(cloudfront) = ctx.cloudfront() {
49+
cloudfront
50+
.invalidate_many(self.paths.clone())
51+
.await
52+
.context("Failed to invalidate paths on CloudFront CDN")?;
53+
}
54+
55+
Ok(())
56+
}
57+
}

src/worker/jobs/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod dump_db;
66
mod expiry_notification;
77
mod index;
88
mod index_version_downloads_archive;
9+
mod invalidate_cdns;
910
mod readmes;
1011
pub mod rss;
1112
mod send_publish_notifications;
@@ -23,6 +24,7 @@ pub use self::dump_db::DumpDb;
2324
pub use self::expiry_notification::SendTokenExpiryNotifications;
2425
pub use self::index::{NormalizeIndex, SquashIndex, SyncToGitIndex, SyncToSparseIndex};
2526
pub use self::index_version_downloads_archive::IndexVersionDownloadsArchive;
27+
pub use self::invalidate_cdns::InvalidateCdns;
2628
pub use self::readmes::RenderAndUploadReadme;
2729
pub use self::send_publish_notifications::SendPublishNotificationsJob;
2830
pub use self::sync_admins::SyncAdmins;

src/worker/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl RunnerExt for Runner<Arc<Environment>> {
2626
.register_job_type::<jobs::DeleteCrateFromStorage>()
2727
.register_job_type::<jobs::DumpDb>()
2828
.register_job_type::<jobs::IndexVersionDownloadsArchive>()
29+
.register_job_type::<jobs::InvalidateCdns>()
2930
.register_job_type::<jobs::NormalizeIndex>()
3031
.register_job_type::<jobs::ProcessCdnLog>()
3132
.register_job_type::<jobs::ProcessCdnLogQueue>()

0 commit comments

Comments
 (0)