diff --git a/common.py b/common.py index 9e64092..614f567 100644 --- a/common.py +++ b/common.py @@ -120,7 +120,11 @@ def api_send_status(name, status, api, token): success = False while not success: try: - response = requests.post(api, data={'auth': token, 'action': 'set', 'tile': name, 'status': status}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) + match = re.match(r"[0-9]{1,7}", name) + if match != None: + response = requests.post(api, data={'auth': token, 'action': 'set', 'tile': name, 'status': status}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) + else: + response = requests.post(api, data={'auth': token, 'action': 'set', 'area': name, 'status': status}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) if response.ok: success = response.json()["success"] if not success: @@ -162,24 +166,45 @@ def get_job(action, host, port, none_exit=True): # Gets new job from manager api def api_get_job(action, api, token, none_exit=True): try: - response = requests.post(api, data={'auth': token, 'action': 'get-job', 'additional-type': action}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) - if response.ok and response.json()["success"] == True: - match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"])) - if match != None: - ret = match.group(0) - if ret == "None" and none_exit: - print("No job got asigned. Exiting...") - sys.exit(0) + # TODO 'if' is temporary until the database is reworked + if action == "done": + response = requests.post(api, data={'auth': token, 'action': 'get-done'}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) + if response.ok and response.json()["success"] == True: + match = re.match(r"[ew]\d{3}[ns]\d{2}|None", str(response.json()["job"])) + if match != None: + ret = match.group(0) + if ret == "None" and none_exit: + print("No job got asigned. Exiting...") + sys.exit(0) + else: + print("Recived invalid job. Retrying in 10 seconds...") + sleep(10) + ret = api_get_job(action, api, token, none_exit) + return ret else: - print("Recived invalid job. Retrying in 10 seconds...") - sleep(10) + print("Unable to get job. Retrying in 60 seconds...") + sleep(60) ret = api_get_job(action, api, token, none_exit) - return ret + return ret else: - print("Unable to get job. Retrying in 60 seconds...") - sleep(60) - ret = api_get_job(action, api, token, none_exit) - return ret + response = requests.post(api, data={'auth': token, 'action': 'get-job', 'additional-type': action}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}) + if response.ok and response.json()["success"] == True: + match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"])) + if match != None: + ret = match.group(0) + if ret == "None" and none_exit: + print("No job got asigned. Exiting...") + sys.exit(0) + else: + print("Recived invalid job. Retrying in 10 seconds...") + sleep(10) + ret = api_get_job(action, api, token, none_exit) + return ret + else: + print("Unable to get job. Retrying in 60 seconds...") + sleep(60) + ret = api_get_job(action, api, token, none_exit) + return ret except (ConnectionError, OSError, IOError): print("Unable to get job. Retrying in 60 seconds...") sleep(60) diff --git a/worldbuild-packager.py b/worldbuild-packager.py index 20027fa..c0e26f2 100755 --- a/worldbuild-packager.py +++ b/worldbuild-packager.py @@ -23,7 +23,7 @@ import socket from subprocess import run, Popen, STDOUT from time import sleep -from common import norm, send_status, get_job +from common import norm, send_status, get_job, api_get_job, api_send_status host = socket.gethostname() port = 12345 @@ -149,7 +149,7 @@ while running: if api == None: name = get_job("done", host, port, none_exit=False) else: - name = api_get_job("done", api, token, none_exit=False) + name = api_get_job("done", api, api_token, none_exit=False) if name == "None": print("No job got asigned. Trying again in one hour.") sleep(3600) @@ -192,7 +192,7 @@ while running: if api == None: send_status(name, "packaged", host, port) else: - api_send_status(name, "packaged", api, token) + api_send_status(name, "packaged", api, api_token) except KeyboardInterrupt: print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again") @@ -203,7 +203,7 @@ while running: if api == None: send_status(name, "packaged", host, port) else: - api_send_status(name, "packaged", api, token) + api_send_status(name, "packaged", api, api_token) except KeyboardInterrupt: # TODO doesn't work print("Forcing shutdown...")