@@ -25,6 +25,7 @@ use mas_templates::Templates;
2525use opentelemetry:: { metrics:: Histogram , KeyValue } ;
2626use rand:: SeedableRng ;
2727use sqlx:: PgPool ;
28+ use tracing:: Instrument ;
2829
2930use crate :: telemetry:: METER ;
3031
@@ -85,29 +86,43 @@ impl AppState {
8586 self . conn_acquisition_histogram = Some ( histogram) ;
8687 }
8788
88- /// Init the metadata cache.
89- ///
90- /// # Panics
91- ///
92- /// Panics if the metadata cache could not be initialized.
93- pub async fn init_metadata_cache ( & self ) {
94- // XXX: this panics because the error is annoying to propagate
95- let conn = self
96- . pool
97- . acquire ( )
98- . await
99- . expect ( "Failed to acquire a database connection" ) ;
100-
101- let mut repo = PgRepository :: from_conn ( conn) ;
102-
103- self . metadata_cache
104- . warm_up_and_run (
105- & self . http_client ,
106- std:: time:: Duration :: from_secs ( 60 * 15 ) ,
107- & mut repo,
108- )
109- . await
110- . expect ( "Failed to warm up the metadata cache" ) ;
89+ /// Init the metadata cache in the background
90+ pub fn init_metadata_cache ( & self ) {
91+ let pool = self . pool . clone ( ) ;
92+ let metadata_cache = self . metadata_cache . clone ( ) ;
93+ let http_client = self . http_client . clone ( ) ;
94+
95+ tokio:: spawn (
96+ async move {
97+ let conn = match pool. acquire ( ) . await {
98+ Ok ( conn) => conn,
99+ Err ( e) => {
100+ tracing:: error!(
101+ error = & e as & dyn std:: error:: Error ,
102+ "Failed to acquire a database connection"
103+ ) ;
104+ return ;
105+ }
106+ } ;
107+
108+ let mut repo = PgRepository :: from_conn ( conn) ;
109+
110+ if let Err ( e) = metadata_cache
111+ . warm_up_and_run (
112+ & http_client,
113+ std:: time:: Duration :: from_secs ( 60 * 15 ) ,
114+ & mut repo,
115+ )
116+ . await
117+ {
118+ tracing:: error!(
119+ error = & e as & dyn std:: error:: Error ,
120+ "Failed to warm up the metadata cache"
121+ ) ;
122+ }
123+ }
124+ . instrument ( tracing:: info_span!( "metadata_cache.background_warmup" ) ) ,
125+ ) ;
111126 }
112127}
113128
0 commit comments