mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-24 17:25:13 +00:00
BIG-28720 Allowing to use a factory instead of manually instantite the Jobs
This commit is contained in:
parent
15a14d8a86
commit
7d2ce1bc8b
@ -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
|
class Resque_Job implements Resque_JobInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @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,10 +24,15 @@ class Resque_Job
|
|||||||
public $payload;
|
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;
|
private $instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Resque_Job_FactoryInterface
|
||||||
|
*/
|
||||||
|
private $jobFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate a new instance of a job.
|
* Instantiate a new instance of a job.
|
||||||
*
|
*
|
||||||
@ -40,17 +45,18 @@ class Resque_Job
|
|||||||
$this->payload = $payload;
|
$this->payload = $payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new job and save it to the specified queue.
|
* Create a new job and save it to the specified queue.
|
||||||
*
|
*
|
||||||
* @param string $queue The name of the queue to place the job in.
|
* @param string $queue The name of the queue to place the job in.
|
||||||
* @param string $class The name of the class that contains the code to execute the job.
|
* @param string $class The name of the class that contains the code to execute the job.
|
||||||
* @param array $args Any optional arguments that should be passed when the job is executed.
|
* @param array $args Any optional arguments that should be passed when the job is executed.
|
||||||
* @param boolean $monitor Set to true to be able to monitor the status of a job.
|
* @param boolean $monitor Set to true to be able to monitor the status of a job.
|
||||||
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
|
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
|
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
|
||||||
{
|
{
|
||||||
if (is_null($id)) {
|
if (is_null($id)) {
|
||||||
@ -81,7 +87,7 @@ class Resque_Job
|
|||||||
* instance of Resque_Job for it.
|
* instance of Resque_Job for it.
|
||||||
*
|
*
|
||||||
* @param string $queue The name of the queue to check for a job in.
|
* @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)
|
public static function reserve($queue)
|
||||||
{
|
{
|
||||||
@ -99,7 +105,7 @@ class Resque_Job
|
|||||||
*
|
*
|
||||||
* @param array $queues
|
* @param array $queues
|
||||||
* @param int $timeout
|
* @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)
|
public static function reserveBlocking(array $queues, $timeout = null)
|
||||||
{
|
{
|
||||||
@ -152,11 +158,11 @@ class Resque_Job
|
|||||||
return $this->payload['args'][0];
|
return $this->payload['args'][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the instantiated object for this job that will be performing work.
|
* Get the instantiated object for this job that will be performing work.
|
||||||
*
|
* @return Resque_JobInterface Instance of the object that this job belongs to.
|
||||||
* @return object Instance of the object that this job belongs to.
|
* @throws Resque_Exception
|
||||||
*/
|
*/
|
||||||
public function getInstance()
|
public function getInstance()
|
||||||
{
|
{
|
||||||
if (!is_null($this->instance)) {
|
if (!is_null($this->instance)) {
|
||||||
@ -175,7 +181,11 @@ class Resque_Job
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->instance = new $this->payload['class'];
|
if ($this->jobFactory !== null) {
|
||||||
|
$this->instance = $this->jobFactory->create();
|
||||||
|
} else {
|
||||||
|
$this->instance = new $this->payload['class'];
|
||||||
|
}
|
||||||
$this->instance->job = $this;
|
$this->instance->job = $this;
|
||||||
$this->instance->args = $this->getArguments();
|
$this->instance->args = $this->getArguments();
|
||||||
$this->instance->queue = $this->queue;
|
$this->instance->queue = $this->queue;
|
||||||
@ -272,4 +282,15 @@ class Resque_Job
|
|||||||
}
|
}
|
||||||
return '(' . implode(' | ', $name) . ')';
|
return '(' . implode(' | ', $name) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Resque_Job_FactoryInterface $jobFactory
|
||||||
|
* @return Resque_Job
|
||||||
|
*/
|
||||||
|
public function setJobFactory(Resque_Job_FactoryInterface $jobFactory)
|
||||||
|
{
|
||||||
|
$this->jobFactory = $jobFactory;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
9
lib/Resque/Job/FactoryInterface.php
Normal file
9
lib/Resque/Job/FactoryInterface.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
interface Resque_Job_FactoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return Resque_JobInterface
|
||||||
|
*/
|
||||||
|
public function create();
|
||||||
|
}
|
9
lib/Resque/JobInterface.php
Normal file
9
lib/Resque/JobInterface.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
interface Resque_JobInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function perform();
|
||||||
|
}
|
@ -362,4 +362,44 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
$this->assertEquals(Resque::size($queue), 2);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,11 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
|
|||||||
protected $resque;
|
protected $resque;
|
||||||
protected $redis;
|
protected $redis;
|
||||||
|
|
||||||
|
public static function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
}
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$config = file_get_contents(REDIS_CONF);
|
$config = file_get_contents(REDIS_CONF);
|
||||||
@ -20,4 +25,4 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
|
|||||||
// Flush redis
|
// Flush redis
|
||||||
$this->redis->flushAll();
|
$this->redis->flushAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user