1
1
from datetime import datetime , timedelta
2
2
from pyres import ResQ , str_to_class , safe_str_to_class
3
3
from pyres import failure
4
+ from pyres .failure .redis import RedisBackend
4
5
5
6
class Job (object ):
6
7
"""Every job on the ResQ is an instance of the *Job* class.
7
-
8
+
8
9
The ``__init__`` takes these keyword arguments:
9
-
10
+
10
11
``queue`` -- A string defining the queue to which this Job will be added.
11
-
12
+
12
13
``payload`` -- A dictionary which contains the string name of a class which extends this Job and
13
14
a list of args which will be passed to that class.
14
-
15
+
15
16
``resq`` -- An instance of the ResQ class.
16
-
17
+
17
18
``worker`` -- The name of a specific worker if you'd like this Job to be done by that worker. Default is "None".
18
-
19
+
19
20
"""
20
21
def __init__ (self , queue , payload , resq , worker = None ):
21
22
self ._queue = queue
22
23
self ._payload = payload
23
24
self .resq = resq
24
25
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
+
26
31
def __str__ (self ):
27
32
return "(Job{%s} | %s | %s)" % (
28
33
self ._queue , self ._payload ['class' ], repr (self ._payload ['args' ]))
29
-
34
+
30
35
def perform (self ):
31
36
"""This method converts payload into args and calls the ``perform`` method
32
37
on the payload class.
33
-
38
+
34
39
"""
35
40
payload_class_str = self ._payload ["class" ]
36
41
payload_class = safe_str_to_class (payload_class_str )
@@ -41,11 +46,11 @@ def perform(self):
41
46
except :
42
47
if not self .retry (payload_class , args ):
43
48
raise
44
-
49
+
45
50
def fail (self , exception ):
46
51
"""This method provides a way to fail a job and will use whatever failure backend
47
52
you've provided. The default is the ``RedisBackend``.
48
-
53
+
49
54
"""
50
55
fail = failure .create (exception , self ._queue , self ._payload , self ._worker )
51
56
fail .save (self .resq )
@@ -70,7 +75,7 @@ def retry(self, payload_class, args):
70
75
def reserve (cls , queue , res , worker = None ):
71
76
"""Reserve a job on the queue. This marks this job so that other workers
72
77
will not pick it up.
73
-
78
+
74
79
"""
75
80
payload = res .pop (queue )
76
81
if payload :
0 commit comments