From 7d2ce1bc8be5042bfcdda611520614f761d7c321 Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Thu, 6 Oct 2016 12:49:15 +1100 Subject: [PATCH 1/5] BIG-28720 Allowing to use a factory instead of manually instantite the Jobs --- lib/Resque/Job.php | 63 +++++++++++++++++++---------- lib/Resque/Job/FactoryInterface.php | 9 +++++ lib/Resque/JobInterface.php | 9 +++++ test/Resque/Tests/JobTest.php | 40 ++++++++++++++++++ test/Resque/Tests/TestCase.php | 7 +++- 5 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 lib/Resque/Job/FactoryInterface.php create mode 100644 lib/Resque/JobInterface.php diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index dfd8869..6c39e18 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -6,7 +6,7 @@ * @author Chris Boulton * @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. * @@ -40,17 +45,18 @@ class Resque_Job $this->payload = $payload; } - /** - * 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 $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 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. - * - * @return string - */ + /** + * 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 $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 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. + * + * @return string + * @throws \InvalidArgumentException + */ public static function create($queue, $class, $args = null, $monitor = false, $id = null) { if (is_null($id)) { @@ -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) { @@ -152,11 +158,11 @@ class Resque_Job return $this->payload['args'][0]; } - /** - * Get the instantiated object for this job that will be performing work. - * - * @return object Instance of the object that this job belongs to. - */ + /** + * Get the instantiated object for this job that will be performing work. + * @return Resque_JobInterface Instance of the object that this job belongs to. + * @throws Resque_Exception + */ public function getInstance() { 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->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; + } } diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php new file mode 100644 index 0000000..9a2b14c --- /dev/null +++ b/lib/Resque/Job/FactoryInterface.php @@ -0,0 +1,9 @@ +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; + } } diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index 4ed65de..c828f6f 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -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); @@ -20,4 +25,4 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase // Flush redis $this->redis->flushAll(); } -} \ No newline at end of file +} From de22db682686700f96544e089a23cbb1974c1d63 Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Thu, 6 Oct 2016 16:07:16 +1100 Subject: [PATCH 2/5] Making it compatible with PHP < 5.5 Making the factory receive the classname --- lib/Resque/Job.php | 2 +- lib/Resque/Job/FactoryInterface.php | 3 ++- test/Resque/Tests/JobTest.php | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index 6c39e18..31952aa 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -182,7 +182,7 @@ class Resque_Job implements Resque_JobInterface } if ($this->jobFactory !== null) { - $this->instance = $this->jobFactory->create(); + $this->instance = $this->jobFactory->create($this->payload['class']); } else { $this->instance = new $this->payload['class']; } diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php index 9a2b14c..89edbee 100644 --- a/lib/Resque/Job/FactoryInterface.php +++ b/lib/Resque/Job/FactoryInterface.php @@ -3,7 +3,8 @@ interface Resque_Job_FactoryInterface { /** + * @param $className * @return Resque_JobInterface */ - public function create(); + public function create($className); } diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 1f2b428..32111d0 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -365,30 +365,30 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase public function testUseFactoryToGetJobInstance() { $payload = array( - 'class' => Some_Job_Class::class, + 'class' => 'Some_Job_Class', 'args' => null ); $job = new Resque_Job('jobs', $payload); - $factory = $this->getMock(Resque_Job_FactoryInterface::class); + $factory = $this->getMock('Resque_Job_FactoryInterface'); $job->setJobFactory($factory); - $testJob = $this->getMock(Resque_JobInterface::class); + $testJob = $this->getMock('Resque_JobInterface'); $factory->expects(self::once())->method('create')->will($this->returnValue($testJob)); $instance = $job->getInstance(); - $this->assertInstanceOf(Resque_JobInterface::class, $instance); + $this->assertInstanceOf('Resque_JobInterface', $instance); } public function testDoNotUseFactoryToGetInstance() { $payload = array( - 'class' => Some_Job_Class::class, + 'class' => 'Some_Job_Class', 'args' => null ); $job = new Resque_Job('jobs', $payload); - $factory = $this->getMock(Resque_Job_FactoryInterface::class); - $testJob = $this->getMock(Resque_JobInterface::class); + $factory = $this->getMock('Resque_Job_FactoryInterface'); + $testJob = $this->getMock('Resque_JobInterface'); $factory->expects(self::never())->method('create')->will(self::returnValue($testJob)); $instance = $job->getInstance(); - $this->assertInstanceOf(Resque_JobInterface::class, $instance); + $this->assertInstanceOf('Resque_JobInterface', $instance); } } From 8f542e5035c12f27e74b3c223ecf9c566381bd64 Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Wed, 12 Oct 2016 19:22:31 +1100 Subject: [PATCH 3/5] Making the factory responsible to set the arguments and the queue --- lib/Resque/Job.php | 13 +++++++------ lib/Resque/Job/FactoryInterface.php | 4 +++- test/Resque/Tests/JobTest.php | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index 31952aa..1465b36 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -182,13 +182,14 @@ class Resque_Job implements Resque_JobInterface } if ($this->jobFactory !== null) { - $this->instance = $this->jobFactory->create($this->payload['class']); - } else { - $this->instance = new $this->payload['class']; + $this->instance = $this->jobFactory->create($this->payload['class'], $this->getArguments(), $this->queue); + return $this->instance; } - $this->instance->job = $this; - $this->instance->args = $this->getArguments(); - $this->instance->queue = $this->queue; + $this->instance = new $this->payload['class']; + $this->instance->job = $this; + $this->instance->args = $this->getArguments(); + $this->instance->queue = $this->queue; + return $this->instance; } diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php index 89edbee..d12211d 100644 --- a/lib/Resque/Job/FactoryInterface.php +++ b/lib/Resque/Job/FactoryInterface.php @@ -4,7 +4,9 @@ interface Resque_Job_FactoryInterface { /** * @param $className + * @param array $args + * @param $queue * @return Resque_JobInterface */ - public function create($className); + public function create($className, array $args, $queue); } diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 32111d0..a71e6f0 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -369,10 +369,8 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase 'args' => null ); $job = new Resque_Job('jobs', $payload); - $factory = $this->getMock('Resque_Job_FactoryInterface'); + $factory = new Some_Stub_Factory(); $job->setJobFactory($factory); - $testJob = $this->getMock('Resque_JobInterface'); - $factory->expects(self::once())->method('create')->will($this->returnValue($testJob)); $instance = $job->getInstance(); $this->assertInstanceOf('Resque_JobInterface', $instance); } @@ -403,3 +401,18 @@ class Some_Job_Class implements Resque_JobInterface return true; } } + +class Some_Stub_Factory implements Resque_Job_FactoryInterface +{ + + /** + * @param $className + * @param array $args + * @param $queue + * @return Resque_JobInterface + */ + public function create($className, array $args, $queue) + { + return new Some_Job_Class(); + } +} From df20186c37011786f150c7e2867961dadc5f385e Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Thu, 13 Oct 2016 14:45:02 +1100 Subject: [PATCH 4/5] Convert spaces to tabs --- lib/Resque/Job.php | 138 ++++++++++++++-------------- lib/Resque/Job/FactoryInterface.php | 14 +-- lib/Resque/JobInterface.php | 8 +- test/Resque/Tests/JobTest.php | 100 ++++++++++---------- test/Resque/Tests/TestCase.php | 8 +- 5 files changed, 134 insertions(+), 134 deletions(-) diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index 1465b36..bb79f32 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -28,10 +28,10 @@ class Resque_Job implements Resque_JobInterface */ private $instance; - /** - * @var Resque_Job_FactoryInterface - */ - private $jobFactory; + /** + * @var Resque_Job_FactoryInterface + */ + private $jobFactory; /** * Instantiate a new instance of a job. @@ -45,18 +45,18 @@ class Resque_Job implements Resque_JobInterface $this->payload = $payload; } - /** - * 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 $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 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. - * - * @return string - * @throws \InvalidArgumentException - */ + /** + * 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 $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 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. + * + * @return string + * @throws \InvalidArgumentException + */ public static function create($queue, $class, $args = null, $monitor = false, $id = null) { if (is_null($id)) { @@ -82,41 +82,41 @@ class Resque_Job implements Resque_JobInterface return $id; } - /** - * Find the next available job from the specified queue and return an - * instance of Resque_Job for it. - * - * @param string $queue The name of the queue to check for a job in. - * @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) - { - $payload = Resque::pop($queue); - if(!is_array($payload)) { - return false; - } + /** + * Find the next available job from the specified queue and return an + * instance of Resque_Job for it. + * + * @param string $queue The name of the queue to check for a job in. + * @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) + { + $payload = Resque::pop($queue); + if(!is_array($payload)) { + return false; + } - return new Resque_Job($queue, $payload); - } + return new Resque_Job($queue, $payload); + } - /** - * Find the next available job from the specified queues using blocking list pop - * and return an instance of Resque_Job for it. - * - * @param array $queues - * @param int $timeout - * @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) - { - $item = Resque::blpop($queues, $timeout); + /** + * Find the next available job from the specified queues using blocking list pop + * and return an instance of Resque_Job for it. + * + * @param array $queues + * @param int $timeout + * @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) + { + $item = Resque::blpop($queues, $timeout); - if(!is_array($item)) { - return false; - } + if(!is_array($item)) { + return false; + } - return new Resque_Job($item['queue'], $item['payload']); - } + return new Resque_Job($item['queue'], $item['payload']); + } /** * Update the status of the current job. @@ -158,11 +158,11 @@ class Resque_Job implements Resque_JobInterface return $this->payload['args'][0]; } - /** - * Get the instantiated object for this job that will be performing work. - * @return Resque_JobInterface Instance of the object that this job belongs to. - * @throws Resque_Exception - */ + /** + * Get the instantiated object for this job that will be performing work. + * @return Resque_JobInterface Instance of the object that this job belongs to. + * @throws Resque_Exception + */ public function getInstance() { if (!is_null($this->instance)) { @@ -181,14 +181,14 @@ class Resque_Job implements Resque_JobInterface ); } - 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; + 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; } @@ -284,14 +284,14 @@ class Resque_Job implements Resque_JobInterface return '(' . implode(' | ', $name) . ')'; } - /** - * @param Resque_Job_FactoryInterface $jobFactory - * @return Resque_Job - */ - public function setJobFactory(Resque_Job_FactoryInterface $jobFactory) - { - $this->jobFactory = $jobFactory; + /** + * @param Resque_Job_FactoryInterface $jobFactory + * @return Resque_Job + */ + public function setJobFactory(Resque_Job_FactoryInterface $jobFactory) + { + $this->jobFactory = $jobFactory; - return $this; - } + return $this; + } } diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php index d12211d..be573a2 100644 --- a/lib/Resque/Job/FactoryInterface.php +++ b/lib/Resque/Job/FactoryInterface.php @@ -2,11 +2,11 @@ interface Resque_Job_FactoryInterface { - /** - * @param $className - * @param array $args - * @param $queue - * @return Resque_JobInterface - */ - public function create($className, array $args, $queue); + /** + * @param $className + * @param array $args + * @param $queue + * @return Resque_JobInterface + */ + public function create($className, array $args, $queue); } diff --git a/lib/Resque/JobInterface.php b/lib/Resque/JobInterface.php index f31281d..be5891d 100644 --- a/lib/Resque/JobInterface.php +++ b/lib/Resque/JobInterface.php @@ -2,8 +2,8 @@ interface Resque_JobInterface { - /** - * @return bool - */ - public function perform(); + /** + * @return bool + */ + public function perform(); } diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index a71e6f0..a1788ed 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -183,16 +183,16 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase public function testJobWithNamespace() { - Resque_Redis::prefix('php'); - $queue = 'jobs'; - $payload = array('another_value'); - Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload); - - $this->assertEquals(Resque::queues(), array('jobs')); - $this->assertEquals(Resque::size($queue), 1); + Resque_Redis::prefix('php'); + $queue = 'jobs'; + $payload = array('another_value'); + Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload); - Resque_Redis::prefix('resque'); - $this->assertEquals(Resque::size($queue), 0); + $this->assertEquals(Resque::queues(), array('jobs')); + $this->assertEquals(Resque::size($queue), 1); + + Resque_Redis::prefix('resque'); + $this->assertEquals(Resque::size($queue), 0); } public function testDequeueAll() @@ -363,56 +363,56 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase } public function testUseFactoryToGetJobInstance() - { - $payload = array( - 'class' => 'Some_Job_Class', - 'args' => null - ); - $job = new Resque_Job('jobs', $payload); - $factory = new Some_Stub_Factory(); - $job->setJobFactory($factory); - $instance = $job->getInstance(); - $this->assertInstanceOf('Resque_JobInterface', $instance); - } + { + $payload = array( + 'class' => 'Some_Job_Class', + 'args' => null + ); + $job = new Resque_Job('jobs', $payload); + $factory = new Some_Stub_Factory(); + $job->setJobFactory($factory); + $instance = $job->getInstance(); + $this->assertInstanceOf('Resque_JobInterface', $instance); + } - public function testDoNotUseFactoryToGetInstance() - { - $payload = array( - 'class' => 'Some_Job_Class', - 'args' => null - ); - $job = new Resque_Job('jobs', $payload); - $factory = $this->getMock('Resque_Job_FactoryInterface'); - $testJob = $this->getMock('Resque_JobInterface'); - $factory->expects(self::never())->method('create')->will(self::returnValue($testJob)); - $instance = $job->getInstance(); - $this->assertInstanceOf('Resque_JobInterface', $instance); - } + public function testDoNotUseFactoryToGetInstance() + { + $payload = array( + 'class' => 'Some_Job_Class', + 'args' => null + ); + $job = new Resque_Job('jobs', $payload); + $factory = $this->getMock('Resque_Job_FactoryInterface'); + $testJob = $this->getMock('Resque_JobInterface'); + $factory->expects(self::never())->method('create')->will(self::returnValue($testJob)); + $instance = $job->getInstance(); + $this->assertInstanceOf('Resque_JobInterface', $instance); + } } class Some_Job_Class implements Resque_JobInterface { - /** - * @return bool - */ - public function perform() - { - return true; - } + /** + * @return bool + */ + public function perform() + { + return true; + } } class Some_Stub_Factory implements Resque_Job_FactoryInterface { - /** - * @param $className - * @param array $args - * @param $queue - * @return Resque_JobInterface - */ - public function create($className, array $args, $queue) - { - return new Some_Job_Class(); - } + /** + * @param $className + * @param array $args + * @param $queue + * @return Resque_JobInterface + */ + public function create($className, array $args, $queue) + { + return new Some_Job_Class(); + } } diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index c828f6f..6a01219 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -11,10 +11,10 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase protected $resque; protected $redis; - public static function setUpBeforeClass() - { - date_default_timezone_set('UTC'); - } + public static function setUpBeforeClass() + { + date_default_timezone_set('UTC'); + } public function setUp() { From be19e12c315923345d9d6c5b911d57cdfaad012b Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Fri, 14 Oct 2016 14:41:19 +1100 Subject: [PATCH 5/5] Making the factory the default layer to construct jobs --- lib/Resque/Job.php | 40 ++++++++++++----------------- lib/Resque/Job/Factory.php | 32 +++++++++++++++++++++++ lib/Resque/Job/FactoryInterface.php | 2 +- test/Resque/Tests/EventTest.php | 2 +- test/Resque/Tests/JobTest.php | 17 +++++++++--- 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 lib/Resque/Job/Factory.php diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index bb79f32..8508f76 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -6,7 +6,7 @@ * @author Chris Boulton * @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. @@ -24,7 +24,7 @@ class Resque_Job implements Resque_JobInterface 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; @@ -169,28 +169,9 @@ class Resque_Job implements Resque_JobInterface return $this->instance; } - if(!class_exists($this->payload['class'])) { - throw new Resque_Exception( - 'Could not find job class ' . $this->payload['class'] . '.' - ); - } - - 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; + $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue); + $this->instance->job = $this; + return $this->instance; } /** @@ -294,4 +275,15 @@ class Resque_Job implements Resque_JobInterface return $this; } + + /** + * @return Resque_Job_FactoryInterface + */ + public function getJobFactory() + { + if ($this->jobFactory === null) { + $this->jobFactory = new Resque_Job_Factory(); + } + return $this->jobFactory; + } } diff --git a/lib/Resque/Job/Factory.php b/lib/Resque/Job/Factory.php new file mode 100644 index 0000000..cf17294 --- /dev/null +++ b/lib/Resque/Job/Factory.php @@ -0,0 +1,32 @@ +args = $args; + $instance->queue = $queue; + return $instance; + } +} diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php index be573a2..b8c102c 100644 --- a/lib/Resque/Job/FactoryInterface.php +++ b/lib/Resque/Job/FactoryInterface.php @@ -8,5 +8,5 @@ interface Resque_Job_FactoryInterface * @param $queue * @return Resque_JobInterface */ - public function create($className, array $args, $queue); + public function create($className, $args, $queue); } diff --git a/test/Resque/Tests/EventTest.php b/test/Resque/Tests/EventTest.php index 1147c99..6e102cf 100644 --- a/test/Resque/Tests/EventTest.php +++ b/test/Resque/Tests/EventTest.php @@ -31,7 +31,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase $payload = array( 'class' => 'Test_Job', 'args' => array( - 'somevar', + array('somevar'), ), ); $job = new Resque_Job('jobs', $payload); diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index a1788ed..fe58ce4 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -362,13 +362,24 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->assertEquals(Resque::size($queue), 2); } - public function testUseFactoryToGetJobInstance() + public function testUseDefaultFactoryToGetJobInstance() { $payload = array( 'class' => 'Some_Job_Class', 'args' => null ); $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(); $job->setJobFactory($factory); $instance = $job->getInstance(); @@ -379,7 +390,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase { $payload = array( 'class' => 'Some_Job_Class', - 'args' => null + 'args' => array(array()) ); $job = new Resque_Job('jobs', $payload); $factory = $this->getMock('Resque_Job_FactoryInterface'); @@ -411,7 +422,7 @@ class Some_Stub_Factory implements Resque_Job_FactoryInterface * @param $queue * @return Resque_JobInterface */ - public function create($className, array $args, $queue) + public function create($className, $args, $queue) { return new Some_Job_Class(); }