diff --git a/README.md b/README.md index 6336f6e..ba6fc99 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,6 @@ Here, we simply specify the data of the job, then we get the queue manager (more that will store those jobs (in most queuing systems you can create as much queues as you want), and then we push it so that it can pe popped later. - ### Handling dependencies for jobs Often, your job will have dependencies. For instance, the EncodingJob may need an Encoder object to help encode @@ -172,7 +171,7 @@ return array( ); ``` -> Note: if you don't have any dependencies for your jobs, you DO NOT need to add all your jobs to the invokables` +> Note: if you don't have any dependencies for your jobs, you DO NOT need to add all your jobs to the `invokables` > list, because the JobPluginManager is configured in a way that it automatically adds any unknown classes to the > `invokables` list. @@ -210,18 +209,18 @@ class EncodingJob extends AbstractJob implements QueueAwareInterface } ``` -If you want to avoid the boilerplate code, you can use the ProvidesQueue trait (only for PHP >=5.4): +If you want to avoid the boilerplate code, you can use the QueueAwareTrait trait (only for PHP >=5.4): ```php namespace Application\Job; use SlmQueue\Job\AbstractJob; -use SlmQueue\Queue\ProvidesQueue; +use SlmQueue\Queue\QueueAwareTrait; use SlmQueue\Queue\QueueAwareInterface; class EncodingJob extends AbstractJob implements QueueAwareInterface { - use ProvidesQueue; + use QueueAwareTrait; public function execute() { @@ -230,7 +229,6 @@ class EncodingJob extends AbstractJob implements QueueAwareInterface } ``` - ### Adding queues The Job thing is pretty agnostic to any queue management systems. However, the queues are not. SlmQueue provides @@ -251,17 +249,16 @@ In both cases, adding a new queue is as simple as adding a new line in your `mod ```php return array( 'slm_queue' => array( - 'queues' => array( + 'queue_manager' => array( 'factories' => array( - 'encodingQueue' => 'SlmQueueBeanstalkd\Factory\TubeFactory' // This is the factory provided by - // SlmQueueBeanstalkd module + 'encodingQueue' => 'SlmQueueSqs\Factory\SqsQueueFactory' // This is the factory provided by + // SlmQueueSqs module ) ) ) ); ``` - ### Executing jobs Once again, executing jobs is dependant on the queue system used. Therefore, please refer to either SlmQueueBeanstalkd, diff --git a/config/module.config.php b/config/module.config.php index 1aa8a47..cc669bb 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -11,13 +11,18 @@ 'slm_queue' => array( /** - * Parameters for the worker + * Worker options */ 'worker' => array( 'max_runs' => 100000, 'max_memory' => 100 * 1024 * 1024 ), + /** + * Queue configuration + */ + 'queues' => array(), + /** * Job manager configuration */ @@ -27,10 +32,5 @@ * Queue manager configuration */ 'queue_manager' => array(), - - /** - * Queue configuration options - */ - 'queues' => array(), - ), + ) ); diff --git a/config/slm_queue.global.php.dist b/config/slm_queue.global.php.dist index 05b859b..b678403 100644 --- a/config/slm_queue.global.php.dist +++ b/config/slm_queue.global.php.dist @@ -11,17 +11,24 @@ return array( * Parameters for the worker. It defines some criterias that can be reached before the * worker stops to process any other jobs */ - 'worker' => array( - /** - * Specify how many jobs can be processed by a worker until it stops (default to 100 000) - */ - // 'max_runs' => 100000, + 'worker' => array( + /** + * Specify how many jobs can be processed by a worker until it stops (default to 100 000) + */ + // 'max_runs' => 100000, - /** - * Specifiy the max memory (in bytes) that can be used by the worker before it stops (default to 100 MB) - */ - // 'max_memory' => 100 * 1024 * 1024 - ), + /** + * Specifiy the max memory (in bytes) that can be used by the worker before it stops (default to 100 MB) + */ + // 'max_memory' => 100 * 1024 * 1024 + ), + + /** + * Allow to configure a specific queue. + * + * Available options depends on the queue factory + */ + 'queues' => array(), /** * Allow to configure dependencies for jobs that are pulled from any queue. This works like any other diff --git a/src/SlmQueue/Queue/AbstractQueue.php b/src/SlmQueue/Queue/AbstractQueue.php index 193ec17..a765e34 100644 --- a/src/SlmQueue/Queue/AbstractQueue.php +++ b/src/SlmQueue/Queue/AbstractQueue.php @@ -57,8 +57,9 @@ public function createJob($className, $content = null, array $metadata = array() { /** @var $job \SlmQueue\Job\JobInterface */ $job = $this->jobPluginManager->get($className); - $job->setContent(unserialize($content)) - ->setMetadata($metadata); + + $job->setContent(unserialize($content)); + $job->setMetadata($metadata); return $job; } diff --git a/src/SlmQueue/Worker/AbstractWorker.php b/src/SlmQueue/Worker/AbstractWorker.php index b4f468d..659f0ad 100644 --- a/src/SlmQueue/Worker/AbstractWorker.php +++ b/src/SlmQueue/Worker/AbstractWorker.php @@ -36,7 +36,6 @@ abstract class AbstractWorker implements WorkerInterface, EventManagerAwareInter */ protected $options; - /** * Constructor * diff --git a/tests/SlmQueueTest/Job/JobTest.php b/tests/SlmQueueTest/Job/JobTest.php index e7572fd..9a37b38 100644 --- a/tests/SlmQueueTest/Job/JobTest.php +++ b/tests/SlmQueueTest/Job/JobTest.php @@ -20,7 +20,7 @@ public function testCorrectlySerializeJobContent() $job = new SimpleJob(); $job->setContent('Foo'); - $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":"Foo","metadata":[]}', $job->jsonSerialize()); + $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":[]}', $job->jsonSerialize()); } public function testCorrectlySerializeJobMetadata() @@ -28,7 +28,7 @@ public function testCorrectlySerializeJobMetadata() $job = new SimpleJob(); $job->setMetadata('foo', 'Bar'); - $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":null,"metadata":{"foo":"Bar"}}', $job->jsonSerialize()); + $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":"N;","metadata":{"foo":"Bar"}}', $job->jsonSerialize()); } public function testCorrectlySerializeJobContentAndMetadata() @@ -37,7 +37,7 @@ public function testCorrectlySerializeJobContentAndMetadata() $job->setContent('Foo'); $job->setMetadata('foo', 'Bar'); - $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":"Foo","metadata":{"foo":"Bar"}}', $job->jsonSerialize()); + $this->assertEquals('{"class":"SlmQueueTest\\\Asset\\\SimpleJob","content":"s:3:\"Foo\";","metadata":{"foo":"Bar"}}', $job->jsonSerialize()); } public function testCorrectlyUnserializeJob() @@ -47,7 +47,7 @@ public function testCorrectlyUnserializeJob() $job = json_decode($job->jsonSerialize(), true); $this->assertEquals('SlmQueueTest\Asset\SimpleJob', $job['class']); - $this->assertEquals('Foo', $job['content']); + $this->assertEquals('Foo', unserialize($job['content'])); } public function testJobCanBeExecuted() diff --git a/tests/SlmQueueTest/Options/WorkerOptionsTest.php b/tests/SlmQueueTest/Options/WorkerOptionsTest.php index ad2a4e4..c627f08 100644 --- a/tests/SlmQueueTest/Options/WorkerOptionsTest.php +++ b/tests/SlmQueueTest/Options/WorkerOptionsTest.php @@ -3,29 +3,19 @@ namespace SlmQueueTest\Options; use PHPUnit_Framework_TestCase as TestCase; -use SlmQueueTest\Util\ServiceManagerFactory; -use Zend\ServiceManager\ServiceManager; +use SlmQueue\Options\WorkerOptions; class WorkerOptionsTest extends TestCase { - /** - * @var ServiceManager - */ - protected $serviceManager; - - public function setUp() - { - parent::setUp(); - $this->serviceManager = ServiceManagerFactory::getServiceManager(); - } - - public function testCreateWorkerOptions() + public function testGettersAndSetters() { - /** @var $workerOptions \SlmQueue\Options\WorkerOptions */ - $workerOptions = $this->serviceManager->get('SlmQueue\Options\WorkerOptions'); + $workerOptions = new WorkerOptions(array( + 'max_runs' => 10, + 'max_memory' => 1000 + )); $this->assertInstanceOf('SlmQueue\Options\WorkerOptions', $workerOptions); - $this->assertEquals(100000, $workerOptions->getMaxRuns()); - $this->assertEquals(104857600, $workerOptions->getMaxMemory()); + $this->assertEquals(10, $workerOptions->getMaxRuns()); + $this->assertEquals(1000, $workerOptions->getMaxMemory()); } } diff --git a/tests/SlmQueueTest/Queue/QueueTest.php b/tests/SlmQueueTest/Queue/QueueTest.php index 98cd3a3..d2be481 100644 --- a/tests/SlmQueueTest/Queue/QueueTest.php +++ b/tests/SlmQueueTest/Queue/QueueTest.php @@ -2,6 +2,7 @@ namespace SlmQueueTest\Job; +use DateTime; use PHPUnit_Framework_TestCase as TestCase; use SlmQueueTest\Asset\SimpleQueue; use SlmQueueTest\Asset\SimpleJob; @@ -61,7 +62,7 @@ public function testCanCreateJobWithContent() ->will($this->returnValue($job)); $queue = new SimpleQueue('queue', $jobPluginManager); - $result = $queue->createJob('SimpleJob', 'Foo'); + $result = $queue->createJob('SimpleJob', serialize('Foo')); } public function testCanCreateJobWithMetadata()