make last state persistent, fix exceptions

This commit is contained in:
bain 2021-11-28 00:26:55 +01:00
parent b92cf90a0b
commit b66b63e6bd
No known key found for this signature in database
GPG key ID: A708F07AF3D92C02

View file

@ -4,6 +4,8 @@ import time
import logging
import datetime
import socket
import json
import os
import requests
import pydig
@ -62,7 +64,7 @@ def self_check():
try:
if requests.get("https://google.com/").status_code != 200:
return False
except ConnectionError:
except requests.exceptions.ConnectionError:
return False
return True
@ -72,7 +74,7 @@ def http_requirement(url: str, code: int) -> bool:
for i in range(2):
try:
resp = requests.get(url)
except ConnectionError:
except requests.exceptions.ConnectionError:
passed = False
else:
passed = resp.status_code == code
@ -140,4 +142,11 @@ monitors = {
if __name__ == '__main__':
# we assume this is gonna be run in a cron job as the gitpython
# library is slowly leaking memory apparently
if os.path.exists("last-state"):
with open("last-state", 'r') as f:
last_states = json.load(f)
check(monitors)
with open("last-state", 'w+') as f:
json.dump(last_states, f)