Skip to content

Commit 76ae08d

Browse files
committed
Increment retry_attempt on jobs that fail to retry
1 parent 536258c commit 76ae08d

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

plain-worker/plain/worker/models.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,15 @@ def retryable(self):
260260

261261
def retry_failed_jobs(self):
262262
for result in self.retryable():
263-
result.retry_job()
263+
try:
264+
result.retry_job()
265+
except Exception:
266+
# If something went wrong (like a job class being deleted)
267+
# then we immediately increment the retry_attempt on the existing obj
268+
# so it won't retry forever.
269+
logger.exception()
270+
result.retry_attempt += 1
271+
result.save(update_fields=["retry_attempt"])
264272

265273

266274
class JobResultStatuses(models.TextChoices):
@@ -331,9 +339,6 @@ def retry_job(self, delay: int | None = None):
331339
job = jobs_registry.load_job(self.job_class, self.parameters)
332340
retry_delay = delay or job.get_retry_delay(retry_attempt)
333341

334-
# TODO a job class could have been deleted, and it would fail to load.
335-
# What do we do then? Increment the retry attempt and leave it in the db?
336-
337342
with transaction.atomic():
338343
result = job.run_in_worker(
339344
# Pass most of what we know through so it stays consistent
@@ -345,15 +350,11 @@ def retry_job(self, delay: int | None = None):
345350
# Unique key could be passed also?
346351
)
347352

348-
# It's possible this could return a list of pending
349-
# jobs, so we need to check if we actually created a new job
350-
if isinstance(result, JobRequest):
351-
# We need to know the retry request for this result
352-
self.retry_job_request_uuid = result.uuid
353-
self.save(update_fields=["retry_job_request_uuid"])
354-
else:
355-
# What to do in this situation? Will continue to run the retry
356-
# logic until it successfully retries or it is deleted.
357-
pass
353+
# TODO it is actually possible that result is a list
354+
# of pending jobs, which would need to be handled...
355+
# Right now it will throw an exception which could be caught by retry_failed_jobs.
356+
357+
self.retry_job_request_uuid = result.uuid
358+
self.save(update_fields=["retry_job_request_uuid"])
358359

359360
return result

0 commit comments

Comments
 (0)