5
5
6
6
use std:: { future:: Future , process:: ExitCode , time:: Duration } ;
7
7
8
- use futures_util:: future:: BoxFuture ;
8
+ use futures_util:: future:: { BoxFuture , Either } ;
9
9
use mas_handlers:: ActivityTracker ;
10
10
use mas_templates:: Templates ;
11
11
use tokio:: signal:: unix:: { Signal , SignalKind } ;
@@ -131,12 +131,28 @@ impl LifecycleManager {
131
131
pub async fn run ( mut self ) -> ExitCode {
132
132
notify ( & [ sd_notify:: NotifyState :: Ready ] ) ;
133
133
134
- let mut watchdog_usec = 0 ;
135
- let watchdog_enabled = sd_notify:: watchdog_enabled ( false , & mut watchdog_usec) ;
136
- let mut watchdog_interval = tokio:: time:: interval ( Duration :: from_micros ( watchdog_usec / 2 ) ) ;
134
+ // This will be `Some` if we have the watchdog enabled, and `None` if not
135
+ let mut watchdog_interval = {
136
+ let mut watchdog_usec = 0 ;
137
+ if sd_notify:: watchdog_enabled ( false , & mut watchdog_usec) {
138
+ Some ( tokio:: time:: interval ( Duration :: from_micros (
139
+ watchdog_usec / 2 ,
140
+ ) ) )
141
+ } else {
142
+ None
143
+ }
144
+ } ;
137
145
138
146
// Wait for a first shutdown signal and trigger the soft shutdown
139
147
let likely_crashed = loop {
148
+ // This makes a Future that will either yield the watchdog tick if enabled, or a
149
+ // pending Future if not
150
+ let watchdog_tick = if let Some ( watchdog_interval) = & mut watchdog_interval {
151
+ Either :: Left ( watchdog_interval. tick ( ) )
152
+ } else {
153
+ Either :: Right ( futures_util:: future:: pending ( ) )
154
+ } ;
155
+
140
156
tokio:: select! {
141
157
( ) = self . soft_shutdown_token. cancelled( ) => {
142
158
tracing:: warn!( "Another task triggered a shutdown, it likely crashed! Shutting down" ) ;
@@ -153,7 +169,7 @@ impl LifecycleManager {
153
169
break false ;
154
170
} ,
155
171
156
- _ = watchdog_interval . tick ( ) , if watchdog_enabled => {
172
+ _ = watchdog_tick => {
157
173
notify( & [
158
174
sd_notify:: NotifyState :: Watchdog ,
159
175
] ) ;
0 commit comments