/** * @param \DelayedJobs\DelayedJob\Job $job * @return \DelayedJobs\DelayedJob\Job|bool */ public function enqueue(Job $job) { $jobId = time() + mt_rand(0, time()); $job->setId($jobId); static::$_jobs[$jobId] = $job; return $job; }
public function persistJob(Job $job) { $job_data = $job->getData(); $job_entity = $this->newEntity($job_data); if (!$job_entity->status) { $job_entity->status = DelayedJob::STATUS_NEW; } $options = ['atomic' => !$this->connection()->inTransaction()]; $result = $this->save($job_entity, $options); if (!$result) { return false; } $job->setId($job_entity->id); return $job; }
public function consume(callable $callback, callable $heartbeat) { $prefix = $this->config('prefix'); $channel = $this->getChannel(); $channel->basic_qos(null, $this->config('qos'), null); $tag = $channel->basic_consume($prefix . 'queue', '', false, false, false, false, function (AMQPMessage $message) use($callback) { $body = json_decode($message->body, true); $job = new Job(); $job->setId($body['id'])->setBrokerMessage($message); return $callback($job, $message->delivery_info['redelivered']); }); $this->_tag = $tag; $time = microtime(true); while (count($channel->callbacks)) { try { $channel->wait(null, false, static::TIMEOUT); } catch (AMQPTimeoutException $e) { $heartbeat(); } catch (AMQPIOWaitException $e) { $heartbeat(); } if (microtime(true) - $time >= static::TIMEOUT) { $heartbeat(); $time = microtime(true); } } }