|
28 | 28 |
|
29 | 29 | logger = logging.getLogger("uvicorn.error") |
30 | 30 |
|
| 31 | + |
| 32 | +def parse_bool_envvar(variable: str, default: bool = False) -> bool: |
| 33 | + """Parses a boolean environment variable.""" |
| 34 | + |
| 35 | + value = os.getenv(variable, default) |
| 36 | + |
| 37 | + if value is None: |
| 38 | + return default |
| 39 | + |
| 40 | + if isinstance(value, bool): |
| 41 | + return value |
| 42 | + |
| 43 | + if value.lower() in ["true", "yes", "1"]: |
| 44 | + return True |
| 45 | + elif value.lower() in ["false", "no", "0"]: |
| 46 | + return False |
| 47 | + else: |
| 48 | + raise ValueError(f"Invalid boolean value: {value}") |
| 49 | + |
| 50 | + |
31 | 51 | MAX_TIME_TO_ALERT: float = float( |
32 | 52 | os.getenv( |
33 | 53 | "LVMBEAT_SEND_EMAIL_AFTER", |
34 | 54 | config["outside_monitor.send_email_after"], |
35 | 55 | ) |
36 | 56 | ) |
37 | 57 |
|
| 58 | +START_MONITOR: bool = parse_bool_envvar("LVMBEAT_START_MONITOR", True) |
| 59 | + |
38 | 60 |
|
39 | 61 | @dataclass |
40 | 62 | class EmailSettings: |
@@ -174,34 +196,36 @@ def send_email(message: str, subject: str): |
174 | 196 | ) |
175 | 197 |
|
176 | 198 |
|
177 | | -@fastapi_utils.tasks.repeat_every( |
178 | | - seconds=config["outside_monitor.check_heartbeat_every"], |
179 | | - logger=logger, |
180 | | - raise_exceptions=False, |
181 | | -) |
182 | | -async def check_heartbeat(): |
183 | | - """Checks if we have received a heartbeat from LCO or sends an alert.""" |
| 199 | +if START_MONITOR: |
184 | 200 |
|
185 | | - logger.debug("Checking heartbeat.") |
| 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.""" |
186 | 208 |
|
187 | | - if not app.state.enabled: |
188 | | - logger.debug("Heartbeat monitor is disabled.") |
189 | | - return |
| 209 | + logger.debug("Checking heartbeat.") |
190 | 210 |
|
191 | | - now = time.time() |
| 211 | + if not app.state.enabled: |
| 212 | + logger.debug("Heartbeat monitor is disabled.") |
| 213 | + return |
192 | 214 |
|
193 | | - if not app.state.last_seen: |
194 | | - # Wait max_time_to_alert before sending the first alert. |
195 | | - if not app.state.active and now - app.state.started_at >= MAX_TIME_TO_ALERT: |
196 | | - await send_internet_down_notification() |
| 215 | + now = time.time() |
197 | 216 |
|
198 | | - elif not app.state.active and now - app.state.last_seen > MAX_TIME_TO_ALERT: |
199 | | - await send_internet_down_notification() |
| 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() |
200 | 221 |
|
201 | | - elif app.state.active and now - app.state.last_seen < MAX_TIME_TO_ALERT: |
202 | | - await send_internet_up_notification() |
| 222 | + elif not app.state.active and now - app.state.last_seen > MAX_TIME_TO_ALERT: |
| 223 | + await send_internet_down_notification() |
| 224 | + |
| 225 | + elif app.state.active and now - app.state.last_seen < MAX_TIME_TO_ALERT: |
| 226 | + await send_internet_up_notification() |
203 | 227 |
|
204 | | - update_state_file(app.state) |
| 228 | + update_state_file(app.state) |
205 | 229 |
|
206 | 230 |
|
207 | 231 | async def send_internet_down_notification(): |
|
0 commit comments