Skip to content

Commit ceab13c

Browse files
committed
Allow not starting the monitor loop by setting LVMBEAT_START_MONITOR=0
1 parent 8ba5fb3 commit ceab13c

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### 🚀 New
66

77
* Added `/notify/<up|down>` to the monitor server to manually trigger notifications.
8+
* Allow not starting the monitor loop by setting `LVMBEAT_START_MONITOR=0`. In this case notifications need to be sent manually by calling the `/notify` route.
89

910
### ✨ Improved
1011

src/lvmbeat/monitor.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,35 @@
2828

2929
logger = logging.getLogger("uvicorn.error")
3030

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+
3151
MAX_TIME_TO_ALERT: float = float(
3252
os.getenv(
3353
"LVMBEAT_SEND_EMAIL_AFTER",
3454
config["outside_monitor.send_email_after"],
3555
)
3656
)
3757

58+
START_MONITOR: bool = parse_bool_envvar("LVMBEAT_START_MONITOR", True)
59+
3860

3961
@dataclass
4062
class EmailSettings:
@@ -174,34 +196,36 @@ def send_email(message: str, subject: str):
174196
)
175197

176198

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:
184200

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."""
186208

187-
if not app.state.enabled:
188-
logger.debug("Heartbeat monitor is disabled.")
189-
return
209+
logger.debug("Checking heartbeat.")
190210

191-
now = time.time()
211+
if not app.state.enabled:
212+
logger.debug("Heartbeat monitor is disabled.")
213+
return
192214

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()
197216

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()
200221

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()
203227

204-
update_state_file(app.state)
228+
update_state_file(app.state)
205229

206230

207231
async def send_internet_down_notification():

0 commit comments

Comments
 (0)