Skip to content

Commit 7d2ce1b

Browse files
author
Sebastian Machuca
committed
BIG-28720 Allowing to use a factory instead of manually instantite the Jobs
1 parent 15a14d8 commit 7d2ce1b

File tree

5 files changed

+106
-22
lines changed

5 files changed

+106
-22
lines changed

lib/Resque/Job.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Chris Boulton <[email protected]>
77
* @license http://www.opensource.org/licenses/mit-license.php
88
*/
9-
class Resque_Job
9+
class Resque_Job implements Resque_JobInterface
1010
{
1111
/**
1212
* @var string The name of the queue that this job belongs to.
@@ -24,10 +24,15 @@ class Resque_Job
2424
public $payload;
2525

2626
/**
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.
2828
*/
2929
private $instance;
3030

31+
/**
32+
* @var Resque_Job_FactoryInterface
33+
*/
34+
private $jobFactory;
35+
3136
/**
3237
* Instantiate a new instance of a job.
3338
*
@@ -40,17 +45,18 @@ public function __construct($queue, $payload)
4045
$this->payload = $payload;
4146
}
4247

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+
*/
5460
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
5561
{
5662
if (is_null($id)) {
@@ -81,7 +87,7 @@ public static function create($queue, $class, $args = null, $monitor = false, $i
8187
* instance of Resque_Job for it.
8288
*
8389
* @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.
8591
*/
8692
public static function reserve($queue)
8793
{
@@ -99,7 +105,7 @@ public static function reserve($queue)
99105
*
100106
* @param array $queues
101107
* @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.
103109
*/
104110
public static function reserveBlocking(array $queues, $timeout = null)
105111
{
@@ -152,11 +158,11 @@ public function getArguments()
152158
return $this->payload['args'][0];
153159
}
154160

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+
*/
160166
public function getInstance()
161167
{
162168
if (!is_null($this->instance)) {
@@ -175,7 +181,11 @@ public function getInstance()
175181
);
176182
}
177183

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+
}
179189
$this->instance->job = $this;
180190
$this->instance->args = $this->getArguments();
181191
$this->instance->queue = $this->queue;
@@ -272,4 +282,15 @@ public function __toString()
272282
}
273283
return '(' . implode(' | ', $name) . ')';
274284
}
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+
}
275296
}

lib/Resque/Job/FactoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
interface Resque_Job_FactoryInterface
4+
{
5+
/**
6+
* @return Resque_JobInterface
7+
*/
8+
public function create();
9+
}

lib/Resque/JobInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
interface Resque_JobInterface
4+
{
5+
/**
6+
* @return bool
7+
*/
8+
public function perform();
9+
}

test/Resque/Tests/JobTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,44 @@ public function testDequeueItemWithiWrongArg()
362362
$this->assertEquals(Resque::size($queue), 2);
363363
}
364364

365+
public function testUseFactoryToGetJobInstance()
366+
{
367+
$payload = array(
368+
'class' => Some_Job_Class::class,
369+
'args' => null
370+
);
371+
$job = new Resque_Job('jobs', $payload);
372+
$factory = $this->getMock(Resque_Job_FactoryInterface::class);
373+
$job->setJobFactory($factory);
374+
$testJob = $this->getMock(Resque_JobInterface::class);
375+
$factory->expects(self::once())->method('create')->will($this->returnValue($testJob));
376+
$instance = $job->getInstance();
377+
$this->assertInstanceOf(Resque_JobInterface::class, $instance);
378+
}
379+
380+
public function testDoNotUseFactoryToGetInstance()
381+
{
382+
$payload = array(
383+
'class' => Some_Job_Class::class,
384+
'args' => null
385+
);
386+
$job = new Resque_Job('jobs', $payload);
387+
$factory = $this->getMock(Resque_Job_FactoryInterface::class);
388+
$testJob = $this->getMock(Resque_JobInterface::class);
389+
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
390+
$instance = $job->getInstance();
391+
$this->assertInstanceOf(Resque_JobInterface::class, $instance);
392+
}
393+
}
394+
395+
class Some_Job_Class implements Resque_JobInterface
396+
{
397+
398+
/**
399+
* @return bool
400+
*/
401+
public function perform()
402+
{
403+
return true;
404+
}
365405
}

test/Resque/Tests/TestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
1111
protected $resque;
1212
protected $redis;
1313

14+
public static function setUpBeforeClass()
15+
{
16+
date_default_timezone_set('UTC');
17+
}
18+
1419
public function setUp()
1520
{
1621
$config = file_get_contents(REDIS_CONF);
@@ -20,4 +25,4 @@ public function setUp()
2025
// Flush redis
2126
$this->redis->flushAll();
2227
}
23-
}
28+
}

0 commit comments

Comments
 (0)