Skip to content

Commit 7e03f6e

Browse files
authored
Add base Job class (#92)
* Add base Job class * Require every job to extend the base Job class * Add more strict types for Job class properties
1 parent ecfce08 commit 7e03f6e

File tree

11 files changed

+163
-94
lines changed

11 files changed

+163
-94
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Resque\Resque::enqueue('default', 'My_Job', $args);
110110
Each job should be in its own class, and include a `perform` method.
111111

112112
```php
113-
class My_Job
113+
class My_Job extends \Resque\Job\Job
114114
{
115115
public function perform()
116116
{
@@ -132,9 +132,9 @@ defined, it will be called before the `perform` method is run. The `tearDown`
132132
method, if defined, will be called after the job finishes.
133133

134134
```php
135-
class My_Job
135+
class My_Job extends \Resque\Job\Job
136136
{
137-
public function setUp()
137+
public function setUp(): void
138138
{
139139
// ... Set up environment for this job
140140
}
@@ -144,7 +144,7 @@ class My_Job
144144
// .. Run job
145145
}
146146

147-
public function tearDown()
147+
public function tearDown(): void
148148
{
149149
// ... Remove environment for this job
150150
}

lib/Job/Factory.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@ class Factory implements FactoryInterface
1010
* @param $className
1111
* @param array $args
1212
* @param $queue
13-
* @return \Resque\Job\JobInterface
13+
* @return \Resque\Job\Job
1414
* @throws \Resque\Exceptions\ResqueException
1515
*/
16-
public function create($className, $args, $queue)
16+
public function create(string $className, array $args, string $queue): Job
1717
{
1818
if (!class_exists($className)) {
1919
throw new ResqueException(
2020
'Could not find job class ' . $className . '.'
2121
);
2222
}
2323

24-
if (!method_exists($className, 'perform')) {
25-
throw new ResqueException(
26-
'Job class ' . $className . ' does not contain a perform method.'
27-
);
28-
}
29-
3024
$instance = new $className();
3125
$instance->args = $args;
3226
$instance->queue = $queue;

lib/Job/FactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface FactoryInterface
88
* @param $className
99
* @param array $args
1010
* @param $queue
11-
* @return \Resque\Job\JobInterface
11+
* @return \Resque\Job\Job
1212
*/
13-
public function create($className, $args, $queue);
13+
public function create(string $className, array $args, string $queue): Job;
1414
}

lib/Job/Job.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Resque\Job;
4+
5+
use Resque\JobHandler;
6+
7+
/**
8+
* Base Resque Job class.
9+
*
10+
* @package Resque\Job
11+
* @author Heinz Wiesinger <[email protected]>
12+
* @license http://www.opensource.org/licenses/mit-license.php
13+
*/
14+
abstract class Job
15+
{
16+
/**
17+
* Job arguments
18+
* @var array
19+
*/
20+
public $args;
21+
22+
/**
23+
* Associated JobHandler instance
24+
* @var JobHandler
25+
*/
26+
public $job;
27+
28+
/**
29+
* Name of the queue the job was in
30+
* @var string
31+
*/
32+
public $queue;
33+
34+
/**
35+
* (Optional) Job setup
36+
*
37+
* @return void
38+
*/
39+
public function setUp(): void
40+
{
41+
// no-op
42+
}
43+
44+
/**
45+
* (Optional) Job teardown
46+
*
47+
* @return void
48+
*/
49+
public function tearDown(): void
50+
{
51+
// no-op
52+
}
53+
54+
/**
55+
* Main method of the Job
56+
*
57+
* @return mixed|void
58+
*/
59+
abstract public function perform();
60+
}

lib/Job/JobInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/JobHandler.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Resque;
44

5-
use InvalidArgumentException;
65
use Resque\Job\PID;
76
use Resque\Job\Status;
87
use Resque\Exceptions\DoNotPerformException;
98
use Resque\Job\FactoryInterface;
109
use Resque\Job\Factory;
10+
use Resque\Job\Job;
1111
use Error;
1212

1313
/**
@@ -50,7 +50,7 @@ class JobHandler
5050
public $endTime;
5151

5252
/**
53-
* @var object|\Resque\Job\JobInterface Instance of the class performing work for this job.
53+
* @var Job Instance of the class performing work for this job.
5454
*/
5555
private $instance;
5656

@@ -83,19 +83,13 @@ public function __construct($queue, $payload)
8383
* @param string $prefix The prefix needs to be set for the status key
8484
*
8585
* @return string
86-
* @throws \InvalidArgumentException
8786
*/
88-
public static function create($queue, $class, $args = null, $monitor = false, $id = null, $prefix = "")
87+
public static function create($queue, $class, array $args = [], $monitor = false, $id = null, $prefix = "")
8988
{
9089
if (is_null($id)) {
9190
$id = Resque::generateJobId();
9291
}
9392

94-
if ($args !== null && !is_array($args)) {
95-
throw new InvalidArgumentException(
96-
'Supplied $args must be an array.'
97-
);
98-
}
9993
Resque::push($queue, array(
10094
'class' => $class,
10195
'args' => array($args),
@@ -183,7 +177,7 @@ public function getStatus()
183177
*
184178
* @return array Array of arguments.
185179
*/
186-
public function getArguments()
180+
public function getArguments(): array
187181
{
188182
if (!isset($this->payload['args'])) {
189183
return array();
@@ -194,10 +188,10 @@ public function getArguments()
194188

195189
/**
196190
* Get the instantiated object for this job that will be performing work.
197-
* @return \Resque\Job\JobInterface Instance of the object that this job belongs to.
191+
* @return \Resque\Job\Job Instance of the object that this job belongs to.
198192
* @throws \Resque\Exceptions\ResqueException
199193
*/
200-
public function getInstance()
194+
public function getInstance(): Job
201195
{
202196
if (!is_null($this->instance)) {
203197
return $this->instance;
@@ -212,9 +206,8 @@ public function getInstance()
212206
* Actually execute a job by calling the perform method on the class
213207
* associated with the job with the supplied arguments.
214208
*
215-
* @return bool
216-
* @throws Resque\Exceptions\ResqueException When the job's class could not be found
217-
* or it does not contain a perform method.
209+
* @return mixed
210+
* @throws Resque\Exceptions\ResqueException When the job's class could not be found.
218211
*/
219212
public function perform()
220213
{
@@ -225,15 +218,12 @@ public function perform()
225218
$this->startTime = microtime(true);
226219

227220
$instance = $this->getInstance();
228-
if (is_callable([$instance, 'setUp'])) {
229-
$instance->setUp();
230-
}
221+
222+
$instance->setUp();
231223

232224
$result = $instance->perform();
233225

234-
if (is_callable([$instance, 'tearDown'])) {
235-
$instance->tearDown();
236-
}
226+
$instance->tearDown();
237227

238228
$this->endTime = microtime(true);
239229

@@ -343,7 +333,7 @@ public function setJobFactory(FactoryInterface $jobFactory)
343333
/**
344334
* @return Resque\Job\FactoryInterface
345335
*/
346-
public function getJobFactory()
336+
public function getJobFactory(): FactoryInterface
347337
{
348338
if ($this->jobFactory === null) {
349339
$this->jobFactory = new Factory();

lib/Resque.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public static function size($queue)
234234
*
235235
* @return string|boolean Job ID when the job was created, false if creation was cancelled due to beforeEnqueue
236236
*/
237-
public static function enqueue($queue, $class, $args = null, $trackStatus = false, $prefix = "")
237+
public static function enqueue($queue, $class, array $args = [], $trackStatus = false, $prefix = "")
238238
{
239239
$id = Resque::generateJobId();
240240
$hookParams = array(

test/Resque/Tests/JobHandlerTest.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use \Resque\Redis;
88
use \Resque\JobHandler;
99
use \Resque\Stat;
10-
use \Resque\Job\JobInterface;
10+
use \Resque\Job\Job;
1111
use \Resque\Job\FactoryInterface;
1212
use \Test_Job_With_SetUp;
1313
use \Test_Job_With_TearDown;
@@ -68,15 +68,6 @@ public function testQeueuedJobCanBeReserved()
6868
$this->assertEquals('Test_Job', $job->payload['class']);
6969
}
7070

71-
public function testObjectArgumentsCannotBePassedToJob()
72-
{
73-
$this->expectException('InvalidArgumentException');
74-
75-
$args = new stdClass();
76-
$args->test = 'somevalue';
77-
Resque::enqueue('jobs', 'Test_Job', $args);
78-
}
79-
8071
public function testQueuedJobReturnsExactSamePassedInArguments()
8172
{
8273
$args = array(
@@ -168,10 +159,10 @@ public function testJobWithSetUpCallbackFiresSetUp()
168159
{
169160
$payload = array(
170161
'class' => 'Test_Job_With_SetUp',
171-
'args' => array(
162+
'args' => array(array(
172163
'somevar',
173164
'somevar2',
174-
),
165+
)),
175166
);
176167
$job = new JobHandler('jobs', $payload);
177168
$job->perform();
@@ -183,10 +174,10 @@ public function testJobWithTearDownCallbackFiresTearDown()
183174
{
184175
$payload = array(
185176
'class' => 'Test_Job_With_TearDown',
186-
'args' => array(
177+
'args' => array(array(
187178
'somevar',
188179
'somevar2',
189-
),
180+
)),
190181
);
191182
$job = new JobHandler('jobs', $payload);
192183
$job->perform();
@@ -410,7 +401,7 @@ public function testUseFactoryToGetJobInstance()
410401
$factory = new Some_Stub_Factory();
411402
$job->setJobFactory($factory);
412403
$instance = $job->getInstance();
413-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
404+
$this->assertInstanceOf('Resque\Job\Job', $instance);
414405
}
415406

416407
public function testDoNotUseFactoryToGetInstance()
@@ -422,11 +413,11 @@ public function testDoNotUseFactoryToGetInstance()
422413
$job = new JobHandler('jobs', $payload);
423414
$factory = $this->getMockBuilder('Resque\Job\FactoryInterface')
424415
->getMock();
425-
$testJob = $this->getMockBuilder('Resque\Job\JobInterface')
416+
$testJob = $this->getMockBuilder('Resque\Job\Job')
426417
->getMock();
427418
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
428419
$instance = $job->getInstance();
429-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
420+
$this->assertInstanceOf('Resque\Job\Job', $instance);
430421
}
431422

432423
public function testJobStatusIsNullIfIdMissingFromPayload()
@@ -505,7 +496,7 @@ public function testJobHandlerSetsStartAndEndTimeForFailedJob()
505496
}
506497
}
507498

508-
class Some_Job_Class implements JobInterface
499+
class Some_Job_Class extends Job
509500
{
510501

511502
/**
@@ -524,9 +515,9 @@ class Some_Stub_Factory implements FactoryInterface
524515
* @param $className
525516
* @param array $args
526517
* @param $queue
527-
* @return Resque\Job\JobInterface
518+
* @return Resque\Job\Job
528519
*/
529-
public function create($className, $args, $queue)
520+
public function create($className, $args, $queue): Job
530521
{
531522
return new Some_Job_Class();
532523
}

test/Resque/Tests/JobPIDTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testQueuedJobDoesNotReturnPID()
3434
$this->logger->expects($this->never())
3535
->method('log');
3636

37-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
37+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
3838
$this->assertEquals(0, PID::get($token));
3939
}
4040

@@ -43,14 +43,14 @@ public function testRunningJobReturnsPID()
4343
// Cannot use InProgress_Job on non-forking OS.
4444
if(!function_exists('pcntl_fork')) return;
4545

46-
$token = Resque::enqueue('jobs', 'InProgress_Job', null, true);
46+
$token = Resque::enqueue('jobs', 'InProgress_Job', [], true);
4747
$this->worker->work(0);
4848
$this->assertNotEquals(0, PID::get($token));
4949
}
5050

5151
public function testFinishedJobDoesNotReturnPID()
5252
{
53-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
53+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
5454
$this->worker->work(0);
5555
$this->assertEquals(0, PID::get($token));
5656
}

0 commit comments

Comments
 (0)