From eaf598d73ce234791ee22ef82f7f095aece9f2a6 Mon Sep 17 00:00:00 2001 From: John Stowers Date: Sat, 27 Oct 2018 06:42:31 +0200 Subject: [PATCH] Pass job_id to death penalty class (#936) This allows custom workers to use associated custom Timeout classes and apply custom timeouts or less messy death methods --- docs/docs/workers.md | 10 ++++++++++ rq/timeouts.py | 2 +- rq/worker.py | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/docs/workers.md b/docs/docs/workers.md index 414d89c..e3d205e 100644 --- a/docs/docs/workers.md +++ b/docs/docs/workers.md @@ -290,6 +290,16 @@ queue.enqueue(some_func) {% endhighlight %} +## Custom DeathPenalty classes + +When a Job times-out, the worker will try to kill it using the supplied +`death_penalty_class` (default: `UnixSignalDeathPenalty`). This can be overridden +if you wish to attempt to kill jobs in an application specific or 'cleaner' manner. + +DeathPenalty classes are constructed with the following arguments +`BaseDeathPenalty(timeout, JobTimeoutException, job_id=job.id)` + + ## Custom exception handlers _New in version 0.5.5._ diff --git a/rq/timeouts.py b/rq/timeouts.py index b211097..3f9ac5b 100644 --- a/rq/timeouts.py +++ b/rq/timeouts.py @@ -27,7 +27,7 @@ class HorseMonitorTimeoutException(BaseTimeoutException): class BaseDeathPenalty(object): """Base class to setup job timeouts.""" - def __init__(self, timeout, exception=JobTimeoutException): + def __init__(self, timeout, exception=JobTimeoutException, **kwargs): self._timeout = timeout self._exception = exception diff --git a/rq/worker.py b/rq/worker.py index a05a6ba..cdfa33f 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -795,7 +795,7 @@ class Worker(object): try: job.started_at = utcnow() timeout = job.timeout or self.queue_class.DEFAULT_TIMEOUT - with self.death_penalty_class(timeout, JobTimeoutException): + with self.death_penalty_class(timeout, JobTimeoutException, job_id=job.id): rv = job.perform() job.ended_at = utcnow()