Check jobs currently worked on for duplicates

This commit is contained in:
Holger Reinhardt 2017-09-13 17:16:46 +02:00
parent fe1cb573bf
commit 591012d123

View File

@ -31,23 +31,6 @@ class Resque
return $this;
}
/**
* @param Job $job
* @param bool $trackStatus
*
* @return null|\Resque_Job_Status
*/
public function enqueue(Job $job, $trackStatus = false)
{
$id = \Resque::enqueue($job->queue(), $job->name(), $job->arguments(), $trackStatus);
if (true === $trackStatus) {
return new \Resque_Job_Status($id);
}
return null;
}
/**
* @param Job $job
* @param bool $trackStatus
@ -64,9 +47,50 @@ class Resque
}
}
foreach ($this->working() as $workingJob) {
if (true === $job->equals($workingJob)) {
return ($trackStatus) ? new \Resque_Job_Status($workingJob->job->payload['id']) : null;
}
}
return $this->enqueue($job, $trackStatus);
}
/**
* @return Job[]
*/
private function working()
{
$jobs = [];
foreach (\Resque::redis()->smembers('workers') as $worker) {
$job = \Resque::redis()->get('worker:' . $worker);
$job = \json_decode($job, true);
if (!\json_last_error()) {
$jobs[] = (new \Resque_Job($job['queue'], $job['payload']))
->getInstance();
}
}
return $jobs;
}
/**
* @param Job $job
* @param bool $trackStatus
*
* @return null|\Resque_Job_Status
*/
public function enqueue(Job $job, $trackStatus = false)
{
$id = \Resque::enqueue($job->queue(), $job->name(), $job->arguments(), $trackStatus);
if (true === $trackStatus) {
return new \Resque_Job_Status($id);
}
return null;
}
/**
* @return \Resque_Redis
*/