Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Serialize content
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jan 9, 2014
2 parents 6e78192 + 43a0324 commit a1d8c9d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 53 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
{
Expand All @@ -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
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -27,10 +32,5 @@
* Queue manager configuration
*/
'queue_manager' => array(),

/**
* Queue configuration options
*/
'queues' => array(),
),
)
);
27 changes: 17 additions & 10 deletions config/slm_queue.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/SlmQueue/Queue/AbstractQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/SlmQueue/Worker/AbstractWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ abstract class AbstractWorker implements WorkerInterface, EventManagerAwareInter
*/
protected $options;


/**
* Constructor
*
Expand Down
8 changes: 4 additions & 4 deletions tests/SlmQueueTest/Job/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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()
{
$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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
26 changes: 8 additions & 18 deletions tests/SlmQueueTest/Options/WorkerOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 2 additions & 1 deletion tests/SlmQueueTest/Queue/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SlmQueueTest\Job;

use DateTime;
use PHPUnit_Framework_TestCase as TestCase;
use SlmQueueTest\Asset\SimpleQueue;
use SlmQueueTest\Asset\SimpleJob;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a1d8c9d

Please sign in to comment.