/** * @return int exit code */ public function listen() { $this->initConnection(); /** @var Job $job */ $job = $this->connection->watch($this->tube)->reserveFromTube($this->tube, $this->timeout); return $job; }
/** * 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); } }
/** * Esegue il worker ad libitum * * @param string $tube * @return void */ public function execute($tube) { $queue = new Pheanstalk($this->host); declare (ticks=1); $tube = !is_null($tube) ? QueueManager::resolveChannelName($tube) : null; $queue->watch($tube); pcntl_signal(SIGTERM, function ($signal) use($tube) { $this->shouldClose = true; if ((int) QueueManager::getTubeStats($tube, 'current-jobs-ready') === 0) { exit; } }); while ($job = $queue->reserve()) { try { $this->log($job, "Starting Job..."); $this->jobRunner->execute($job, $this->logger); } catch (\Exception $e) { $this->log($job, "Job Failed and Buried: " . $e->getMessage(), LoggerInterface::SEVERITY_ERROR); $queue->bury($job); continue; } $this->log($job, "Job Finished Succesfully"); $queue->delete($job); $this->closeIfPossible(); } }
/** * @inheritdoc */ public function purge($queue) { $stat = $this->beanstalk->statsTube($queue); $this->beanstalk->watch($queue); for ($i = 0; $i < $stat->current_jobs_ready; $i++) { $job = $this->beanstalk->peekReady(); $this->beanstalk->delete($job); } for ($i = 0; $i < $stat->current_jobs_delayed; $i++) { $job = $this->beanstalk->peekDelayed(); $this->beanstalk->delete($job); } for ($i = 0; $i < $stat->current_jobs_buried; $i++) { $job = $this->beanstalk->peekBuried(); $this->beanstalk->delete($job); } }
/** * Start the worker. */ public function run() { // Set up pheanstalk: $this->pheanstalk->watch($this->queue); $this->pheanstalk->ignore('default'); while ($this->run) { $beanstalkJob = $this->pheanstalk->reserve(900); $jobData = json_decode($beanstalkJob->getData(), true); // Delete invalid jobs: if (empty($jobData) || !is_array($jobData) || empty($jobData['type'])) { $this->pheanstalk->delete($beanstalkJob); continue; } $job = new Job($jobData); // Delete jobs we don't have handlers for: if (!array_key_exists($job->getType(), $this->handlers)) { $this->pheanstalk->delete($beanstalkJob); Manager::update($job, Job::STATUS_FAILURE, 'No handler exists for this job type.'); continue; } // Try and load the job handler: $handlerClass = $this->handlers[$job->getType()]['handler']; try { $handler = new $handlerClass($job, $this); if (!$handler instanceof Handler) { throw new \Exception('Job handler does not extend \\Octo\\Job\\Handler.'); } Manager::update($job, Job::STATUS_RUNNING); // Try and run the job: $success = $handler->run(); } catch (\Exception $ex) { $this->pheanstalk->delete($beanstalkJob); Manager::update($job, Job::STATUS_FAILURE, $ex->getMessage()); continue; } Manager::update($job, $success ? Job::STATUS_SUCCESS : Job::STATUS_FAILURE, $handler->getMessage()); // Remove the job when we're done: $this->pheanstalk->delete($beanstalkJob); } }
/** * 获取队列数量 * * @param string tube * @param Pheanstalk pheanstalk * @return string|boolean */ public function getJob($tube, Pheanstalk $pheanstalk) { if (!isset($this->jobs[$tube])) { return false; } $timeout = 3; $job = $pheanstalk->watch($tube)->reserve($timeout); if (empty($job) || !is_object($job) || $job->getId() == 0 || empty($job->getData())) { return false; } $data = ['job' => $job, 'handle' => $this->jobs[$tube]]; return serialize($data); }
/** * Test if BeanstalkdPublisher works as expected. */ public function testLogging() { $container = $this->getContainer(); $publisher = $container->get('ongr_task_messenger.publisher.default.beanstalkd'); $logger = new NullLogger(); $publisher->setLogger($logger); $task = new SyncTask(SyncTask::SYNC_TASK_PRESERVEHOST); $task->setName('task_foo'); $task->setCommand('command_foo'); $publisher->publish($task); $pheanstalk = new Pheanstalk($container->getParameter('ongr_task_messenger.publisher.default.beanstalkd.host'), $container->getParameter('ongr_task_messenger.publisher.default.beanstalkd.port')); $job = $pheanstalk->watch('general')->reserve(); $jobData = json_decode($job->getData(), true); $this->assertEquals($jobData['task'], 'ongr.task.task_foo'); $this->assertEquals($jobData['args'][0], 'command_foo -e test'); }
public function createService(ServiceLocatorInterface $serviceLocator) { $workerManager = $serviceLocator->get(WorkerManager::class); $writer = new CsvStream('data/logs/daemon.' . date('Y-m-d') . '.csv'); $writer->setFormatter(new Xml(['rootElement' => 'log'])); $logger = new Logger(); $logger->addWriter(new Stream(fopen('php://output', 'w'))); $logger->addWriter($writer); $config = $serviceLocator->get('Config'); $config = $config['zource_daemon']; $pheanstalk = new Pheanstalk($config['host'], $config['port'], $config['timeout']); foreach ($config['tubes'] as $tube) { $pheanstalk->watch($tube); } $pheanstalk->ignore('default'); return new Daemon($pheanstalk, $workerManager, $logger, $config['lifetime'], $config['interval']); }
/** * @inheritdoc */ public function dequeue($queue) { $job = $this->beanstalk->watch($queue)->ignore('default')->reserve(static::TIMEOUT); $data = $job instanceof Job ? $job->getData() : $job; return [unserialize($data), $job]; }
<?php /** * This file describes a Pheanstalk worker */ require_once dirname(__FILE__) . "/vendor/autoload.php"; use Pheanstalk\Pheanstalk; // Create a new connection to pheanstalk. $pheanstalk = new Pheanstalk('127.0.0.1'); // Watch the test tube. $pheanstalk->watch('test'); // While we can reserve a job... while ($job = $pheanstalk->reserve()) { sleep(5); // Process the data and write it to STDOUT. $data = $job->getData(); echo "{$data} \n"; // Delete the job from the queue. $pheanstalk->delete($job); sleep(5); // If the message was 'kill', exit this loop. if ($data == 'kill') { break; } }
<?php require 'vendor/autoload.php'; use Pheanstalk\Pheanstalk; $stalk = new Pheanstalk('127.0.0.1'); while (1) { $job = $stalk->watch('test')->ignore('default')->reserve(); echo $job->getData(); $stalk->delete($job); }
public function purge($queue) { while ($job = $this->beanstalkd->watch($queue)->ignore("default")->reserve(0)) { $this->beanstalkd->delete($job); } }
<?php use Monolog\Handler\StreamHandler; use Monolog\Logger; use Pheanstalk\Pheanstalk; require 'vendor/autoload.php'; date_default_timezone_set('Europe/Riga'); sleep(10); $conn = new PDO('mysql:host=db;dbname=todo', 'root', 'root'); $pheanstalk = new Pheanstalk('beanstalkd'); $logger = new Monolog\Logger('worker'); $logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO)); $tubeName = 'todotube'; $pheanstalk->watch($tubeName); $logger->addInfo('Started to watch tube ' . $tubeName); $stmt = $conn->prepare('INSERT INTO Todo (text, status, created_at, updated_at) VALUES (?, ?, ?, ?)'); while ($job = $pheanstalk->ignore('default')->reserve()) { $data = json_decode($job->getData(), true); $logger->addInfo('Got data', $data); $stmt->execute([$data['text'], $data['status'], $data['created_at'], '1999-12-12 12:12:12']); $logger->addInfo('Successfully inserted!'); $pheanstalk->delete($job); }
public function getNext() { $this->beanStalkdClient->watch($this->sourceQueue); return $this->beanStalkdClient->reserve(0); }