@@ -15,7 +15,7 @@ use std::{
1515 } ,
1616 time:: { Duration , Instant } ,
1717} ;
18- use tokio:: sync:: mpsc;
18+ use tokio:: sync:: { mpsc, oneshot } ;
1919
2020#[ cfg( unix) ]
2121use crate :: crashtracker:: crashtracker_unix_socket_path;
@@ -32,7 +32,28 @@ use crate::tracer::SHM_LIMITER;
3232use crate :: watchdog:: Watchdog ;
3333use crate :: { ddog_daemon_entry_point, setup_daemon_process} ;
3434
35- async fn main_loop < L , C , Fut > ( listener : L , cancel : Arc < C > ) -> io:: Result < ( ) >
35+ /// Configuration for main_loop behavior
36+ pub struct MainLoopConfig {
37+ pub enable_ctrl_c_handler : bool ,
38+ pub enable_crashtracker : bool ,
39+ pub external_shutdown_rx : Option < oneshot:: Receiver < ( ) > > ,
40+ }
41+
42+ impl Default for MainLoopConfig {
43+ fn default ( ) -> Self {
44+ Self {
45+ enable_ctrl_c_handler : true ,
46+ enable_crashtracker : true ,
47+ external_shutdown_rx : None ,
48+ }
49+ }
50+ }
51+
52+ pub async fn main_loop < L , C , Fut > (
53+ listener : L ,
54+ cancel : Arc < C > ,
55+ loop_config : MainLoopConfig ,
56+ ) -> io:: Result < ( ) >
3657where
3758 L : FnOnce ( Box < dyn Fn ( IpcClient ) > ) -> Fut ,
3859 Fut : Future < Output = io:: Result < ( ) > > ,
@@ -64,29 +85,45 @@ where
6485 }
6586 } ) ;
6687
67- tokio:: spawn ( async move {
68- if let Err ( err) = tokio:: signal:: ctrl_c ( ) . await {
69- tracing:: error!( "Error setting up signal handler {}" , err) ;
70- }
71- tracing:: info!( "Received Ctrl-C Signal, shutting down" ) ;
72- cancel ( ) ;
73- } ) ;
88+ if let Some ( shutdown_rx) = loop_config. external_shutdown_rx {
89+ let cancel = cancel. clone ( ) ;
90+ tokio:: spawn ( async move {
91+ let _ = shutdown_rx. await ;
92+ tracing:: info!( "External shutdown signal received" ) ;
93+ cancel ( ) ;
94+ } ) ;
95+ }
96+
97+ if loop_config. enable_ctrl_c_handler {
98+ let cancel = cancel. clone ( ) ;
99+ tokio:: spawn ( async move {
100+ if let Err ( err) = tokio:: signal:: ctrl_c ( ) . await {
101+ tracing:: error!( "Error setting up signal handler {}" , err) ;
102+ }
103+ tracing:: info!( "Received Ctrl-C Signal, shutting down" ) ;
104+ cancel ( ) ;
105+ } ) ;
106+ }
74107
75108 #[ cfg( unix) ]
76- tokio:: spawn ( async move {
77- let socket_path = crashtracker_unix_socket_path ( ) ;
78- match libdd_crashtracker:: get_receiver_unix_socket ( socket_path. to_str ( ) . unwrap_or_default ( ) )
79- {
80- Ok ( listener) => loop {
81- if let Err ( e) =
82- libdd_crashtracker:: async_receiver_entry_point_unix_listener ( & listener) . await
83- {
84- tracing:: warn!( "Got error while receiving crash report: {e}" ) ;
85- }
86- } ,
87- Err ( e) => tracing:: error!( "Failed setting up the crashtracker listener: {e}" ) ,
88- }
89- } ) ;
109+ if loop_config. enable_crashtracker {
110+ tokio:: spawn ( async move {
111+ let socket_path = crashtracker_unix_socket_path ( ) ;
112+ match libdd_crashtracker:: get_receiver_unix_socket (
113+ socket_path. to_str ( ) . unwrap_or_default ( ) ,
114+ ) {
115+ Ok ( listener) => loop {
116+ if let Err ( e) =
117+ libdd_crashtracker:: async_receiver_entry_point_unix_listener ( & listener)
118+ . await
119+ {
120+ tracing:: warn!( "Got error while receiving crash report: {e}" ) ;
121+ }
122+ } ,
123+ Err ( e) => tracing:: error!( "Failed setting up the crashtracker listener: {e}" ) ,
124+ }
125+ } ) ;
126+ }
90127
91128 // Init. Early, before we start listening.
92129 drop ( SHM_LIMITER . lock ( ) ) ;
@@ -143,6 +180,19 @@ where
143180}
144181
145182pub fn enter_listener_loop < F , L , Fut , C > ( acquire_listener : F ) -> anyhow:: Result < ( ) >
183+ where
184+ F : FnOnce ( ) -> io:: Result < ( L , C ) > ,
185+ L : FnOnce ( Box < dyn Fn ( IpcClient ) > ) -> Fut ,
186+ Fut : Future < Output = io:: Result < ( ) > > ,
187+ C : Fn ( ) + Sync + Send + ' static ,
188+ {
189+ enter_listener_loop_with_config ( acquire_listener, MainLoopConfig :: default ( ) )
190+ }
191+
192+ pub fn enter_listener_loop_with_config < F , L , Fut , C > (
193+ acquire_listener : F ,
194+ loop_config : MainLoopConfig ,
195+ ) -> anyhow:: Result < ( ) >
146196where
147197 F : FnOnce ( ) -> io:: Result < ( L , C ) > ,
148198 L : FnOnce ( Box < dyn Fn ( IpcClient ) > ) -> Fut ,
@@ -159,7 +209,7 @@ where
159209 let ( listener, cancel) = acquire_listener ( ) ?;
160210
161211 runtime
162- . block_on ( main_loop ( listener, Arc :: new ( cancel) ) )
212+ . block_on ( main_loop ( listener, Arc :: new ( cancel) , loop_config ) )
163213 . map_err ( |e| e. into ( ) )
164214}
165215
0 commit comments