handle sigterm and shutdown immediately

This commit is contained in:
Mark Ormesher 2021-01-20 18:50:36 +00:00
parent 839ffe2551
commit d763be7931
No known key found for this signature in database
GPG Key ID: DE308192D728C145

View File

@ -1,4 +1,4 @@
import requests, json, sys, os
import requests, json, sys, signal, os
import time
PATH = os.getcwd() + "/"
@ -7,6 +7,12 @@ version = float(str(sys.version_info[0]) + "." + str(sys.version_info[1]))
if(version < 3.5):
raise Exception("This script requires Python 3.5+")
def sigtermHandler(sig_no, stack_frame):
print("Caught SIGTERM, shutting down...")
sys.exit(0)
signal.signal(signal.SIGTERM, sigtermHandler)
with open(PATH + "config.json") as config_file:
config = json.loads(config_file.read())
@ -126,7 +132,8 @@ def updateIPs():
for ip in getIPs():
commitRecord(ip)
if(len(sys.argv) > 1):
try:
if(len(sys.argv) > 1):
if(sys.argv[1] == "--repeat"):
print("Updating A & AAAA records every 10 minutes")
updateIPs()
@ -138,5 +145,7 @@ if(len(sys.argv) > 1):
next_time += (time.time() - next_time) // delay * delay + delay
else:
print("Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.")
else:
else:
updateIPs()
except SystemExit:
print("Goodbye!")