/** * Schedule a job in the future * * @access public * @param Job $job * @param DateTime $dateTime * @return $this */ public function schedule(Job $job, DateTime $dateTime) { $now = new DateTime(); $when = clone $dateTime; $delay = $when->getTimestamp() - $now->getTimestamp(); $this->beanstalk->putInTube($this->queueName, $job->serialize(), Pheanstalk::DEFAULT_PRIORITY, $delay); return $this; }
/** * {@inheritDoc} */ public function putInTube($tube, $data, $priority = self::DEFAULT_PRIORITY, $delay = self::DEFAULT_DELAY, $ttr = self::DEFAULT_TTR) { if ($this->dispatcher) { $this->dispatcher->dispatch(CommandEvent::PUT_IN_TUBE, new CommandEvent($this, ['tube' => $tube, 'data' => $data, 'priority' => $priority, 'delay' => $delay, 'ttr' => $ttr])); } return $this->pheanstalk->putInTube($tube, $data, $priority, $delay, $ttr); }
/** * Add a job to the queue. * * @param string $action The action * @param array $payload The job's payload * @param string|int $delay The delay after which the job can be reserved. * Can be a number of seconds, or a date-diff * string relative from now, like "10 seconds". * @param int $priority From 0 (most urgent) to 0xFFFFFFFF (least urgent) * @param int $ttr Time To Run: seconds a job can be reserved for * * @throws \InvalidArgumentException When the action is not defined * @throws \InvalidArgumentException When `$delay` or `$priority` is negative * * @return int The job id */ public function add($action, array $payload, $delay = null, $priority = null, $ttr = null) { if (false === $this->hasExecutor($action)) { throw new \InvalidArgumentException(sprintf('Action "%s" is not defined in QueueManager', $action)); } if (null === $delay) { $delay = PheanstalkInterface::DEFAULT_DELAY; } if (null === $priority) { $priority = PheanstalkInterface::DEFAULT_PRIORITY; } if (null === $ttr) { $ttr = $this->defaultTtr; } if (!is_numeric($delay)) { $delay = strtotime(sprintf('+ %s', $delay)) - time(); } if ($delay < 0) { throw new \InvalidArgumentException(sprintf('You cannot schedule a job in the past (delay was %d)', $delay)); } if ($priority < 0) { throw new \InvalidArgumentException(sprintf('The priority for a job cannot be negative (was %d)', $priority)); } $payload = json_encode($payload); $jobId = $this->pheanstalk->putInTube($action, $payload, $priority, $delay, $ttr); $this->logJob($jobId, sprintf('Added job in tube "%s" with: payload: %s, priority: %d, delay: %ds, ttr: %s', $action, $payload, $priority, $delay, $ttr)); return $jobId; }
/** * {@inheritdoc} */ public function retry($queueName, Envelope $env) { $e = $this->assurePheanstalkEnvelope($env)->retry(); $data = $this->serialize($e); // since we need to update the job payload here, we have to delete // it and re-add it manually. This isn't transational, so there's // a (very real) possiblity of data loss. try { $this->conn->delete($env->getJob()); $id = $this->conn->putInTube($queueName, $data, $this->options['retry-priority'], $this->options['retry-delay'], $this->options['retry-ttr']); } catch (\Pheanstalk\Exception $e) { throw PheanstalkError::fromException($e); } return new PheanstalkEnvelope(new Job($id, $data), $e); }
/** * {@inheritdoc} */ public function retry($queueName, Envelope $env) { if (!$env instanceof PheanstalkEnvelope) { throw new InvalidEnvelope(sprintf('%s requires that envelopes be instances of "%s", got "%s"', __CLASS__, PheanstalkEnvelope::class, get_class($env))); } $e = $env->retry(); $data = $this->serialize($e); // since we need to update the job payload here, we have to delete // it and re-add it manually. This isn't transational, so there's // a (very real) possiblity of data loss. try { $this->conn->delete($env->getJob()); $id = $this->conn->putInTube($queueName, $data, $this->options['retry-priority'], $this->options['retry-delay'], $this->options['retry-ttr']); } catch (\Pheanstalk\Exception $e) { throw PheanstalkError::fromException($e); } return new PheanstalkEnvelope(new Job($id, $data), $e); }
/** * {@inheritdoc} */ public function push($queue, Job $job) { $options = $this->options + $job->getOptions(); return $this->pheanstalk->putInTube($queue, json_encode($job->createPayload()), $options['priority'], $options['delay'], $options['timeout']); }
/** * {@inheritdoc} */ public function push($item, $eta = null) { $this->pheanstalk->putInTube($this->tubeName, $item, PheanstalkInterface::DEFAULT_PRIORITY, QueueUtils::calculateDelay($eta)); }