1+ use super :: caching:: ArtifactCache ;
12use crate :: db:: file:: add_path_into_database;
23use crate :: db:: {
34 add_build_into_database, add_doc_coverage, add_package_into_database,
@@ -27,13 +28,14 @@ use rustwide::{AlternativeRegistry, Build, Crate, Toolchain, Workspace, Workspac
2728use std:: collections:: { HashMap , HashSet } ;
2829use std:: path:: Path ;
2930use std:: sync:: Arc ;
30- use tracing:: { debug, info, warn} ;
31+ use tracing:: { debug, info, instrument , warn} ;
3132
3233const USER_AGENT : & str = "docs.rs builder (https://github.com/rust-lang/docs.rs)" ;
3334const COMPONENTS : & [ & str ] = & [ "llvm-tools-preview" , "rustc-dev" , "rustfmt" ] ;
3435const DUMMY_CRATE_NAME : & str = "empty-library" ;
3536const DUMMY_CRATE_VERSION : & str = "1.0.0" ;
3637
38+ #[ derive( Debug ) ]
3739pub enum PackageKind < ' a > {
3840 Local ( & ' a Path ) ,
3941 CratesIo ,
@@ -48,6 +50,7 @@ pub struct RustwideBuilder {
4850 storage : Arc < Storage > ,
4951 metrics : Arc < Metrics > ,
5052 index : Arc < Index > ,
53+ artifact_cache : ArtifactCache ,
5154 rustc_version : String ,
5255 repository_stats_updater : Arc < RepositoryStatsUpdater > ,
5356 skip_build_if_exists : bool ,
@@ -90,6 +93,7 @@ impl RustwideBuilder {
9093 Ok ( RustwideBuilder {
9194 workspace,
9295 toolchain,
96+ artifact_cache : ArtifactCache :: new ( config. prefix . join ( "artifact_cache" ) ) ?,
9397 config,
9498 db : context. pool ( ) ?,
9599 storage : context. storage ( ) ?,
@@ -200,6 +204,7 @@ impl RustwideBuilder {
200204
201205 let has_changed = old_version. as_deref ( ) != Some ( & self . rustc_version ) ;
202206 if has_changed {
207+ self . artifact_cache . purge ( ) ?;
203208 self . add_essential_files ( ) ?;
204209 }
205210 Ok ( has_changed)
@@ -322,6 +327,7 @@ impl RustwideBuilder {
322327 self . build_package ( & package. name , & package. version , PackageKind :: Local ( path) )
323328 }
324329
330+ #[ instrument( skip( self ) ) ]
325331 pub fn build_package (
326332 & mut self ,
327333 name : & str ,
@@ -386,6 +392,34 @@ impl RustwideBuilder {
386392 ( || -> Result < bool > {
387393 use docsrs_metadata:: BuildTargets ;
388394
395+ let release_data = match self
396+ . index
397+ . api ( )
398+ . get_release_data ( name, version)
399+ . context ( "error fetching release data from crates.io" )
400+ {
401+ Ok ( data) => data,
402+ Err ( err) => {
403+ warn ! ( "{:#?}" , err) ;
404+ ReleaseData :: default ( )
405+ }
406+ } ;
407+
408+ if let Some ( ref published_by) = release_data. published_by {
409+ info ! (
410+ host_target_dir=?build. host_target_dir( ) ,
411+ published_by_id=published_by. id,
412+ published_by_login=published_by. login,
413+ "restoring artifact cache" ,
414+ ) ;
415+ if let Err ( err) = self
416+ . artifact_cache
417+ . restore_to ( & published_by. id . to_string ( ) , build. host_target_dir ( ) )
418+ {
419+ warn ! ( ?err, "could not restore artifact cache" ) ;
420+ }
421+ }
422+
389423 let mut has_docs = false ;
390424 let mut successful_targets = Vec :: new ( ) ;
391425 let metadata = Metadata :: from_crate_root ( build. host_source_dir ( ) ) ?;
@@ -537,6 +571,22 @@ impl RustwideBuilder {
537571 Err ( err) => warn ! ( "{:#?}" , err) ,
538572 }
539573
574+ if let Some ( ref published_by) = release_data. published_by {
575+ info ! (
576+ host_target_dir=?build. host_target_dir( ) ,
577+ published_by_id=published_by. id,
578+ published_by_login=published_by. login,
579+ "saving artifact cache" ,
580+ ) ;
581+ if let Err ( err) = self
582+ . artifact_cache
583+ . save ( & published_by. id . to_string ( ) , build. host_target_dir ( ) )
584+ . context ( "error giving back artifact cache" )
585+ {
586+ warn ! ( ?err, "could not give back artifact cache" ) ;
587+ } ;
588+ }
589+
540590 if res. result . successful {
541591 // delete eventually existing files from pre-archive storage.
542592 // we're doing this in the end so eventual problems in the build
0 commit comments