Some refinements

This commit is contained in:
Holger Reinhardt 2016-07-29 09:17:13 +02:00
parent 7bb3c9ef85
commit b60fb0d712
4 changed files with 110 additions and 13 deletions

View file

@ -1,7 +1,8 @@
<?php
namespace Hlgrrnhrdt\Resque;
use Resque as PhpResque;
use Resque;
use RuntimeException;
/**
* Resque
@ -15,19 +16,73 @@ class ResqueManager
*/
private $resque;
public function __construct(PhpResque $resque)
public function __construct(Resque $resque)
{
$this->resque = $resque;
}
public function enqueue(Job $job)
/**
* @param Job $job
* @param bool $trackStatus
*
* @return null|\Resque_Job_Status
*/
public function enqueue(Job $job, $trackStatus = false)
{
$this->resque->enqueue($job->queue(), get_class($job), $job->arguments());
$id = $this->resque->enqueue($job->queue(), get_class($job), $job->arguments(), $trackStatus);
if (true === $trackStatus) {
return new \Resque_Job_Status($id);
}
return null;
}
public function enqueueOnce(Job $job)
/**
* @param Job $job
* @param bool $trackStatus
*
* @return null|\Resque_Job_Status
*/
public function enqueueOnce(Job $job, $trackStatus = false)
{
$queue = new Queue($job->queue());
foreach ($queue->jobs() as $queuedJob) {
if ($job->payload['class'] === get_class($queuedJob) && count(array_intersect($queuedJob->getArguments(),
$job->arguments())) === $job->arguments()
) {
return ($trackStatus) ? new \Resque_Job_Status($job->payload['id']) : null;
}
}
return $this->enqueue($job, $trackStatus);
}
/**
* @return \Resque_Redis
*/
public function redis()
{
return $this->resque->redis();
}
/**
* @return int
*
* @throws RuntimeException
*/
public function fork()
{
if (false === function_exists('pcntl_fork')) {
return -1;
}
$pid = pcntl_fork();
if (-1 === $pid) {
throw new RuntimeException('Unable to fork child worker.');
}
return $pid;
}
}