예제 #1
0
 /**
  * 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();
     }
 }
 /**
  * Mark a message as finished
  *
  * @param Message $message
  * @return boolean TRUE if the message could be removed
  */
 public function finish(Message $message)
 {
     $messageIdentifier = $message->getIdentifier();
     $pheanstalkJob = $this->client->peek($messageIdentifier);
     $this->client->delete($pheanstalkJob);
     $message->setState(Message::STATE_DONE);
     return TRUE;
 }
예제 #3
0
파일: Worker.php 프로젝트: dw250100785/Octo
 /**
  * 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);
     }
 }
예제 #4
0
파일: Manager.php 프로젝트: block8/octo
 public static function delete(Job $job)
 {
     if ($job->getQueueId()) {
         $pheanstalk = new Pheanstalk(Config::getInstance()->get('Octo.worker.host'));
         try {
             $beanstalkJob = $pheanstalk->peek($job->getQueueId());
             $pheanstalk->delete($beanstalkJob);
         } catch (\Exception $ex) {
             // Job is not in beanstalk.
         }
     }
     self::getStore()->delete($job);
 }
예제 #5
0
 /**
  * Checks that the job received is actually from PHPCI, and has a valid type.
  * @param Job $job
  * @param $jobData
  * @return bool
  */
 protected function verifyJob(Job $job, $jobData)
 {
     if (empty($jobData) || !is_array($jobData)) {
         // Probably not from PHPCI.
         $this->pheanstalk->delete($job);
         return false;
     }
     if (!array_key_exists('type', $jobData) || $jobData['type'] !== 'phpci.build') {
         // Probably not from PHPCI.
         $this->pheanstalk->delete($job);
         return false;
     }
     return true;
 }
 /**
  * Set up dependencies
  */
 public function setUp()
 {
     parent::setUp();
     $configurationManager = $this->objectManager->get('TYPO3\\Flow\\Configuration\\ConfigurationManager');
     $settings = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'TYPO3.Jobqueue.Beanstalkd');
     if (!isset($settings['testing']['enabled']) || $settings['testing']['enabled'] !== TRUE) {
         $this->markTestSkipped('beanstalkd is not configured (TYPO3.Jobqueue.Beanstalkd.testing.enabled != TRUE)');
     }
     $queueName = 'Test-queue';
     $this->queue = new BeanstalkdQueue($queueName, $settings['testing']);
     $clientOptions = $settings['testing']['client'];
     $host = isset($clientOptions['host']) ? $clientOptions['host'] : '127.0.0.1';
     $port = isset($clientOptions['port']) ? $clientOptions['port'] : '11300';
     $this->client = new Pheanstalk($host, $port);
     // flush queue:
     try {
         while (true) {
             $job = $this->client->peekDelayed($queueName);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
     try {
         while (true) {
             $job = $this->client->peekBuried($queueName);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
     try {
         while (true) {
             $job = $this->client->peekReady($queueName);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
 }
예제 #7
0
 /**
  * Delete message from queue
  *
  * @param int $pid
  * @param callable $callback function handler
  * @throws \Deliveries\Exceptions\BrokerException
  * @return boolean
  */
 public function delete($pid = null, callable $callback = null)
 {
     try {
         // get broker job by process id
         $job = $this->broker->peek($pid);
         // remove process
         $this->broker->delete($job);
         if (is_null($callback) === false) {
             $callback();
         }
         return true;
     } catch (Exception $e) {
         throw new BrokerException($e->getMessage());
     }
 }
예제 #8
0
 /**
  * @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 flush()
 {
     try {
         while (true) {
             $job = $this->client->peekDelayed($this->name);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
     try {
         while (true) {
             $job = $this->client->peekBuried($this->name);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
     try {
         while (true) {
             $job = $this->client->peekReady($this->name);
             $this->client->delete($job);
         }
     } catch (\Exception $e) {
     }
 }
예제 #10
0
 /**
  * @inheritdoc
  */
 public function processed($job)
 {
     return (bool) $this->beanstalk->delete($job);
 }
예제 #11
0
 /**
  * Delete the job from the queue.
  *
  * @return void
  */
 public function delete()
 {
     parent::delete();
     $this->pheanstalk->delete($this->job);
 }
예제 #12
0
 public function stopped($job)
 {
     return $this->beanStalkdClient->delete($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;
    }
}
예제 #14
0
<?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);
}
예제 #15
0
 public function clearQueue($queue = 'default')
 {
     while ($job = $this->queue->reserveFromTube($queue, 0)) {
         $this->queue->delete($job);
     }
 }
 /**
  * @inheritdoc
  */
 public function delete(array $message)
 {
     $this->beanstalk->delete($message['message']);
 }
예제 #17
0
 /**
  * @param $jobId
  */
 public function removeJob($jobId)
 {
     $this->connection->delete($this->connection->peek($jobId));
 }
예제 #18
0
 public function purge($queue)
 {
     while ($job = $this->beanstalkd->watch($queue)->ignore("default")->reserve(0)) {
         $this->beanstalkd->delete($job);
     }
 }
예제 #19
0
 /**
  * {@inheritDoc}
  */
 public function delete(JobInterface $job)
 {
     $this->pheanstalk->delete($job);
 }