Skip to content

Commit ac1fde8

Browse files
committed
invalidate CDN in more build error cases, & invalidate after yank / create-delete
1 parent b5451e0 commit ac1fde8

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

src/bin/cratesfyi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ impl Context for BinContext {
606606
self.pool()?,
607607
self.metrics()?,
608608
self.config()?,
609+
self.cdn()?,
609610
self.storage()?,
610611
);
611612
fn storage(self) -> Storage = Storage::new(

src/build_queue.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cdn::{self, CdnBackend};
12
use crate::db::{delete_crate, Pool};
23
use crate::docbuilder::PackageKind;
34
use crate::error::Result;
@@ -27,6 +28,7 @@ pub(crate) struct QueuedCrate {
2728
#[derive(Debug)]
2829
pub struct BuildQueue {
2930
config: Arc<Config>,
31+
cdn: Arc<CdnBackend>,
3032
storage: Arc<Storage>,
3133
pub(crate) db: Pool,
3234
metrics: Arc<Metrics>,
@@ -38,11 +40,13 @@ impl BuildQueue {
3840
db: Pool,
3941
metrics: Arc<Metrics>,
4042
config: Arc<Config>,
43+
cdn: Arc<CdnBackend>,
4144
storage: Arc<Storage>,
4245
) -> Self {
4346
BuildQueue {
4447
max_attempts: config.build_attempts.into(),
4548
config,
49+
cdn,
4650
db,
4751
metrics,
4852
storage,
@@ -173,6 +177,10 @@ impl BuildQueue {
173177
)
174178
});
175179
self.metrics.total_builds.inc();
180+
if let Err(err) = cdn::invalidate_crate(&self.config, &self.cdn, &to_process.name) {
181+
report_error(&err);
182+
}
183+
176184
match res {
177185
Ok(()) => {
178186
transaction.execute("DELETE FROM queue WHERE id = $1;", &[&to_process.id])?;
@@ -284,6 +292,10 @@ impl BuildQueue {
284292
Ok(_) => debug!("{}-{} yanked", release.name, release.version),
285293
Err(err) => report_error(&err),
286294
}
295+
if let Err(err) = cdn::invalidate_crate(&self.config, &self.cdn, &release.name)
296+
{
297+
report_error(&err);
298+
}
287299
}
288300

289301
Change::Added(release) => {
@@ -319,6 +331,9 @@ impl BuildQueue {
319331
Ok(_) => info!("crate {} was deleted from the index and will be deleted from the database", krate),
320332
Err(err) => report_error(&err),
321333
}
334+
if let Err(err) = cdn::invalidate_crate(&self.config, &self.cdn, krate) {
335+
report_error(&err);
336+
}
322337
}
323338
}
324339
}

src/cdn.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::Config;
2-
use anyhow::{Error, Result};
2+
use anyhow::{Context, Error, Result};
33
use aws_sdk_cloudfront::{
44
model::{InvalidationBatch, Paths},
55
Client, RetryConfig,
@@ -18,6 +18,7 @@ pub(crate) enum CdnKind {
1818
CloudFront,
1919
}
2020

21+
#[derive(Debug)]
2122
pub enum CdnBackend {
2223
Dummy(Arc<Mutex<Vec<(String, String)>>>),
2324
CloudFront {
@@ -116,6 +117,18 @@ impl CdnBackend {
116117
}
117118
}
118119

120+
pub(crate) fn invalidate_crate(config: &Config, cdn: &CdnBackend, name: &str) -> Result<()> {
121+
if let Some(distribution_id) = config.cloudfront_distribution_id_web.as_ref() {
122+
cdn.create_invalidation(
123+
distribution_id,
124+
&[&format!("/{}*", name), &format!("/crate/{}*", name)],
125+
)
126+
.context("error creating CDN invalidation")?;
127+
}
128+
129+
Ok(())
130+
}
131+
119132
#[cfg(test)]
120133
mod tests {
121134
use super::*;

src/docbuilder/rustwide_builder.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::cdn::CdnBackend;
21
use crate::db::file::add_path_into_database;
32
use crate::db::{
43
add_build_into_database, add_doc_coverage, add_package_into_database,
@@ -10,12 +9,11 @@ use crate::index::api::ReleaseData;
109
use crate::repositories::RepositoryStatsUpdater;
1110
use crate::storage::{rustdoc_archive_path, source_archive_path};
1211
use crate::utils::{
13-
copy_dir_all, parse_rustc_version, queue_builder, report_error, set_config, CargoMetadata,
14-
ConfigName,
12+
copy_dir_all, parse_rustc_version, queue_builder, set_config, CargoMetadata, ConfigName,
1513
};
1614
use crate::{db::blacklist::is_blacklisted, utils::MetadataPackage};
1715
use crate::{Config, Context, Index, Metrics, Storage};
18-
use anyhow::{anyhow, bail, Context as _, Error};
16+
use anyhow::{anyhow, bail, Error};
1917
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
2018
use failure::Error as FailureError;
2119
use log::{debug, info, warn, LevelFilter};
@@ -44,7 +42,6 @@ pub struct RustwideBuilder {
4442
config: Arc<Config>,
4543
db: Pool,
4644
storage: Arc<Storage>,
47-
cdn: Arc<CdnBackend>,
4845
metrics: Arc<Metrics>,
4946
index: Arc<Index>,
5047
rustc_version: String,
@@ -83,7 +80,6 @@ impl RustwideBuilder {
8380
config,
8481
db: context.pool()?,
8582
storage: context.storage()?,
86-
cdn: context.cdn()?,
8783
metrics: context.metrics()?,
8884
index: context.index()?,
8985
rustc_version: String::new(),
@@ -504,18 +500,6 @@ impl RustwideBuilder {
504500
.purge_from_cache(&self.workspace)
505501
.map_err(FailureError::compat)?;
506502
local_storage.close()?;
507-
if let Some(distribution_id) = self.config.cloudfront_distribution_id_web.as_ref() {
508-
if let Err(err) = self
509-
.cdn
510-
.create_invalidation(
511-
distribution_id,
512-
&[&format!("/{}*", name), &format!("/crate/{}*", name)],
513-
)
514-
.context("error creating CDN invalidation")
515-
{
516-
report_error(&err);
517-
}
518-
}
519503
Ok(successful)
520504
}
521505

@@ -823,7 +807,7 @@ pub(crate) struct BuildResult {
823807
mod tests {
824808
use super::*;
825809
use crate::{
826-
cdn::CdnKind,
810+
cdn::{CdnBackend, CdnKind},
827811
test::{assert_redirect, assert_success, wrapper},
828812
};
829813
use serde_json::Value;

src/test/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ impl TestEnvironment {
292292
self.db().pool(),
293293
self.metrics(),
294294
self.config(),
295+
self.cdn(),
295296
self.storage(),
296297
))
297298
})

0 commit comments

Comments
 (0)