forked from eventio/bbq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ville Mattila
committed
Jul 20, 2013
1 parent
5c0642e
commit 06dd62b
Showing
8 changed files
with
385 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Eventio\BBQ\Job; | ||
|
||
use Eventio\BBQ\Job\Job; | ||
|
||
/** | ||
* @author Ville Mattila <[email protected]> | ||
*/ | ||
class RedisQueueJob extends Job | ||
{ | ||
|
||
public function __construct($payload, $rawData, $processingKey = null) | ||
{ | ||
parent::__construct($payload); | ||
$this->rawData = $rawData; | ||
$this->processingKey = $processingKey; | ||
} | ||
|
||
protected $rawData; | ||
|
||
public function getRawData() | ||
{ | ||
return $this->rawData; | ||
} | ||
|
||
protected $processingKey; | ||
|
||
public function getProcessingKey() | ||
{ | ||
return $this->processingKey; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Eventio\BBQ\Queue; | ||
|
||
use Eventio\BBQ\Job\JobInterface; | ||
use Eventio\BBQ\Job\Payload\JobPayloadInterface; | ||
use Eventio\BBQ\Job\RedisQueueJob; | ||
|
||
/** | ||
* @author Ville Mattila <[email protected]> | ||
*/ | ||
class RedisQueue extends AbstractQueue | ||
{ | ||
|
||
/** | ||
* @var \Predis\ClientInterface | ||
*/ | ||
protected $predis; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $queueKey; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $processingQueueKeyPrefix; | ||
|
||
public function __construct($id, \Predis\ClientInterface $predis, $queueKey, array $config = array()) | ||
{ | ||
$this->predis = $predis; | ||
$this->queueKey = $queueKey; | ||
|
||
parent::__construct($id, $config); | ||
} | ||
|
||
protected function init() | ||
{ | ||
$this->config = array_merge(array( | ||
'processing_queue_key_prefix' => '%q:%h:%p', | ||
'allow_infinite_blocking' => false, | ||
'skip_shutdown_release' => false | ||
), $this->config); | ||
|
||
$this->processingQueueKeyPrefix = $this->convertProcessingKey($this->config['processing_queue_key_prefix']); | ||
|
||
if ($this->config['skip_shutdown_release'] == false) { | ||
register_shutdown_function(array($this, 'releaseLockedJobs')); | ||
} | ||
} | ||
|
||
private function convertProcessingKey($template) | ||
{ | ||
$replaces = array( | ||
'%q' => $this->queueKey, | ||
'%h' => gethostname(), | ||
'%p' => getmypid(), | ||
); | ||
return str_replace(array_keys($replaces), array_values($replaces), $template); | ||
} | ||
|
||
public function fetchJob($timeout = null) | ||
{ | ||
$processingKey = uniqid($this->processingQueueKeyPrefix); | ||
|
||
if (false === $this->config['allow_infinite_blocking'] && !$timeout) { | ||
$redisJob = $this->predis->rpoplpush($this->queueKey, $processingKey); | ||
} else { | ||
$redisJob = $this->predis->brpoplpush($this->queueKey, $processingKey, $timeout); | ||
} | ||
if (!$redisJob) { | ||
return null; | ||
} | ||
|
||
$job = new RedisQueueJob(unserialize($redisJob), $redisJob, $processingKey); | ||
$job->setQueue($this); | ||
|
||
$this->lockJob($job); | ||
return $job; | ||
} | ||
|
||
public function finalizeJob(JobInterface $job) | ||
{ | ||
// | ||
$this->predis->lrem($job->getProcessingKey(), 0, $job->getRawData()); | ||
$this->deleteLockedJob($job); | ||
} | ||
|
||
public function pushJob(JobPayloadInterface $jobPayload) | ||
{ | ||
// Adds a new job to the main queue | ||
$this->predis->lpush($this->queueKey, serialize($jobPayload)); | ||
} | ||
|
||
public function releaseJob(JobInterface $job) | ||
{ | ||
// Returning the job back to the main queue from the processing queue | ||
$this->predis->rpoplpush($job->getProcessingKey(), $this->queueKey); | ||
$this->deleteLockedJob($job); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Eventio\BBQ\Tests\Functional; | ||
|
||
use Eventio\BBQ; | ||
use Eventio\BBQ\Job\Payload\StringPayload; | ||
use Eventio\BBQ\Queue\RedisQueue; | ||
|
||
/** | ||
* @author Ville Mattila <[email protected]> | ||
*/ | ||
class RedisTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
protected function setUp() | ||
{ | ||
if (!class_exists('\\Predis\\Client')) { | ||
$this->markTestSkipped( | ||
'Predis library is not installed' | ||
); | ||
} | ||
} | ||
|
||
public function createBBQRedisQueue() | ||
{ | ||
$bbq = new BBQ(); | ||
|
||
$predis = new \Predis\Client(); | ||
$bbq->registerQueue(new RedisQueue('queue1', $predis, 'eventio:bbq:queue1')); | ||
|
||
return $bbq; | ||
} | ||
|
||
public function testJob() | ||
{ | ||
|
||
$bbq = $this->createBBQRedisQueue(); | ||
$bbq->pushJob('queue1', new StringPayload('Test 1')); | ||
|
||
unset($bbq); | ||
|
||
$bbq = $this->createBBQRedisQueue(); | ||
$job = $bbq->fetchJob('queue1'); | ||
|
||
$this->assertInstanceOf('\\Eventio\\BBQ\\Job\\RedisQueueJob', $job); | ||
|
||
$payload = $job->getPayload(); | ||
$this->assertEquals('Test 1', (string) $payload); | ||
|
||
$bbq->finalizeJob($job); | ||
} | ||
|
||
} |
Oops, something went wrong.