6
6
* @author Chris Boulton <[email protected] >
7
7
* @license http://www.opensource.org/licenses/mit-license.php
8
8
*/
9
- class Resque_Job
9
+ class Resque_Job implements Resque_JobInterface
10
10
{
11
11
/**
12
12
* @var string The name of the queue that this job belongs to.
@@ -24,10 +24,15 @@ class Resque_Job
24
24
public $ payload ;
25
25
26
26
/**
27
- * @var object Instance of the class performing work for this job.
27
+ * @var Resque_JobInterface Instance of the class performing work for this job.
28
28
*/
29
29
private $ instance ;
30
30
31
+ /**
32
+ * @var Resque_Job_FactoryInterface
33
+ */
34
+ private $ jobFactory ;
35
+
31
36
/**
32
37
* Instantiate a new instance of a job.
33
38
*
@@ -40,17 +45,18 @@ public function __construct($queue, $payload)
40
45
$ this ->payload = $ payload ;
41
46
}
42
47
43
- /**
44
- * Create a new job and save it to the specified queue.
45
- *
46
- * @param string $queue The name of the queue to place the job in.
47
- * @param string $class The name of the class that contains the code to execute the job.
48
- * @param array $args Any optional arguments that should be passed when the job is executed.
49
- * @param boolean $monitor Set to true to be able to monitor the status of a job.
50
- * @param string $id Unique identifier for tracking the job. Generated if not supplied.
51
- *
52
- * @return string
53
- */
48
+ /**
49
+ * Create a new job and save it to the specified queue.
50
+ *
51
+ * @param string $queue The name of the queue to place the job in.
52
+ * @param string $class The name of the class that contains the code to execute the job.
53
+ * @param array $args Any optional arguments that should be passed when the job is executed.
54
+ * @param boolean $monitor Set to true to be able to monitor the status of a job.
55
+ * @param string $id Unique identifier for tracking the job. Generated if not supplied.
56
+ *
57
+ * @return string
58
+ * @throws \InvalidArgumentException
59
+ */
54
60
public static function create ($ queue , $ class , $ args = null , $ monitor = false , $ id = null )
55
61
{
56
62
if (is_null ($ id )) {
@@ -81,7 +87,7 @@ public static function create($queue, $class, $args = null, $monitor = false, $i
81
87
* instance of Resque_Job for it.
82
88
*
83
89
* @param string $queue The name of the queue to check for a job in.
84
- * @return null |object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
90
+ * @return false |object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
85
91
*/
86
92
public static function reserve ($ queue )
87
93
{
@@ -99,7 +105,7 @@ public static function reserve($queue)
99
105
*
100
106
* @param array $queues
101
107
* @param int $timeout
102
- * @return null |object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
108
+ * @return false |object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
103
109
*/
104
110
public static function reserveBlocking (array $ queues , $ timeout = null )
105
111
{
@@ -152,11 +158,11 @@ public function getArguments()
152
158
return $ this ->payload ['args ' ][0 ];
153
159
}
154
160
155
- /**
156
- * Get the instantiated object for this job that will be performing work.
157
- *
158
- * @return object Instance of the object that this job belongs to.
159
- */
161
+ /**
162
+ * Get the instantiated object for this job that will be performing work.
163
+ * @return Resque_JobInterface Instance of the object that this job belongs to.
164
+ * @throws Resque_Exception
165
+ */
160
166
public function getInstance ()
161
167
{
162
168
if (!is_null ($ this ->instance )) {
@@ -175,7 +181,11 @@ public function getInstance()
175
181
);
176
182
}
177
183
178
- $ this ->instance = new $ this ->payload ['class ' ];
184
+ if ($ this ->jobFactory !== null ) {
185
+ $ this ->instance = $ this ->jobFactory ->create ();
186
+ } else {
187
+ $ this ->instance = new $ this ->payload ['class ' ];
188
+ }
179
189
$ this ->instance ->job = $ this ;
180
190
$ this ->instance ->args = $ this ->getArguments ();
181
191
$ this ->instance ->queue = $ this ->queue ;
@@ -272,4 +282,15 @@ public function __toString()
272
282
}
273
283
return '( ' . implode (' | ' , $ name ) . ') ' ;
274
284
}
285
+
286
+ /**
287
+ * @param Resque_Job_FactoryInterface $jobFactory
288
+ * @return Resque_Job
289
+ */
290
+ public function setJobFactory (Resque_Job_FactoryInterface $ jobFactory )
291
+ {
292
+ $ this ->jobFactory = $ jobFactory ;
293
+
294
+ return $ this ;
295
+ }
275
296
}
0 commit comments