mirror of
https://forgejo.altau.su/lego/lego-monitoring.git
synced 2026-03-10 04:41:10 +00:00
some actual alerts and telegram client
This commit is contained in:
parent
f158bc3778
commit
ffdd0429b3
10 changed files with 314 additions and 33 deletions
|
|
@ -1,19 +1,65 @@
|
|||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import signal
|
||||
import time
|
||||
|
||||
from .alerting import alerts
|
||||
from .core import cvars
|
||||
from .core.config import load_config
|
||||
|
||||
stopping = False
|
||||
|
||||
|
||||
def stop_gracefully(signum, frame):
|
||||
global stopping
|
||||
stopping = True
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
asyncio.run(async_main())
|
||||
|
||||
|
||||
async def async_main():
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="lego-monitoring",
|
||||
description="Lego-monitoring service",
|
||||
)
|
||||
parser.add_argument('-c', '--config', required=True)
|
||||
parser.add_argument("-c", "--config", required=True)
|
||||
|
||||
config_path = parser.parse_args().config
|
||||
config = load_config(config_path)
|
||||
cvars.config.set(config)
|
||||
|
||||
while True:
|
||||
print(f"service running... opt 1 is {config.non_secret_config_option}, opt 2 is secret, but if you really wanna know, it's {config.config_option}", flush=True)
|
||||
time.sleep(300)
|
||||
tg_client = await alerts.get_client()
|
||||
cvars.tg_client.set(tg_client)
|
||||
|
||||
checker_sets = {
|
||||
"start": [
|
||||
alerts.send_start_alert(),
|
||||
],
|
||||
"stop": [], # this is checked later
|
||||
}
|
||||
|
||||
checkers = []
|
||||
for enabled_set in config.enabled_checker_sets:
|
||||
for checker in checker_sets[enabled_set]:
|
||||
checkers.append(checker)
|
||||
|
||||
signal.signal(signal.SIGTERM, stop_gracefully)
|
||||
|
||||
async with asyncio.TaskGroup() as tg:
|
||||
checker_tasks: set[asyncio.Task] = set()
|
||||
for c in checkers:
|
||||
task = tg.create_task(c)
|
||||
checker_tasks.add(task)
|
||||
while True:
|
||||
if stopping:
|
||||
if "stop" in config.enabled_checker_sets:
|
||||
await alerts.send_stop_alert()
|
||||
await tg_client.disconnect()
|
||||
raise SystemExit
|
||||
else:
|
||||
await asyncio.sleep(3)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue