public function run() { $endTime = time() + $this->lifetime; $this->logger->info(sprintf('Started listening with a lifetime of %d seconds.', $this->lifetime)); while (time() < $endTime) { try { /** @var \Pheanstalk\Job $job */ $job = $this->pheanstalk->reserve(1); } catch (Exception $exception) { } if ($job) { $this->logger->info(sprintf('Reserved job #%d: %s', $job->getId(), $job->getData())); try { $data = json_decode($job->getData(), true); /** @var WorkerInterface $worker */ $worker = $this->workerManager->get($data['worker'], $data['params']); $worker->run(new Context($this, $this->logger, $data['params'])); $this->logger->info(sprintf('Finished job #%d', $job->getId())); $this->pheanstalk->delete($job); } catch (Exception $exception) { $this->logger->emerg('Failed to execute job #' . $job->getId(), ['exception' => $exception]); $this->pheanstalk->bury($job); } } usleep($this->interval); } }
/** * {@inheritDoc} */ public function delete($job) { if ($this->dispatcher) { $this->dispatcher->dispatch(CommandEvent::DELETE, new CommandEvent($this, ['job' => $job])); } $this->pheanstalk->delete($job); return $this; }
private function doClear($tubeName, $state) { try { while ($item = $this->pheanstalk->{'peek' . $state}($tubeName)) { $this->pheanstalk->delete($item); } } catch (ServerException $e) { } }
/** * Watch for jobs on the given tube * * @return void */ public function watchForJobs() { // Time the worker will retire $retireTime = time() + $this->ttl; if (null !== $this->tube) { $this->pheanstalk->watchOnly($this->tube); } // Watch the Queue while (!$this->isTerminated()) { $job = $this->pheanstalk->reserve(5); if ($job) { // Let everyone know we just grabbed a job off the queue $this->output->writeln('<comment>Found Job ID: ' . $job->getId() . '</comment>'); // Check the data is valid for us to process a job if (!$this->isValid($job)) { $this->output->writeln('<comment>Invalid Job, skipping.</comment>'); $outcome = self::ACTION_BURY; } else { // Output to let anyone watching know that we're starting a worker $this->output->writeln('<comment>' . $this->getStartMessage($job) . '</comment>'); try { // Process the job $outcome = $this->processJob($job); } catch (\Exception $e) { // Output error $this->output->writeln('<error>Fatal Error: ' . $e->getMessage() . '</error>'); // Bury the job $this->pheanstalk->bury($job); // Break out of while loop break; } // Let the folks know we've completed it $this->output->writeln('<comment>Job Processed.</comment>'); } switch ($outcome) { case self::ACTION_DELETE: // Remove the job from the queue $this->pheanstalk->delete($job); break; case self::ACTION_BURY: // Remove the job from the queue $this->pheanstalk->bury($job); break; case self::ACTION_RELEASE: // Remove the job from the queue $this->pheanstalk->release($job); break; } $this->output->writeln('<info>Waiting for next job...</info>'); } // Check if it's time to retire the worker if (0 !== $this->ttl && time() > $retireTime) { $this->retire(); } } $this->output->writeln('<info>Exiting.</info>'); }
/** * Clears a specific state * * @param string $queue * @param string $state * * @return boolean * * @codeCoverageIgnore */ protected function doClear($queue, $state) { try { while ($item = $this->pheanstalk->{'peek' . $state}($queue)) { $this->pheanstalk->delete($item); } } catch (ServerException $e) { } return true; }
/** * Delete given job and create new job with old or passed params * * @param int $jobId * @param int $priority * @param int $delay */ public function reschedule($jobId, $priority = NULL, $delay = NULL) { /** @var Job $job */ $job = $this->pheanstalk->peek($jobId); $jobStats = $this->pheanstalk->statsJob($job); $this->pheanstalk->useTube($this->tube); $priority = NULL === $priority ? $jobStats['pri'] : $priority; $delay = NULL === $delay ? $jobStats['delay'] : $delay; $newJobId = $this->pheanstalk->put($job->getData(), $priority, $delay, $jobStats['ttr']); $this->pheanstalk->delete($job); $context = ['job_id' => $newJobId, 'old_job_id' => $jobId, 'text' => $job->getData(), 'priority' => $priority, 'delay' => $delay]; $this->logger->info(sprintf('Reschedule job (%d => %d): %s', $jobId, $newJobId, $job->getData()), $context); }
/** * {@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); }
/** * Acknowledge a job * * @access public * @param Job $job * @return $this */ public function completed(Job $job) { $beanstalkJob = new BeanstalkJob($job->getId(), $job->serialize()); $this->beanstalk->delete($beanstalkJob); return $this; }
/** * Permanently deletes a job. * * @param Job $job */ public function delete(Job $job) { $this->pheanstalk->delete($job); $this->logJob($job->getId(), 'Job deleted'); }
public function removeJob($queueName, JobInterface $job) { $this->pheanstalk->delete(new Job($job->getData()['_beanstalk_id'], [])); }
/** * {@inheritdoc} */ public function delete() { return $this->client->delete($this->job); }