finish up and chain it all together - v0.0.1 🎉

This commit is contained in:
askiiart 2024-02-01 12:36:38 -06:00
parent 53eaee5971
commit 9972957176
Signed by untrusted user who does not match committer: askiiart
GPG key ID: BC3800E55FB54D67
5 changed files with 42 additions and 7 deletions

16
.vscode/launch.json vendored Normal file
View file

@ -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
}
]
}

View file

@ -32,4 +32,4 @@ There are none yet, so consider this secret a placeholder
### Alerts
## Logging
### Logging

View file

@ -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
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)

View file

@ -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",

View file

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