@@ -154,6 +154,18 @@ impl SwitchNSquashService {
154154 }
155155}
156156
157+ async fn get_keyset ( pool : PgPool ,
158+ keys_cache : Arc < RwLock < lru:: LruCache < String , KeySet > > > ,
159+ tenant_api_key : & String
160+ ) -> Result < Option < KeySet > , ExecutionError > {
161+ let t = telemetry:: tracer ( "worker_loop_init" ) ;
162+ let s = t. child_span ( "fetch_keyset" ) ;
163+ let keys: Option < KeySet > = fetch_keyset ( & keys_cache, & pool, tenant_api_key) . await ?;
164+ telemetry:: end_span ( s) ;
165+ t. end ( ) ;
166+ Ok ( keys)
167+ }
168+
157169/// Executes the worker logic for the SnS task.
158170pub ( crate ) async fn run_loop (
159171 conf : Config ,
@@ -173,13 +185,16 @@ pub(crate) async fn run_loop(
173185 . listen_all ( conf. db . listen_channels . iter ( ) . map ( |v| v. as_str ( ) ) )
174186 . await ?;
175187
176- let t = telemetry:: tracer ( "worker_loop_init" ) ;
177- let s = t. child_span ( "fetch_keyset" ) ;
178- let keys: KeySet = fetch_keyset ( & keys_cache, & pool, tenant_api_key) . await ?;
179- telemetry:: end_span ( s) ;
180- t. end ( ) ;
181-
182- info ! ( tenant_api_key = tenant_api_key, "Fetched keyset" ) ;
188+ let mut keys = match get_keyset ( pool. clone ( ) , keys_cache. clone ( ) , tenant_api_key) . await ? {
189+ Some ( keys) => {
190+ info ! ( tenant_api_key = tenant_api_key, "Fetched keyset" ) ;
191+ Some ( keys)
192+ }
193+ None => {
194+ warn ! ( tenant_api_key = tenant_api_key, "No keys found for the given tenant_api_key" ) ;
195+ None
196+ }
197+ } ;
183198
184199 let mut gc_ticker = interval ( conf. db . cleanup_interval ) ;
185200 let mut gc_timestamp = SystemTime :: now ( ) ;
@@ -189,6 +204,16 @@ pub(crate) async fn run_loop(
189204 // Continue looping until the service is cancelled or a critical error occurs
190205 update_last_active ( last_active_at. clone ( ) ) . await ;
191206
207+ let Some ( keys) = keys. as_ref ( ) else {
208+ warn ! ( tenant_api_key = tenant_api_key, "No keys available, retrying in 5 seconds" ) ;
209+ tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
210+ if token. is_cancelled ( ) {
211+ return Ok ( ( ) ) ;
212+ }
213+ keys = get_keyset ( pool. clone ( ) , keys_cache. clone ( ) , tenant_api_key) . await ?;
214+ continue ;
215+ } ;
216+
192217 let maybe_remaining = fetch_and_execute_sns_tasks ( & pool, & tx, & keys, & conf, & token) . await ?;
193218 if maybe_remaining {
194219 if token. is_cancelled ( ) {
0 commit comments