💨 Sped up shutdown

 Check every minute for changes
This commit is contained in:
Timothy Miller 2021-03-11 20:34:05 -05:00
parent d3cc054b03
commit 55b705072a

View File

@ -1,4 +1,4 @@
import requests, json, sys, signal, os, time import requests, json, sys, signal, os, time, threading
PATH = os.getcwd() + "/" PATH = os.getcwd() + "/"
version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1])) version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
@ -9,19 +9,15 @@ if(version < 3.5):
raise Exception("This script requires Python 3.5+") raise Exception("This script requires Python 3.5+")
class GracefulExit: class GracefulExit:
kill_now = False
signals = {
signal.SIGINT: 'SIGINT',
signal.SIGTERM: 'SIGTERM'
}
def __init__(self): def __init__(self):
self.kill_now = threading.Event()
signal.signal(signal.SIGINT, self.exit_gracefully) signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully) signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame): def exit_gracefully(self, signum, frame):
print("🛑 Stopping main thread...") print("🛑 Stopping main thread...")
self.kill_now = True self.kill_now.set()
with open(PATH + "config.json") as config_file: with open(PATH + "config.json") as config_file:
config = json.loads(config_file.read()) config = json.loads(config_file.read())
@ -65,17 +61,17 @@ def getIPs():
shown_ipv6_warning = True shown_ipv6_warning = True
print("😨 Warning: IPv6 not detected") print("😨 Warning: IPv6 not detected")
deleteEntries("AAAA") deleteEntries("AAAA")
ips = [] ips = {}
if(a is not None): if(a is not None):
ips.append({ ips["ipv4"] = {
"type": "A", "type": "A",
"ip": a "ip": a
}) }
if(aaaa is not None): if(aaaa is not None):
ips.append({ ips["ipv6"] = {
"type": "AAAA", "type": "AAAA",
"ip": aaaa "ip": aaaa
}) }
return ips return ips
def commitRecord(ip): def commitRecord(ip):
@ -151,22 +147,25 @@ def cf_api(endpoint, method, config, headers={}, data=False):
return response.json() return response.json()
def updateIPs(): def updateIPs(ips):
for ip in getIPs(): for ip in ips.values():
commitRecord(ip) commitRecord(ip)
if __name__ == '__main__': if __name__ == '__main__':
if(len(sys.argv) > 1): if(len(sys.argv) > 1):
if(sys.argv[1] == "--repeat"): if(sys.argv[1] == "--repeat"):
delay = 15*60 delay = 60
print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every 15 minutes") print("⏲️ Updating IPv4 (A) & IPv6 (AAAA) records every minute")
next_time = time.time() next_time = time.time()
killer = GracefulExit() killer = GracefulExit()
while not killer.kill_now: prev_ips = None
time.sleep(max(0, next_time - time.time())) while True:
updateIPs() if killer.kill_now.wait(delay):
next_time += (time.time() - next_time) // delay * delay + delay break
ips = getIPs()
if ips != prev_ips:
updateIPs(ips)
else: else:
print("😡 Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") print("😡 Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
else: else:
updateIPs() updateIPs(getIPs())