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,12 +28,13 @@ 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 DUMMY_CRATE_NAME : & str = "empty-library" ;
3435const DUMMY_CRATE_VERSION : & str = "1.0.0" ;
3536
37+ #[ derive( Debug ) ]
3638pub enum PackageKind < ' a > {
3739 Local ( & ' a Path ) ,
3840 CratesIo ,
@@ -47,6 +49,7 @@ pub struct RustwideBuilder {
4749 storage : Arc < Storage > ,
4850 metrics : Arc < Metrics > ,
4951 index : Arc < Index > ,
52+ artifact_cache : ArtifactCache ,
5053 rustc_version : String ,
5154 repository_stats_updater : Arc < RepositoryStatsUpdater > ,
5255 skip_build_if_exists : bool ,
@@ -89,6 +92,7 @@ impl RustwideBuilder {
8992 Ok ( RustwideBuilder {
9093 workspace,
9194 toolchain,
95+ artifact_cache : ArtifactCache :: new ( config. prefix . join ( "artifact_cache" ) ) ?,
9296 config,
9397 db : context. pool ( ) ?,
9498 storage : context. storage ( ) ?,
@@ -197,6 +201,7 @@ impl RustwideBuilder {
197201
198202 let has_changed = old_version. as_deref ( ) != Some ( & self . rustc_version ) ;
199203 if has_changed {
204+ self . artifact_cache . purge ( ) ?;
200205 self . add_essential_files ( ) ?;
201206 }
202207 Ok ( has_changed)
@@ -319,6 +324,7 @@ impl RustwideBuilder {
319324 self . build_package ( & package. name , & package. version , PackageKind :: Local ( path) )
320325 }
321326
327+ #[ instrument( skip( self ) ) ]
322328 pub fn build_package (
323329 & mut self ,
324330 name : & str ,
@@ -383,6 +389,34 @@ impl RustwideBuilder {
383389 ( || -> Result < bool > {
384390 use docsrs_metadata:: BuildTargets ;
385391
392+ let release_data = match self
393+ . index
394+ . api ( )
395+ . get_release_data ( name, version)
396+ . context ( "error fetching release data from crates.io" )
397+ {
398+ Ok ( data) => data,
399+ Err ( err) => {
400+ warn ! ( "{:#?}" , err) ;
401+ ReleaseData :: default ( )
402+ }
403+ } ;
404+
405+ if let Some ( ref published_by) = release_data. published_by {
406+ info ! (
407+ host_target_dir=?build. host_target_dir( ) ,
408+ published_by_id=published_by. id,
409+ published_by_login=published_by. login,
410+ "restoring artifact cache" ,
411+ ) ;
412+ if let Err ( err) = self
413+ . artifact_cache
414+ . restore_to ( & published_by. id . to_string ( ) , build. host_target_dir ( ) )
415+ {
416+ warn ! ( ?err, "could not restore artifact cache" ) ;
417+ }
418+ }
419+
386420 let mut has_docs = false ;
387421 let mut successful_targets = Vec :: new ( ) ;
388422 let metadata = Metadata :: from_crate_root ( build. host_source_dir ( ) ) ?;
@@ -534,6 +568,22 @@ impl RustwideBuilder {
534568 Err ( err) => warn ! ( "{:#?}" , err) ,
535569 }
536570
571+ if let Some ( ref published_by) = release_data. published_by {
572+ info ! (
573+ host_target_dir=?build. host_target_dir( ) ,
574+ published_by_id=published_by. id,
575+ published_by_login=published_by. login,
576+ "saving artifact cache" ,
577+ ) ;
578+ if let Err ( err) = self
579+ . artifact_cache
580+ . save ( & published_by. id . to_string ( ) , build. host_target_dir ( ) )
581+ . context ( "error giving back artifact cache" )
582+ {
583+ warn ! ( ?err, "could not give back artifact cache" ) ;
584+ } ;
585+ }
586+
537587 if res. result . successful {
538588 // delete eventually existing files from pre-archive storage.
539589 // we're doing this in the end so eventual problems in the build
0 commit comments