From d763be7931c956eab2ebf56cb8f2a51fb035e0ef Mon Sep 17 00:00:00 2001 From: Mark Ormesher Date: Wed, 20 Jan 2021 18:50:36 +0000 Subject: [PATCH] handle sigterm and shutdown immediately --- cloudflare-ddns.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 5bf0abd..0b13a95 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -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()) @@ -67,11 +73,11 @@ def commitRecord(ip): } list = cf_api( "zones/" + c['zone_id'] + "/dns_records?per_page=100&type=" + ip["type"], "GET", c) - + full_subdomain = base_domain_name if subdomain: full_subdomain = subdomain + "." + full_subdomain - + dns_id = "" for r in list["result"]: if (r["name"] == full_subdomain): @@ -110,7 +116,7 @@ def cf_api(endpoint, method, config, headers={}, data=False): else: headers = { "X-Auth-Email": config['authentication']['api_key']['account_email'], - "X-Auth-Key": config['authentication']['api_key']['api_key'], + "X-Auth-Key": config['authentication']['api_key']['api_key'], } if(data == False): @@ -126,17 +132,20 @@ def updateIPs(): for ip in getIPs(): commitRecord(ip) -if(len(sys.argv) > 1): - if(sys.argv[1] == "--repeat"): - print("Updating A & AAAA records every 10 minutes") - updateIPs() - delay = 10*60 # 10 minutes - next_time = time.time() + delay - while True: - time.sleep(max(0, next_time - time.time())) +try: + if(len(sys.argv) > 1): + if(sys.argv[1] == "--repeat"): + print("Updating A & AAAA records every 10 minutes") updateIPs() - next_time += (time.time() - next_time) // delay * delay + delay + delay = 10*60 # 10 minutes + next_time = time.time() + delay + while True: + time.sleep(max(0, next_time - time.time())) + updateIPs() + next_time += (time.time() - next_time) // delay * delay + delay + else: + print("Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") else: - print("Unrecognized parameter '" + sys.argv[1] + "'. Stopping now.") -else: - updateIPs() + updateIPs() +except SystemExit: + print("Goodbye!")