From 3d8031588676df9b9eb486a400817162ca5a92a3 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 19 Sep 2026 20:10:59 -0600 Subject: [PATCH] jobs: write the exception into the Job log on failure test_jobs_failure only saw 'boom' in the log when JobLogHandler captured logger.error(..., exc_info=e). Sanic/pytest dictConfig replaces root handlers (especially under xdist on CI), so that path is empty even though status and error are set. Log the exception on the Job itself and store it in the same _update_job as FAILED. --- wrolpi/jobs.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/wrolpi/jobs.py b/wrolpi/jobs.py index 44acb5c0..a2508240 100644 --- a/wrolpi/jobs.py +++ b/wrolpi/jobs.py @@ -300,8 +300,19 @@ class JobLogHandler(logging.Handler): _log_handler = JobLogHandler() _log_handler.setFormatter(logging.Formatter('%(levelname)s:%(name)s:%(message)s')) -if not any(isinstance(i, JobLogHandler) for i in logging.getLogger().handlers): - logging.getLogger().addHandler(_log_handler) + + +def _ensure_log_handler(): + """Sanic/pytest dictConfig replaces root handlers and would drop JobLogHandler. + + Attach to this module's logger (not only root) so capture survives that reset. + """ + for target in (logging.getLogger(), logger): + if not any(isinstance(i, JobLogHandler) for i in target.handlers): + target.addHandler(_log_handler) + + +_ensure_log_handler() def _json_safe(value): @@ -314,6 +325,7 @@ def _json_safe(value): async def _run_job(job_id: str): """Run one Job to completion (or failure/cancellation) in this process.""" + _ensure_log_handler() record = _claim_job(job_id) if not record: # Cancelled while it was waiting in the queue. @@ -360,8 +372,13 @@ async def _run_job(job_id: str): raise except Exception as e: logger.error(f'Job {job_id} failed', exc_info=e) + # Do not rely on JobLogHandler for this line: Sanic/pytest dictConfig can + # strip root handlers (especially under xdist on CI), so the traceback + # never reaches the Job log even though `error` is set. + context.log(f'{type(e).__name__}: {e}') context.flush() - _update_job(job_id, status=FAILED, error=str(e) or repr(e), finished_at=now().isoformat()) + _update_job(job_id, status=FAILED, error=str(e) or repr(e), + log=list(context._log), finished_at=now().isoformat()) Events.send_user_notify(f'Failed: {record["description"]}') return