Skip to content

Commit b56df98

Browse files
committed
Require every job to extend the base Job class
1 parent 456dd73 commit b56df98

File tree

7 files changed

+43
-67
lines changed

7 files changed

+43
-67
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ 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
{
115-
public function perform()
115+
public function perform(): void
116116
{
117117
// Work work work
118118
echo $this->args['name'];
@@ -132,19 +132,19 @@ 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
}
141141

142-
public function perform()
142+
public function perform(): void
143143
{
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($className, $args, $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($className, $args, $queue): Job;
1414
}

lib/Job/JobInterface.php

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

lib/JobHandler.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Resque\Exceptions\DoNotPerformException;
99
use Resque\Job\FactoryInterface;
1010
use Resque\Job\Factory;
11+
use Resque\Job\Job;
1112
use Error;
1213

1314
/**
@@ -50,7 +51,7 @@ class JobHandler
5051
public $endTime;
5152

5253
/**
53-
* @var object|\Resque\Job\JobInterface Instance of the class performing work for this job.
54+
* @var Job Instance of the class performing work for this job.
5455
*/
5556
private $instance;
5657

@@ -194,10 +195,10 @@ public function getArguments()
194195

195196
/**
196197
* 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.
198+
* @return \Resque\Job\Job Instance of the object that this job belongs to.
198199
* @throws \Resque\Exceptions\ResqueException
199200
*/
200-
public function getInstance()
201+
public function getInstance(): Job
201202
{
202203
if (!is_null($this->instance)) {
203204
return $this->instance;
@@ -225,15 +226,12 @@ public function perform()
225226
$this->startTime = microtime(true);
226227

227228
$instance = $this->getInstance();
228-
if (is_callable([$instance, 'setUp'])) {
229-
$instance->setUp();
230-
}
231229

232-
$result = $instance->perform();
230+
$instance->setUp();
233231

234-
if (is_callable([$instance, 'tearDown'])) {
235-
$instance->tearDown();
236-
}
232+
$instance->perform();
233+
234+
$instance->tearDown();
237235

238236
$this->endTime = microtime(true);
239237

@@ -343,7 +341,7 @@ public function setJobFactory(FactoryInterface $jobFactory)
343341
/**
344342
* @return Resque\Job\FactoryInterface
345343
*/
346-
public function getJobFactory()
344+
public function getJobFactory(): FactoryInterface
347345
{
348346
if ($this->jobFactory === null) {
349347
$this->jobFactory = new Factory();

test/Resque/Tests/JobHandlerTest.php

Lines changed: 10 additions & 10 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;
@@ -410,7 +410,7 @@ public function testUseFactoryToGetJobInstance()
410410
$factory = new Some_Stub_Factory();
411411
$job->setJobFactory($factory);
412412
$instance = $job->getInstance();
413-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
413+
$this->assertInstanceOf('Resque\Job\Job', $instance);
414414
}
415415

416416
public function testDoNotUseFactoryToGetInstance()
@@ -422,11 +422,11 @@ public function testDoNotUseFactoryToGetInstance()
422422
$job = new JobHandler('jobs', $payload);
423423
$factory = $this->getMockBuilder('Resque\Job\FactoryInterface')
424424
->getMock();
425-
$testJob = $this->getMockBuilder('Resque\Job\JobInterface')
425+
$testJob = $this->getMockBuilder('Resque\Job\Job')
426426
->getMock();
427427
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
428428
$instance = $job->getInstance();
429-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
429+
$this->assertInstanceOf('Resque\Job\Job', $instance);
430430
}
431431

432432
public function testJobStatusIsNullIfIdMissingFromPayload()
@@ -505,15 +505,15 @@ public function testJobHandlerSetsStartAndEndTimeForFailedJob()
505505
}
506506
}
507507

508-
class Some_Job_Class implements JobInterface
508+
class Some_Job_Class extends Job
509509
{
510510

511511
/**
512-
* @return bool
512+
* @return void
513513
*/
514-
public function perform()
514+
public function perform(): void
515515
{
516-
return true;
516+
// no-op
517517
}
518518
}
519519

@@ -524,9 +524,9 @@ class Some_Stub_Factory implements FactoryInterface
524524
* @param $className
525525
* @param array $args
526526
* @param $queue
527-
* @return Resque\Job\JobInterface
527+
* @return Resque\Job\Job
528528
*/
529-
public function create($className, $args, $queue)
529+
public function create($className, $args, $queue): Job
530530
{
531531
return new Some_Job_Class();
532532
}

test/bootstrap.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ function sigint()
8585
pcntl_signal(SIGTERM, 'sigint');
8686
}
8787

88-
class Test_Job
88+
class Test_Job extends \Resque\Job\Job
8989
{
9090
public static $called = false;
9191

92-
public function perform()
92+
public function perform(): void
9393
{
9494
self::$called = true;
9595
}
@@ -100,9 +100,9 @@ class Failing_Job_Exception extends \Exception
100100

101101
}
102102

103-
class Failing_Job
103+
class Failing_Job extends \Resque\Job\Job
104104
{
105-
public function perform()
105+
public function perform(): void
106106
{
107107
throw new Failing_Job_Exception('Message!');
108108
}
@@ -114,9 +114,9 @@ public function perform()
114114
*
115115
* CAUTION Use this test job only with Worker::work, i.e. only when you actually trigger the fork in tests.
116116
*/
117-
class InProgress_Job
117+
class InProgress_Job extends \Resque\Job\Job
118118
{
119-
public function perform()
119+
public function perform(): void
120120
{
121121
if(!function_exists('pcntl_fork')) {
122122
// We can't lose the worker on a non-forking OS.
@@ -126,47 +126,42 @@ public function perform()
126126
}
127127
}
128128

129-
class Test_Job_Without_Perform_Method
130-
{
131-
132-
}
133-
134-
class Test_Job_With_SetUp
129+
class Test_Job_With_SetUp extends \Resque\Job\Job
135130
{
136131
public static $called = false;
137132
public $args = false;
138133

139-
public function setUp()
134+
public function setUp(): void
140135
{
141136
self::$called = true;
142137
}
143138

144-
public function perform()
139+
public function perform(): void
145140
{
146141

147142
}
148143
}
149144

150145

151-
class Test_Job_With_TearDown
146+
class Test_Job_With_TearDown extends \Resque\Job\Job
152147
{
153148
public static $called = false;
154149
public $args = false;
155150

156-
public function perform()
151+
public function perform(): void
157152
{
158153

159154
}
160155

161-
public function tearDown()
156+
public function tearDown(): void
162157
{
163158
self::$called = true;
164159
}
165160
}
166161

167-
class Test_Infinite_Recursion_Job
162+
class Test_Infinite_Recursion_Job extends \Resque\Job\Job
168163
{
169-
public function perform()
164+
public function perform(): void
170165
{
171166
$this->perform();
172167
}

0 commit comments

Comments
 (0)