@@ -19,6 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
19
19
let cancellation_token = token. clone ( ) ;
20
20
// add cancellation check
21
21
tokio:: spawn ( async move {
22
+ // FIXME: weird feature-gating here bugs with IDE, fix this later
22
23
#[ cfg( feature = "profiling" ) ]
23
24
{
24
25
const PROFILE_DURATION_SECS : u64 = 120 ;
@@ -86,22 +87,50 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
86
87
Ok ( ( ) )
87
88
}
88
89
89
- /// Waits for SIGTERM or SIGINT, and cancels the given token when the signal is received.
90
+ // FIXME: remove this `unused` once we have a better way to handle this
91
+ /// Waits for various termination signals, and cancels the given token when the signal is received.
92
+ ///
93
+ /// Handles Unix and Windows [target families](https://doc.rust-lang.org/reference/conditional-compilation.html#target_family).
90
94
#[ allow( unused) ]
91
95
async fn wait_for_termination ( cancellation : CancellationToken ) -> std:: io:: Result < ( ) > {
92
- use tokio:: signal:: unix:: { signal, SignalKind } ;
96
+ #[ cfg( unix) ]
97
+ {
98
+ use tokio:: signal:: unix:: { signal, SignalKind } ;
99
+ let mut sigterm = signal ( SignalKind :: terminate ( ) ) ?; // Docker sends SIGTERM
100
+ let mut sigint = signal ( SignalKind :: interrupt ( ) ) ?; // Ctrl+C sends SIGINT
101
+ tokio:: select! {
102
+ _ = sigterm. recv( ) => log:: warn!( "Recieved SIGTERM" ) ,
103
+ _ = sigint. recv( ) => log:: warn!( "Recieved SIGINT" ) ,
104
+ _ = cancellation. cancelled( ) => {
105
+ // no need to wait if cancelled anyways
106
+ // although this is not likely to happen
107
+ return Ok ( ( ) ) ;
108
+ }
109
+ } ;
110
+ }
93
111
94
- let mut sigterm = signal ( SignalKind :: terminate ( ) ) ?; // Docker sends SIGTERM
95
- let mut sigint = signal ( SignalKind :: interrupt ( ) ) ?; // Ctrl+C sends SIGINT
96
- tokio:: select! {
97
- _ = sigterm. recv( ) => log:: warn!( "Recieved SIGTERM" ) ,
98
- _ = sigint. recv( ) => log:: warn!( "Recieved SIGINT" ) ,
99
- _ = cancellation. cancelled( ) => {
100
- // no need to wait if cancelled anyways
101
- // although this is not likely to happen
102
- return Ok ( ( ) ) ;
103
- }
104
- } ;
112
+ #[ cfg( windows) ]
113
+ {
114
+ use tokio:: signal:: windows;
115
+
116
+ // https://learn.microsoft.com/en-us/windows/console/handlerroutine
117
+ let mut signal_c = windows:: ctrl_c ( ) ?;
118
+ let mut signal_break = windows:: ctrl_break ( ) ?;
119
+ let mut signal_close = windows:: ctrl_close ( ) ?;
120
+ let mut signal_shutdown = windows:: ctrl_shutdown ( ) ?;
121
+
122
+ tokio:: select! {
123
+ _ = signal_c. recv( ) => log:: warn!( "Received CTRL_C" ) ,
124
+ _ = signal_break. recv( ) => log:: warn!( "Received CTRL_BREAK" ) ,
125
+ _ = signal_close. recv( ) => log:: warn!( "Received CTRL_CLOSE" ) ,
126
+ _ = signal_shutdown. recv( ) => log:: warn!( "Received CTRL_SHUTDOWN" ) ,
127
+ _ = cancellation. cancelled( ) => {
128
+ // no need to wait if cancelled anyways
129
+ // although this is not likely to happen
130
+ return Ok ( ( ) ) ;
131
+ }
132
+ } ;
133
+ }
105
134
106
135
log:: info!( "Terminating the node..." ) ;
107
136
cancellation. cancel ( ) ;
0 commit comments