Cleanup: Rely on top-level exception handling

This commit is contained in:
Reid 'arrdem' McKenzie 2023-07-08 19:55:54 -06:00
parent b9a2ae4274
commit c6ba7bde1f

View file

@ -80,7 +80,7 @@ def poll_printers(app: App, db: Db) -> None:
): ):
return True # Assume the bed is ready return True # Assume the bed is ready
status = client._post( status: dict = client._post(
"/api/plugin/bedready", "/api/plugin/bedready",
json={ json={
"command": "check_bed", "command": "check_bed",
@ -225,25 +225,18 @@ def revoke_jobs(app: App, db: Db) -> None:
if job.printer_id: if job.printer_id:
printer = db.fetch_printer(pid=job.printer_id) printer = db.fetch_printer(pid=job.printer_id)
log.info(f"Cancelling running job {job.id}")
client = get_client(printer.url, printer.api_key)
try: try:
log.info(f"Cancelling running job {job.id}") client.cancel()
client = get_client(printer.url, printer.api_key) except HTTPError as e:
try: if e.response.status_code == 409:
client.cancel() pass
except HTTPError as e: else:
if e.response.status_code == 409: raise
pass
else:
raise
log.info(f"Job {job.id} -> cancelled") log.info(f"Job {job.id} -> cancelled")
db.finish_job(jid=job.id, state="cancelled") db.finish_job(jid=job.id, state="cancelled")
except TimeoutError:
pass
except Exception:
log.exception("Oop")
else: else:
log.info(f"Unmapped job {job.id} became cancelled") log.info(f"Unmapped job {job.id} became cancelled")
@ -255,40 +248,33 @@ def pull_jobs(app: App, db: Db) -> None:
for job in db.list_running_jobs(): for job in db.list_running_jobs():
printer = db.fetch_printer(pid=job.printer_id) printer = db.fetch_printer(pid=job.printer_id)
client = get_client(printer.url, printer.api_key)
job_state = client.job_info()
try: try:
client = get_client(printer.url, printer.api_key) printer_state = client.printer().get("state").get("flags", {})
job_state = client.job_info() except HTTPError:
try: printer_state = {"disconnected": True, "error": True}
printer_state = client.printer().get("state").get("flags", {})
except HTTPError:
printer_state = {"disconnected": True, "error": True}
if job_state.get("progress", {}).get("completion", 0.0) == 100.0: if job_state.get("progress", {}).get("completion", 0.0) == 100.0:
log.info(f"Job {job.id} has succeeded") log.info(f"Job {job.id} has succeeded")
db.finish_job(jid=job.id, state="success") db.finish_job(jid=job.id, state="success")
elif printer_state.get("error"): elif printer_state.get("error"):
log.warn(f"Job {job.id} has failed") log.warn(f"Job {job.id} has failed")
db.finish_job(jid=job.id, state="failed") db.finish_job(jid=job.id, state="failed")
elif printer_state.get("cancelling"): elif printer_state.get("cancelling"):
log.info(f"Job {job.id} has been acknowledged as cancelled") log.info(f"Job {job.id} has been acknowledged as cancelled")
db.finish_job(jid=job.id, state="cancelled") db.finish_job(jid=job.id, state="cancelled")
elif printer_state.get("printing"): elif printer_state.get("printing"):
db.start_job(jid=job.id) db.start_job(jid=job.id)
else: else:
log.warn( log.warn(
f"Job {job.id} is in a weird state; assuming error\n{pformat(job_state.get('progress'))}\n{pformat(printer_state)}" f"Job {job.id} is in a weird state; assuming error\n{pformat(job_state.get('progress'))}\n{pformat(printer_state)}"
) )
db.finish_job(jid=job.id, state="failed") db.finish_job(jid=job.id, state="failed")
except TimeoutError:
pass
except Exception:
log.exception("Oop")
def send_emails(app, db: Db): def send_emails(app, db: Db):
@ -395,5 +381,8 @@ class Worker(Monitor):
) )
def callback(self): def callback(self):
with closing(self._db_factory(self._app)) as db: try:
self._callback(self._app, db) with closing(self._db_factory(self._app)) as db:
self._callback(self._app, db)
except:
log.exception("Worker tick failed; exception swallowed")