@@ -18,6 +18,16 @@ use tokio::signal::unix::{signal, SignalKind};
18
18
use tokio:: signal:: windows:: ctrl_c;
19
19
use tokio:: time:: timeout;
20
20
21
+ mod signal;
22
+ #[ cfg( unix) ]
23
+ use signal:: unix_signal_handler:: UnixSignalHandler as SignalHandlerImpl ;
24
+ #[ cfg( windows) ]
25
+ use windows_signal_handler:: WindowsSignalHandler as SignalHandlerImpl ;
26
+
27
+ use signal:: SignalHandler ;
28
+
29
+
30
+
21
31
fn main ( ) -> Result < ( ) > {
22
32
// because this function exits right away it does not
23
33
// properly handle destruction of data, so we put the actual
@@ -144,32 +154,16 @@ impl CmdMonitor {
144
154
}
145
155
146
156
pub async fn run_async ( & mut self , config : & spfs:: Config ) -> Result < i32 > {
147
- #[ cfg( unix) ]
148
- let mut interrupt = signal ( SignalKind :: interrupt ( ) )
149
- . map_err ( |err| Error :: process_spawn_error ( "signal()" , err, None ) ) ?;
150
- #[ cfg( windows) ]
151
- let mut interrupt =
152
- ctrl_c ( ) . map_err ( |err| Error :: process_spawn_error ( "ctrl_c()" , err, None ) ) ?;
153
- #[ cfg( unix) ]
154
- let mut quit = signal ( SignalKind :: quit ( ) )
155
- . map_err ( |err| Error :: process_spawn_error ( "signal()" , err, None ) ) ?;
156
- #[ cfg( windows) ]
157
- let mut quit = ctrl_c ( ) . map_err ( |err| Error :: process_spawn_error ( "ctrl_c()" , err, None ) ) ?;
158
- #[ cfg( unix) ]
159
- let mut terminate = signal ( SignalKind :: terminate ( ) )
160
- . map_err ( |err| Error :: process_spawn_error ( "signal()" , err, None ) ) ?;
161
- #[ cfg( windows) ]
162
- let mut terminate =
163
- ctrl_c ( ) . map_err ( |err| Error :: process_spawn_error ( "ctrl_c()" , err, None ) ) ?;
164
-
157
+ let signal_future = SignalHandlerImpl :: build_signal_future ( ) ;
158
+
165
159
let repo = spfs:: open_repository ( & self . runtime_storage ) . await ?;
166
160
let storage = spfs:: runtime:: Storage :: new ( repo) ?;
167
161
let runtime = storage. read_runtime ( & self . runtime ) . await ?;
168
162
tracing:: trace!( "read runtime from storage repo" ) ;
169
-
163
+
170
164
let mut owned = spfs:: runtime:: OwnedRuntime :: upgrade_as_monitor ( runtime) . await ?;
171
165
tracing:: trace!( "upgraded to owned runtime, waiting for empty runtime" ) ;
172
-
166
+
173
167
let fut = spfs:: monitor:: wait_for_empty_runtime ( & owned, config) ;
174
168
let res = tokio:: select! {
175
169
res = fut => {
@@ -178,18 +172,16 @@ impl CmdMonitor {
178
172
}
179
173
// we explicitly catch any signal related to interruption
180
174
// and will act by cleaning up the runtime early
181
- _ = terminate. recv( ) => Err ( spfs:: Error :: String ( "Terminate signal received, cleaning up runtime early" . to_string( ) ) ) ,
182
- _ = interrupt. recv( ) => Err ( spfs:: Error :: String ( "Interrupt signal received, cleaning up runtime early" . to_string( ) ) ) ,
183
- _ = quit. recv( ) => Err ( spfs:: Error :: String ( "Quit signal received, cleaning up runtime early" . to_string( ) ) ) ,
175
+ _ = signal_future => Err ( spfs:: Error :: String ( "Signal received, cleaning up runtime early" . to_string( ) ) ) ,
184
176
} ;
185
177
tracing:: trace!( "runtime empty of processes " ) ;
186
-
178
+
187
179
// need to reload the runtime here to get any changes made to
188
180
// the runtime while it was running so we don't blast them the
189
181
// next time this process saves the runtime state.
190
182
tracing:: trace!( "reloading runtime data before cleanup" ) ;
191
183
owned. reload_state_from_storage ( ) . await ?;
192
-
184
+
193
185
// try to set the running to false to make this
194
186
// runtime easier to identify as safe to delete
195
187
// if the automatic cleanup fails. Any error
@@ -198,12 +190,12 @@ impl CmdMonitor {
198
190
if let Err ( err) = owned. save_state_to_storage ( ) . await {
199
191
tracing:: error!( "failed to save runtime: {err:?}" ) ;
200
192
}
201
-
193
+
202
194
tracing:: trace!( "tearing down and exiting" ) ;
203
195
if let Err ( err) = spfs:: exit_runtime ( & owned) . await {
204
196
tracing:: error!( "failed to tear down runtime: {err:?}" ) ;
205
197
}
206
-
198
+
207
199
tracing:: trace!(
208
200
"{} runtime data" ,
209
201
if owned. is_durable( ) {
@@ -224,7 +216,7 @@ impl CmdMonitor {
224
216
} else if let Err ( err) = owned. delete ( ) . await {
225
217
tracing:: error!( "failed to clean up runtime data: {err:?}" )
226
218
}
227
-
219
+
228
220
res?;
229
221
Ok ( 0 )
230
222
}
0 commit comments