Making the factory the default layer to construct jobs

This commit is contained in:
Sebastian Machuca 2016-10-14 14:41:19 +11:00
parent df20186c37
commit be19e12c31
No known key found for this signature in database
GPG Key ID: 4B3DA17F9CCF39DE
5 changed files with 64 additions and 29 deletions

View File

@ -6,7 +6,7 @@
* @author Chris Boulton <chris@bigcommerce.com> * @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
class Resque_Job implements Resque_JobInterface class Resque_Job
{ {
/** /**
* @var string The name of the queue that this job belongs to. * @var string The name of the queue that this job belongs to.
@ -24,7 +24,7 @@ class Resque_Job implements Resque_JobInterface
public $payload; public $payload;
/** /**
* @var Resque_JobInterface Instance of the class performing work for this job. * @var object|Resque_JobInterface Instance of the class performing work for this job.
*/ */
private $instance; private $instance;
@ -169,28 +169,9 @@ class Resque_Job implements Resque_JobInterface
return $this->instance; return $this->instance;
} }
if(!class_exists($this->payload['class'])) { $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
throw new Resque_Exception( $this->instance->job = $this;
'Could not find job class ' . $this->payload['class'] . '.' return $this->instance;
);
}
if(!method_exists($this->payload['class'], 'perform')) {
throw new Resque_Exception(
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
);
}
if ($this->jobFactory !== null) {
$this->instance = $this->jobFactory->create($this->payload['class'], $this->getArguments(), $this->queue);
return $this->instance;
}
$this->instance = new $this->payload['class'];
$this->instance->job = $this;
$this->instance->args = $this->getArguments();
$this->instance->queue = $this->queue;
return $this->instance;
} }
/** /**
@ -294,4 +275,15 @@ class Resque_Job implements Resque_JobInterface
return $this; return $this;
} }
/**
* @return Resque_Job_FactoryInterface
*/
public function getJobFactory()
{
if ($this->jobFactory === null) {
$this->jobFactory = new Resque_Job_Factory();
}
return $this->jobFactory;
}
} }

View File

@ -0,0 +1,32 @@
<?php
class Resque_Job_Factory implements Resque_Job_FactoryInterface
{
/**
* @param $className
* @param array $args
* @param $queue
* @return Resque_JobInterface
* @throws \Resque_Exception
*/
public function create($className, $args, $queue)
{
if (!class_exists($className)) {
throw new Resque_Exception(
'Could not find job class ' . $className . '.'
);
}
if (!method_exists($className, 'perform')) {
throw new Resque_Exception(
'Job class ' . $className . ' does not contain a perform method.'
);
}
$instance = new $className;
$instance->args = $args;
$instance->queue = $queue;
return $instance;
}
}

View File

@ -8,5 +8,5 @@ interface Resque_Job_FactoryInterface
* @param $queue * @param $queue
* @return Resque_JobInterface * @return Resque_JobInterface
*/ */
public function create($className, array $args, $queue); public function create($className, $args, $queue);
} }

View File

@ -31,7 +31,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
$payload = array( $payload = array(
'class' => 'Test_Job', 'class' => 'Test_Job',
'args' => array( 'args' => array(
'somevar', array('somevar'),
), ),
); );
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);

View File

@ -362,13 +362,24 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$this->assertEquals(Resque::size($queue), 2); $this->assertEquals(Resque::size($queue), 2);
} }
public function testUseFactoryToGetJobInstance() public function testUseDefaultFactoryToGetJobInstance()
{ {
$payload = array( $payload = array(
'class' => 'Some_Job_Class', 'class' => 'Some_Job_Class',
'args' => null 'args' => null
); );
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$instance = $job->getInstance();
$this->assertInstanceOf('Some_Job_Class', $instance);
}
public function testUseFactoryToGetJobInstance()
{
$payload = array(
'class' => 'Some_Job_Class',
'args' => array(array())
);
$job = new Resque_Job('jobs', $payload);
$factory = new Some_Stub_Factory(); $factory = new Some_Stub_Factory();
$job->setJobFactory($factory); $job->setJobFactory($factory);
$instance = $job->getInstance(); $instance = $job->getInstance();
@ -379,7 +390,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
{ {
$payload = array( $payload = array(
'class' => 'Some_Job_Class', 'class' => 'Some_Job_Class',
'args' => null 'args' => array(array())
); );
$job = new Resque_Job('jobs', $payload); $job = new Resque_Job('jobs', $payload);
$factory = $this->getMock('Resque_Job_FactoryInterface'); $factory = $this->getMock('Resque_Job_FactoryInterface');
@ -411,7 +422,7 @@ class Some_Stub_Factory implements Resque_Job_FactoryInterface
* @param $queue * @param $queue
* @return Resque_JobInterface * @return Resque_JobInterface
*/ */
public function create($className, array $args, $queue) public function create($className, $args, $queue)
{ {
return new Some_Job_Class(); return new Some_Job_Class();
} }