@@ -14,7 +14,7 @@ use mas_config::{
1414 AppConfig , ClientsConfig , ConfigurationSection , ConfigurationSectionExt , UpstreamOAuth2Config ,
1515} ;
1616use mas_handlers:: { ActivityTracker , CookieManager , HttpClientFactory , Limiter , MetadataCache } ;
17- use mas_listener:: { server:: Server , shutdown :: ShutdownStream } ;
17+ use mas_listener:: server:: Server ;
1818use mas_matrix_synapse:: SynapseConnection ;
1919use mas_router:: UrlBuilder ;
2020use mas_storage:: SystemClock ;
@@ -24,11 +24,11 @@ use rand::{
2424 thread_rng,
2525} ;
2626use sqlx:: migrate:: Migrate ;
27- use tokio:: signal:: unix:: SignalKind ;
2827use tracing:: { info, info_span, warn, Instrument } ;
2928
3029use crate :: {
3130 app_state:: AppState ,
31+ shutdown:: ShutdownManager ,
3232 util:: {
3333 database_pool_from_config, mailer_from_config, password_manager_from_config,
3434 policy_factory_from_config, register_sighup, site_config_from_config,
@@ -61,6 +61,7 @@ impl Options {
6161 #[ allow( clippy:: too_many_lines) ]
6262 pub async fn run ( self , figment : & Figment ) -> anyhow:: Result < ExitCode > {
6363 let span = info_span ! ( "cli.run.init" ) . entered ( ) ;
64+ let shutdown = ShutdownManager :: new ( ) ?;
6465 let config = AppConfig :: extract ( figment) ?;
6566
6667 if self . migrate {
@@ -173,8 +174,21 @@ impl Options {
173174 url_builder. clone ( ) ,
174175 )
175176 . await ?;
176- // TODO: grab the handle
177- tokio:: spawn ( monitor. run ( ) ) ;
177+
178+ // XXX: The monitor from apalis is a bit annoying to use for graceful shutdowns,
179+ // ideally we'd just give it a cancellation token
180+ let shutdown_future = shutdown. soft_shutdown_token ( ) . cancelled_owned ( ) ;
181+ shutdown. task_tracker ( ) . spawn ( async move {
182+ if let Err ( e) = monitor
183+ . run_with_signal ( async move {
184+ shutdown_future. await ;
185+ Ok ( ( ) )
186+ } )
187+ . await
188+ {
189+ tracing:: error!( error = & e as & dyn std:: error:: Error , "Task worker failed" ) ;
190+ }
191+ } ) ;
178192 }
179193
180194 let listeners_config = config. http . listeners . clone ( ) ;
@@ -186,7 +200,12 @@ impl Options {
186200
187201 // Initialize the activity tracker
188202 // Activity is flushed every minute
189- let activity_tracker = ActivityTracker :: new ( pool. clone ( ) , Duration :: from_secs ( 60 ) ) ;
203+ let activity_tracker = ActivityTracker :: new (
204+ pool. clone ( ) ,
205+ Duration :: from_secs ( 60 ) ,
206+ shutdown. task_tracker ( ) ,
207+ shutdown. soft_shutdown_token ( ) ,
208+ ) ;
190209 let trusted_proxies = config. http . trusted_proxies . clone ( ) ;
191210
192211 // Build a rate limiter.
@@ -302,16 +321,17 @@ impl Options {
302321 . flatten_ok ( )
303322 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
304323
305- let shutdown = ShutdownStream :: default ( )
306- . with_timeout ( Duration :: from_secs ( 60 ) )
307- . with_signal ( SignalKind :: terminate ( ) ) ?
308- . with_signal ( SignalKind :: interrupt ( ) ) ?;
309-
310324 span. exit ( ) ;
311325
312- mas_listener:: server:: run_servers ( servers, shutdown) . await ;
326+ shutdown
327+ . task_tracker ( )
328+ . spawn ( mas_listener:: server:: run_servers (
329+ servers,
330+ shutdown. soft_shutdown_token ( ) ,
331+ shutdown. hard_shutdown_token ( ) ,
332+ ) ) ;
313333
314- state . activity_tracker . shutdown ( ) . await ;
334+ shutdown. run ( ) . await ;
315335
316336 Ok ( ExitCode :: SUCCESS )
317337 }
0 commit comments