Skip to content

Commit fea83d2

Browse files
committed
Ensure that failure backends are set correctly on job creation.
1 parent 9440091 commit fea83d2

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

pyres/job.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
from datetime import datetime, timedelta
22
from pyres import ResQ, str_to_class, safe_str_to_class
33
from pyres import failure
4+
from pyres.failure.redis import RedisBackend
45

56
class Job(object):
67
"""Every job on the ResQ is an instance of the *Job* class.
7-
8+
89
The ``__init__`` takes these keyword arguments:
9-
10+
1011
``queue`` -- A string defining the queue to which this Job will be added.
11-
12+
1213
``payload`` -- A dictionary which contains the string name of a class which extends this Job and
1314
a list of args which will be passed to that class.
14-
15+
1516
``resq`` -- An instance of the ResQ class.
16-
17+
1718
``worker`` -- The name of a specific worker if you'd like this Job to be done by that worker. Default is "None".
18-
19+
1920
"""
2021
def __init__(self, queue, payload, resq, worker=None):
2122
self._queue = queue
2223
self._payload = payload
2324
self.resq = resq
2425
self._worker = worker
25-
26+
27+
# Set the default back end, jobs can override when we import them
28+
# inside perform().
29+
failure.backend = RedisBackend
30+
2631
def __str__(self):
2732
return "(Job{%s} | %s | %s)" % (
2833
self._queue, self._payload['class'], repr(self._payload['args']))
29-
34+
3035
def perform(self):
3136
"""This method converts payload into args and calls the ``perform`` method
3237
on the payload class.
33-
38+
3439
"""
3540
payload_class_str = self._payload["class"]
3641
payload_class = safe_str_to_class(payload_class_str)
@@ -41,11 +46,11 @@ def perform(self):
4146
except:
4247
if not self.retry(payload_class, args):
4348
raise
44-
49+
4550
def fail(self, exception):
4651
"""This method provides a way to fail a job and will use whatever failure backend
4752
you've provided. The default is the ``RedisBackend``.
48-
53+
4954
"""
5055
fail = failure.create(exception, self._queue, self._payload, self._worker)
5156
fail.save(self.resq)
@@ -70,7 +75,7 @@ def retry(self, payload_class, args):
7075
def reserve(cls, queue, res, worker=None):
7176
"""Reserve a job on the queue. This marks this job so that other workers
7277
will not pick it up.
73-
78+
7479
"""
7580
payload = res.pop(queue)
7681
if payload:

0 commit comments

Comments
 (0)