diff --git a/src/Resque.php b/src/Resque.php index 2dd5b26..1da773a 100644 --- a/src/Resque.php +++ b/src/Resque.php @@ -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 */