Skip to content

Commit c01554f

Browse files
committed
jobs/sync_to_sparse_index: Add Fastly CDN support
1 parent c254fa5 commit c01554f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/config/server.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ pub struct Server {
9999

100100
/// Include publication timestamp in index entries (ISO8601 format).
101101
pub index_include_pubtime: bool,
102+
103+
/// Enable Fastly CDN invalidation for sparse index files.
104+
pub sparse_index_fastly_enabled: bool,
102105
}
103106

104107
impl Server {
@@ -248,6 +251,8 @@ impl Server {
248251
disable_token_creation,
249252
banner_message,
250253
index_include_pubtime,
254+
sparse_index_fastly_enabled: var_parsed("SPARSE_INDEX_FASTLY_ENABLED")?
255+
.unwrap_or(false),
251256
})
252257
}
253258
}

src/worker/jobs/index/sync.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fs::File;
1212
use std::io::{ErrorKind, Write};
1313
use std::sync::Arc;
1414
use std::time::Instant;
15-
use tracing::{debug, info, instrument};
15+
use tracing::{debug, info, instrument, warn};
1616

1717
#[derive(Serialize, Deserialize)]
1818
pub struct SyncToGitIndex {
@@ -121,9 +121,28 @@ impl BackgroundJob for SyncToSparseIndex {
121121
let future = env.storage.sync_index(&self.krate, content);
122122
future.await.context("Failed to sync index data")?;
123123

124-
if env.cloudfront().is_some() {
125-
let path = Repository::relative_index_file_for_url(&self.krate);
124+
let path = Repository::relative_index_file_for_url(&self.krate);
125+
126+
if let Some(fastly) = env.fastly()
127+
&& env.config.sparse_index_fastly_enabled
128+
{
129+
let domain_name = &env.config.domain_name;
130+
let domains = [
131+
format!("index.{}", domain_name),
132+
format!("fastly-index.{}", domain_name),
133+
];
134+
135+
for domain in domains {
136+
if let Err(error) = fastly.purge(&domain, &path).await {
137+
warn!(
138+
domain,
139+
path, "Failed to invalidate sparse index on Fastly: {error}"
140+
);
141+
}
142+
}
143+
}
126144

145+
if env.cloudfront().is_some() {
127146
info!(%path, "Queuing index file invalidation on CloudFront");
128147

129148
let paths = &[path];

0 commit comments

Comments
 (0)