public function release(array $message, $delay = PheanstalkInterface::DEFAULT_DELAY) { $this->validateMessage($message); $this->beanstalkd->useTube($message['queue']); $job = new Job($message['id'], $message['body']); return $this->beanstalkd->release($job, PheanstalkInterface::DEFAULT_PRIORITY, $delay); }
/** * @inheritdoc */ public function release($messageId, array $options = []) { $pheanstalkJob = $this->client->peek((int) $messageId); $priority = isset($options['priority']) ? $options['priority'] : PheanstalkInterface::DEFAULT_PRIORITY; $delay = isset($options['delay']) ? $options['delay'] : PheanstalkInterface::DEFAULT_DELAY; $this->client->release($pheanstalkJob, $priority, $delay); }
/** * Start the worker. */ public function startWorker() { $this->pheanstalk->watch($this->queue); $this->pheanstalk->ignore('default'); $buildStore = Factory::getStore('Build'); while ($this->run) { // Get a job from the queue: $job = $this->pheanstalk->reserve(); $this->checkJobLimit(); // Get the job data and run the job: $jobData = json_decode($job->getData(), true); if (!$this->verifyJob($job, $jobData)) { continue; } $this->logger->addInfo('Received build #' . $jobData['build_id'] . ' from Beanstalkd'); // If the job comes with config data, reset our config and database connections // and then make sure we kill the worker afterwards: if (!empty($jobData['config'])) { $this->logger->addDebug('Using job-specific config.'); $currentConfig = Config::getInstance()->getArray(); $config = new Config($jobData['config']); Database::reset($config); } try { $build = BuildFactory::getBuildById($jobData['build_id']); } catch (\Exception $ex) { $this->logger->addWarning('Build #' . $jobData['build_id'] . ' does not exist in the database.'); $this->pheanstalk->delete($job); } try { // Logging relevant to this build should be stored // against the build itself. $buildDbLog = new BuildDBLogHandler($build, Logger::INFO); $this->logger->pushHandler($buildDbLog); $builder = new Builder($build, $this->logger); $builder->execute(); // After execution we no longer want to record the information // back to this specific build so the handler should be removed. $this->logger->popHandler($buildDbLog); } catch (\PDOException $ex) { // If we've caught a PDO Exception, it is probably not the fault of the build, but of a failed // connection or similar. Release the job and kill the worker. $this->run = false; $this->pheanstalk->release($job); } catch (\Exception $ex) { $build->setStatus(Build::STATUS_FAILED); $build->setFinished(new \DateTime()); $build->setLog($build->getLog() . PHP_EOL . PHP_EOL . $ex->getMessage()); $buildStore->save($build); $build->sendStatusPostback(); } // Reset the config back to how it was prior to running this job: if (!empty($currentConfig)) { $config = new Config($currentConfig); Database::reset($config); } // Delete the job when we're done: $this->pheanstalk->delete($job); } }
/** * @return int Status code to exit with */ public function run() { $env = $this->context->env; $tube = $env->get('BEANSTALKD_TUBE') ?: 'default'; $class = $env->get('BEANSTALKD_CONSUMER'); if (!$class) { $this->stdio->outln('<<red>>BEANSTALKD_CONSUMER environmental variable is not set<<reset>>'); return Status::USAGE; } if (!class_exists($class)) { $this->stdio->outln(sprintf('<<red>>BEANSTALKD_CONSUMER does not reference a locatable class: %s<<reset>>', $class)); return Status::DATAERR; } if (!in_array(ConsumerInterface::class, class_implements($class))) { $this->stdio->outln(sprintf('<<red>>BEANSTALKD_CONSUMER references a class that does not implement ConsumerInterface: %s<<reset>>', $class)); return Status::DATAERR; } $this->pheanstalk->watchOnly($tube); $consumer = call_user_func($this->resolver, $class); while (call_user_func($this->listener)) { $reserved = $this->pheanstalk->reserve(); if (!$reserved) { continue; } $job = new Job($reserved->getId(), $reserved->getData()); try { $result = $consumer->consume($job); if ($result === false) { $this->pheanstalk->release($job); continue; } } catch (\Exception $e) { $this->pheanstalk->release($job); throw $e; } $this->pheanstalk->delete($job); } return Status::SUCCESS; }
/** * @inheritdoc */ public function release(array $message, $delay = 0) { $priority = PheanstalkInterface::DEFAULT_PRIORITY; $ttr = PheanstalkInterface::DEFAULT_TTR; if (isset($message['priority'])) { $priority = $message['priority']; unset($message['priority']); } if (isset($message['ttr'])) { $ttr = $message['ttr']; unset($message['ttr']); } $this->beanstalk->release($message, $priority, $delay); }
/** * Release the job back into the queue. * * @param int $delay * @return void */ public function release($delay = 0) { parent::release($delay); $priority = Pheanstalk::DEFAULT_PRIORITY; $this->pheanstalk->release($this->job, $priority, $delay); }
/** * Valid options are: * - priority: the lower the priority is, the sooner the job get popped from the queue (default to 1024) * - delay: the delay in seconds before a job become available to be popped (default to 0 - no delay -) * * {@inheritDoc} */ public function release(JobInterface $job, array $options = array()) { $this->pheanstalk->release($job, isset($options['priority']) ? $options['priority'] : Pheanstalk::DEFAULT_PRIORITY, isset($options['delay']) ? $options['delay'] : Pheanstalk::DEFAULT_DELAY); }