@@ -25,6 +25,7 @@ use mas_templates::Templates;
25
25
use opentelemetry:: { metrics:: Histogram , KeyValue } ;
26
26
use rand:: SeedableRng ;
27
27
use sqlx:: PgPool ;
28
+ use tracing:: Instrument ;
28
29
29
30
use crate :: telemetry:: METER ;
30
31
@@ -85,29 +86,43 @@ impl AppState {
85
86
self . conn_acquisition_histogram = Some ( histogram) ;
86
87
}
87
88
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
+ ) ;
111
126
}
112
127
}
113
128
0 commit comments