@@ -118,7 +118,8 @@ async def lifespan(app: FastAPI):
118118 if os .path .exists (app .state .state_file ):
119119 os .remove (app .state .state_file )
120120
121- await check_heartbeat ()
121+ if START_MONITOR :
122+ await check_heartbeat ()
122123
123124 yield
124125
@@ -196,36 +197,34 @@ def send_email(message: str, subject: str):
196197 )
197198
198199
199- if START_MONITOR :
200-
201- @fastapi_utils .tasks .repeat_every (
202- seconds = config ["outside_monitor.check_heartbeat_every" ],
203- logger = logger ,
204- raise_exceptions = False ,
205- )
206- async def check_heartbeat ():
207- """Checks if we have received a heartbeat from LCO or sends an alert."""
208-
209- logger .debug ("Checking heartbeat." )
200+ @fastapi_utils .tasks .repeat_every (
201+ seconds = config ["outside_monitor.check_heartbeat_every" ],
202+ logger = logger ,
203+ raise_exceptions = False ,
204+ )
205+ async def check_heartbeat ():
206+ """Checks if we have received a heartbeat from LCO or sends an alert."""
210207
211- if not app .state .enabled :
212- logger .debug ("Heartbeat monitor is disabled." )
213- return
208+ logger .debug ("Checking heartbeat." )
214209
215- now = time .time ()
210+ if not app .state .enabled :
211+ logger .debug ("Heartbeat monitor is disabled." )
212+ return
216213
217- if not app .state .last_seen :
218- # Wait max_time_to_alert before sending the first alert.
219- if not app .state .active and now - app .state .started_at >= MAX_TIME_TO_ALERT :
220- await send_internet_down_notification ()
214+ now = time .time ()
221215
222- elif not app .state .active and now - app .state .last_seen > MAX_TIME_TO_ALERT :
216+ if not app .state .last_seen :
217+ # Wait max_time_to_alert before sending the first alert.
218+ if not app .state .active and now - app .state .started_at >= MAX_TIME_TO_ALERT :
223219 await send_internet_down_notification ()
224220
225- elif app .state .active and now - app .state .last_seen < MAX_TIME_TO_ALERT :
226- await send_internet_up_notification ()
221+ elif not app .state .active and now - app .state .last_seen > MAX_TIME_TO_ALERT :
222+ await send_internet_down_notification ()
223+
224+ elif app .state .active and now - app .state .last_seen < MAX_TIME_TO_ALERT :
225+ await send_internet_up_notification ()
227226
228- update_state_file (app .state )
227+ update_state_file (app .state )
229228
230229
231230async def send_internet_down_notification ():
0 commit comments