feat(worker): add configurable debug logging

This commit is contained in:
2026-06-21 02:04:19 +03:30
parent 7a2c26a4e6
commit 201196ffb0
10 changed files with 122 additions and 7 deletions

View File

@@ -27,12 +27,14 @@ class JobWorkerRunner:
self.stop_event.set()
def start(self):
logger.info("Starting job worker runner with %s thread(s).", self.thread_count)
for index in range(self.thread_count):
thread = threading.Thread(target=self._run_thread, args=(index,), name=f"job-worker-{index + 1}")
thread.start()
self.threads.append(thread)
def wait(self):
logger.debug("Waiting for worker threads to stop.")
while any(thread.is_alive() for thread in self.threads):
for thread in self.threads:
thread.join(timeout=0.25)
@@ -54,12 +56,17 @@ class JobWorkerRunner:
now = time.monotonic()
if now - last_cleanup >= settings.JOB_WORKER_CLEANUP_INTERVAL_SECONDS:
try:
cleanup_expired_jobs(batch_size=settings.JOB_WORKER_CLEANUP_BATCH_SIZE)
cleaned = cleanup_expired_jobs(batch_size=settings.JOB_WORKER_CLEANUP_BATCH_SIZE)
if cleaned:
logger.info("Cleaned up %s expired job lease(s).", cleaned)
else:
logger.debug("Expired job cleanup found no work.")
except Exception:
logger.exception("Expired job cleanup failed.")
last_cleanup = now
try:
logger.debug("Polling for the next queued job.")
job = claim_next_job(worker_id=worker_id, lease_seconds=settings.JOB_WORKER_LEASE_SECONDS)
except Exception:
logger.exception("Job claim failed.")
@@ -67,6 +74,7 @@ class JobWorkerRunner:
continue
if job is None:
logger.debug("No eligible job found; sleeping %.3f second(s).", poll_seconds)
self.stop_event.wait(poll_seconds)
continue
@@ -77,14 +85,20 @@ class JobWorkerRunner:
def _execute_claimed_job(self, job, *, worker_id: str):
attempt = job.attempts
logger.info("Executing job %s type=%s attempt=%s worker=%s.", job.id, job.type, attempt, worker_id)
try:
result = execute_handler(job, worker_id=worker_id)
except Exception as exc:
logger.info("Job %s attempt=%s raised error: %s", job.id, attempt, exc)
updated_job = fail_job(job.id, worker_id=worker_id, attempt=attempt, error=str(exc))
if updated_job is None:
logger.info("Worker lost ownership before failing job %s attempt %s.", job.id, attempt)
logger.warning("Worker lost ownership before failing job %s attempt %s.", job.id, attempt)
else:
logger.debug("Failure handling persisted job %s with status=%s.", updated_job.id, updated_job.status)
return
updated_job = complete_job(job.id, worker_id=worker_id, attempt=attempt, result=result)
if updated_job is None:
logger.info("Worker lost ownership before completing job %s attempt %s.", job.id, attempt)
logger.warning("Worker lost ownership before completing job %s attempt %s.", job.id, attempt)
else:
logger.info("Completed job %s attempt=%s worker=%s.", updated_job.id, attempt, worker_id)