BIG-28720 Allowing to use a factory instead of manually instantite the Jobs

This commit is contained in:
Sebastian Machuca 2016-10-06 12:49:15 +11:00
parent 15a14d8a86
commit 7d2ce1bc8b
No known key found for this signature in database
GPG Key ID: 4B3DA17F9CCF39DE
5 changed files with 106 additions and 22 deletions

View File

@ -6,7 +6,7 @@
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job
class Resque_Job implements Resque_JobInterface
{
/**
* @var string The name of the queue that this job belongs to.
@ -24,10 +24,15 @@ class Resque_Job
public $payload;
/**
* @var object Instance of the class performing work for this job.
* @var Resque_JobInterface Instance of the class performing work for this job.
*/
private $instance;
/**
* @var Resque_Job_FactoryInterface
*/
private $jobFactory;
/**
* Instantiate a new instance of a job.
*
@ -50,6 +55,7 @@ class Resque_Job
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
*
* @return string
* @throws \InvalidArgumentException
*/
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
{
@ -81,7 +87,7 @@ class Resque_Job
* instance of Resque_Job for it.
*
* @param string $queue The name of the queue to check for a job in.
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
* @return false|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
*/
public static function reserve($queue)
{
@ -99,7 +105,7 @@ class Resque_Job
*
* @param array $queues
* @param int $timeout
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
* @return false|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
*/
public static function reserveBlocking(array $queues, $timeout = null)
{
@ -154,8 +160,8 @@ class Resque_Job
/**
* Get the instantiated object for this job that will be performing work.
*
* @return object Instance of the object that this job belongs to.
* @return Resque_JobInterface Instance of the object that this job belongs to.
* @throws Resque_Exception
*/
public function getInstance()
{
@ -175,7 +181,11 @@ class Resque_Job
);
}
if ($this->jobFactory !== null) {
$this->instance = $this->jobFactory->create();
} else {
$this->instance = new $this->payload['class'];
}
$this->instance->job = $this;
$this->instance->args = $this->getArguments();
$this->instance->queue = $this->queue;
@ -272,4 +282,15 @@ class Resque_Job
}
return '(' . implode(' | ', $name) . ')';
}
/**
* @param Resque_Job_FactoryInterface $jobFactory
* @return Resque_Job
*/
public function setJobFactory(Resque_Job_FactoryInterface $jobFactory)
{
$this->jobFactory = $jobFactory;
return $this;
}
}

View File

@ -0,0 +1,9 @@
<?php
interface Resque_Job_FactoryInterface
{
/**
* @return Resque_JobInterface
*/
public function create();
}

View File

@ -0,0 +1,9 @@
<?php
interface Resque_JobInterface
{
/**
* @return bool
*/
public function perform();
}

View File

@ -362,4 +362,44 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$this->assertEquals(Resque::size($queue), 2);
}
public function testUseFactoryToGetJobInstance()
{
$payload = array(
'class' => Some_Job_Class::class,
'args' => null
);
$job = new Resque_Job('jobs', $payload);
$factory = $this->getMock(Resque_Job_FactoryInterface::class);
$job->setJobFactory($factory);
$testJob = $this->getMock(Resque_JobInterface::class);
$factory->expects(self::once())->method('create')->will($this->returnValue($testJob));
$instance = $job->getInstance();
$this->assertInstanceOf(Resque_JobInterface::class, $instance);
}
public function testDoNotUseFactoryToGetInstance()
{
$payload = array(
'class' => Some_Job_Class::class,
'args' => null
);
$job = new Resque_Job('jobs', $payload);
$factory = $this->getMock(Resque_Job_FactoryInterface::class);
$testJob = $this->getMock(Resque_JobInterface::class);
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
$instance = $job->getInstance();
$this->assertInstanceOf(Resque_JobInterface::class, $instance);
}
}
class Some_Job_Class implements Resque_JobInterface
{
/**
* @return bool
*/
public function perform()
{
return true;
}
}

View File

@ -11,6 +11,11 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
protected $resque;
protected $redis;
public static function setUpBeforeClass()
{
date_default_timezone_set('UTC');
}
public function setUp()
{
$config = file_get_contents(REDIS_CONF);