From 9972957176d990baf1ff8f63ca66f08165364709 Mon Sep 17 00:00:00 2001 From: askiiart Date: Thu, 1 Feb 2024 12:36:38 -0600 Subject: [PATCH] =?UTF-8?q?finish=20up=20and=20chain=20it=20all=20together?= =?UTF-8?q?=20-=20v0.0.1=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 16 ++++++++++++++++ docs/extensions/README.md | 2 +- helpers.py | 16 ++++++++++++---- services-example.json | 2 +- updog.py | 13 ++++++++++++- 5 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8aaf1c3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "updog.py", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/docs/extensions/README.md b/docs/extensions/README.md index cf53621..9c8da45 100644 --- a/docs/extensions/README.md +++ b/docs/extensions/README.md @@ -32,4 +32,4 @@ There are none yet, so consider this secret a placeholder ### Alerts -## Logging \ No newline at end of file +### Logging \ No newline at end of file diff --git a/helpers.py b/helpers.py index 0aa8ddd..10c4294 100644 --- a/helpers.py +++ b/helpers.py @@ -6,6 +6,8 @@ import time # It shouldn't drift, but that's untested # Keep in mind you can't catch Exceptions for this, it just crashes the thread. # Some more scheduling stuff: https://www.redwood.com/article/python-job-scheduling/ + + class RepeatedTimer(object): ''' Run stuff repeatedly every x seconds @@ -13,13 +15,13 @@ class RepeatedTimer(object): from time import sleep def hello(name): - print "Hello %s!" % name + print(f'Hello {name}!') print()"starting...") rt = RepeatedTimer(1, hello, "World") # it auto-starts, no need of rt.start() try: sleep(5) # your long-running job goes here... - catch: + except: rt.stop() # better in a try/catch block to make sure the program ends! ''' @@ -53,6 +55,7 @@ class RepeatedTimer(object): self._timer.cancel() self.is_running = False + def create_instances(config, checkers, alerts, logging): ''' Creates instances of all the extensions according to config @@ -75,8 +78,13 @@ def create_instances(config, checkers, alerts, logging): return instances -def run_check(checker, alerts, logging): + +def run_check(service_name, checker, alerts, logging): ''' Does the logic and stuff for running the checker, calling alerts/logging, and so on ''' - pass \ No newline at end of file + status = checker.get_status() + return_codes = checker.get_return_codes() + status_message = return_codes[status] + logging.log(service_name, status, status_message) + alerts.alert(service_name, status, status_message) diff --git a/services-example.json b/services-example.json index f06059a..d0dfd1c 100644 --- a/services-example.json +++ b/services-example.json @@ -7,7 +7,7 @@ "port": 443, "lol": "CheckerTemplate ignores these options lol" }, - "rate": 60, + "rate": 1, "alerts": "AlertsTemplate", "alerts-args": { "url": "https://example.com/webhook-url-or-whatever-goes-here", diff --git a/updog.py b/updog.py index 979c500..2484411 100644 --- a/updog.py +++ b/updog.py @@ -73,4 +73,15 @@ elif 'services-example.json' in os.listdir(): with open(config_filename, 'rt') as config_file: config = json.loads(''.join(config_file.readlines())) -print(create_instances(config, checkers, alerts, logging)) \ No newline at end of file +instances = create_instances(config, checkers, alerts, logging) +timers = {} + +try: + for service in config.keys(): + timers[service] = RepeatedTimer(config[service]['rate'], run_check, + service, instances[service]['checker'], instances[service]['alerts'], instances[service]['logging']) +except: + # Stops all RepeatedTimers since they're threaded + # Doesn't actually stop the threads, just stops repeating it + for service in timers.keys(): + timers[service].stop()