Fixed packager

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-04-10 01:41:43 +02:00
parent aa44873891
commit 985c2ed5fc
2 changed files with 45 additions and 20 deletions

View file

@ -120,7 +120,11 @@ def api_send_status(name, status, api, token):
success = False success = False
while not success: while not success:
try: 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: if response.ok:
success = response.json()["success"] success = response.json()["success"]
if not success: if not success:
@ -162,24 +166,45 @@ def get_job(action, host, port, none_exit=True):
# Gets new job from manager api # Gets new job from manager api
def api_get_job(action, api, token, none_exit=True): def api_get_job(action, api, token, none_exit=True):
try: 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"}) # TODO 'if' is temporary until the database is reworked
if response.ok and response.json()["success"] == True: if action == "done":
match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"])) response = requests.post(api, data={'auth': token, 'action': 'get-done'}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"})
if match != None: if response.ok and response.json()["success"] == True:
ret = match.group(0) match = re.match(r"[ew]\d{3}[ns]\d{2}|None", str(response.json()["job"]))
if ret == "None" and none_exit: if match != None:
print("No job got asigned. Exiting...") ret = match.group(0)
sys.exit(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: else:
print("Recived invalid job. Retrying in 10 seconds...") print("Unable to get job. Retrying in 60 seconds...")
sleep(10) sleep(60)
ret = api_get_job(action, api, token, none_exit) ret = api_get_job(action, api, token, none_exit)
return ret return ret
else: else:
print("Unable to get job. Retrying in 60 seconds...") response = requests.post(api, data={'auth': token, 'action': 'get-job', 'additional-type': action}, headers={"lAccept": "application/json", "Content-Type": "application/x-www-form-urlencoded"})
sleep(60) if response.ok and response.json()["success"] == True:
ret = api_get_job(action, api, token, none_exit) match = re.match(r"[ew]\d{3}[ns]\d{2}|[0-9]{1,7}|None", str(response.json()["job"]))
return ret 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): except (ConnectionError, OSError, IOError):
print("Unable to get job. Retrying in 60 seconds...") print("Unable to get job. Retrying in 60 seconds...")
sleep(60) sleep(60)

View file

@ -23,7 +23,7 @@ import socket
from subprocess import run, Popen, STDOUT from subprocess import run, Popen, STDOUT
from time import sleep 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() host = socket.gethostname()
port = 12345 port = 12345
@ -149,7 +149,7 @@ while running:
if api == None: if api == None:
name = get_job("done", host, port, none_exit=False) name = get_job("done", host, port, none_exit=False)
else: 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": if name == "None":
print("No job got asigned. Trying again in one hour.") print("No job got asigned. Trying again in one hour.")
sleep(3600) sleep(3600)
@ -192,7 +192,7 @@ while running:
if api == None: if api == None:
send_status(name, "packaged", host, port) send_status(name, "packaged", host, port)
else: else:
api_send_status(name, "packaged", api, token) api_send_status(name, "packaged", api, api_token)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again") print("Graceful shutdown triggered. To force immedate stop, press Ctrl+C again")
@ -203,7 +203,7 @@ while running:
if api == None: if api == None:
send_status(name, "packaged", host, port) send_status(name, "packaged", host, port)
else: else:
api_send_status(name, "packaged", api, token) api_send_status(name, "packaged", api, api_token)
except KeyboardInterrupt: except KeyboardInterrupt:
# TODO doesn't work # TODO doesn't work
print("Forcing shutdown...") print("Forcing shutdown...")