示例#1
3
 /**
  * Count the current number of messages on the queue.
  *
  * @param $queue Queue Name
  *
  * @return int Count
  */
 public function getMessagesCurrentCountOnQueue($queue)
 {
     try {
         return $this->queue->statsTube($queue)['current-jobs-ready'];
     } catch (ConnectionException $e) {
         \PHPUnit_Framework_Assert::fail("queue [{$queue}] not found");
     }
 }
示例#2
0
 protected function setUp()
 {
     $pheanstalk = new Pheanstalk(getenv('BEANSTALK_HOST'));
     if (!$pheanstalk->getConnection()->isServiceListening()) {
         $this->markTestSkipped('Beanstalk service is not available.');
     }
 }
示例#3
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();
     }
 }
示例#4
0
 /**
  * {@inheritDoc}
  */
 public function putInTube($tube, $data, $priority = PheanstalkInterface::DEFAULT_PRIORITY, $delay = PheanstalkInterface::DEFAULT_DELAY, $ttr = PheanstalkInterface::DEFAULT_TTR)
 {
     try {
         $this->_beanstalk->useTube($tube);
         return $this->put($data, $priority, $delay, $ttr);
     } catch (ConnectionException $e) {
         Yii::error($e);
         return false;
     }
 }
示例#5
0
 /**
  * @inheritdoc
  */
 public function put(AbstractTube $tube, $data)
 {
     $workload = array('tube' => $tube->getName(), 'data' => $tube->getDataTransformer()->sleepData($data));
     $success = false;
     try {
         $success = $this->pheanstalk->put(json_encode($workload), $tube->getPriority(), $tube->getDelay(), $tube->getTtr());
     } catch (\Pheanstalk\Exception $e) {
         $this->fallBack($tube, $data);
     }
     return $success;
 }
示例#6
0
 /** {@inheritdoc} */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $srcHost = $input->getArgument(self::ATTR_SRC_HOST);
     $dstHost = $input->getArgument(self::ATTR_DST_HOST);
     $srcPort = $input->getOption(self::OPT_SRC_PORT);
     $dstPort = $input->getOption(self::OPT_DST_PORT);
     $src = new Pheanstalk($srcHost, $srcPort);
     $dst = new Pheanstalk($dstHost, $dstPort);
     foreach ($src->listTubes() as $tube) {
         var_dump($tube);
     }
 }
示例#7
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);
 }
示例#8
0
 /**
  * 获取队列数量
  *
  * @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);
 }
 /**
  * @param Pheanstalk $transport
  * @param null $tube
  * @param MailWrappedMessage|null $message
  * @return int
  * @throws MailWrapperSendException
  * @throws MailWrapperSetupException
  */
 public static function store(Pheanstalk $transport, $tube = null, MailWrappedMessage $message = null)
 {
     if (!$message || !$message instanceof MailWrappedMessage) {
         throw new MailWrapperSetupException('No Message');
     }
     if (!is_string($tube)) {
         throw new MailWrapperSetupException('No Tube');
     }
     try {
         $storageId = $transport->putInTube($tube, $message);
         return $storageId;
     } catch (\Exception $exception) {
         throw new MailWrapperSendException('Message store failure');
     }
 }
 /**
  * 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');
 }
示例#11
0
 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']);
 }
示例#12
0
 public function work()
 {
     foreach ($this->limits as $index => $limit) {
         $counterName = "queue:{$this->tube}:throttle:{$index}";
         $started = (bool) $this->redis->setnx($counterName, $limit['requests']);
         $counter = $this->redis->get($counterName);
         if ($started) {
             $this->redis->transaction(function (MultiExec $transaction) use($counterName, $limit) {
                 $transaction->expire($counterName, $limit['seconds']);
                 $transaction->decr($counterName);
             });
         } else {
             if ($counter > 1) {
                 $this->redis->decr($counterName);
             } else {
                 $this->pheanstalk->pauseTube($this->tube, $this->redis->ttl($counterName));
             }
         }
     }
 }
示例#13
0
 private function generateStatsTable(Pheanstalk $pheanstalk, $tube = null)
 {
     if ($tube) {
         $tubes[] = $tube;
     } else {
         $tubes = $pheanstalk->listTubes();
     }
     $stats = array();
     foreach ($tubes as $tube) {
         $stats[$tube] = (array) $pheanstalk->statsTube($tube);
     }
     $statsToDisplay = array('current-jobs-urgent', 'current-jobs-ready', 'current-jobs-reserved', 'current-jobs-delayed', 'current-jobs-buried', 'current-waiting', 'total-jobs');
     foreach ($stats as &$tubeStats) {
         foreach ($tubeStats as $key => $val) {
             if (!in_array($key, $statsToDisplay)) {
                 unset($tubeStats[$key]);
             }
         }
     }
     return $stats;
 }
示例#14
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;
 }
示例#15
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;
 }
示例#16
0
 /** {@inheritdoc} */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $srcHost = $input->getArgument(self::ATTR_HOST);
     $srcPort = $input->getOption(self::OPT_PORT);
     $sort = $input->getOption(self::OPT_SORT);
     $order = $input->getOption(self::OPT_ORDER);
     $noZeros = $input->getOption(self::OPT_NO_ZEROS);
     $columns = ['name' => 'name', 'current-jobs-ready' => 'ready', 'current-jobs-reserved' => 'reserved', 'current-jobs-delayed' => 'delayed', 'current-jobs-buried' => 'buried'];
     $src = new Pheanstalk($srcHost, $srcPort);
     $table = new TableHelper(false);
     $table->setLayout(TableHelper::LAYOUT_BORDERLESS);
     $table->setHeaders($columns);
     $tubeNames = $src->listTubes();
     ksort($tubeNames);
     $data = [];
     foreach ($tubeNames as $tube) {
         /** @var ArrayResponse $response */
         $response = $src->statsTube($tube);
         $tubeData = $response->getArrayCopy();
         $tubeData = array_intersect_key($tubeData, $columns);
         if ($noZeros) {
             foreach ($tubeData as $key => $value) {
                 if ('0' === $value) {
                     $tubeData[$key] = '';
                 }
             }
         }
         $data[] = $tubeData;
     }
     $column = array_search($sort, $columns);
     uasort($data, function (array $a1, array $a2) use($column, $order) {
         return strnatcmp($a1[$column], $a2[$column]) * $order;
     });
     $table->addRows($data);
     $table->render($output);
 }
示例#17
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());
     }
 }
示例#18
0
 /**
  *
  * @return number
  */
 public function is_message($queue = '')
 {
     try {
         if ($queue) {
             $result = $this->beanstalk->statsTube($queue);
         } else {
             $result = $this->beanstalk->stats();
         }
         $messageCount = 0;
         foreach ($result as $key => $val) {
             if (in_array($key, ['current-jobs-urgent', 'current-jobs-ready', 'current-jobs-reserved', 'current-jobs-delayed'])) {
                 $messageCount += $val;
             }
         }
         return $messageCount;
     } catch (ServerException $e) {
         return 1;
     }
 }
 /**
  * @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) {
     }
 }
 /**
  * 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) {
     }
 }
示例#21
0
 /**
  * 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);
     }
 }
示例#22
0
 /**
  * @inheritdoc
  */
 public function processed($job)
 {
     return (bool) $this->beanstalk->delete($job);
 }
示例#23
0
 /**
  * Delete a message from the Beanstalk queue.
  *
  * @param string $queue        	
  * @param string $id        	
  * @return void
  */
 public function deleteMessage($queue, $id)
 {
     $this->pheanstalk->useTube($this->getQueue($queue))->delete($id);
 }
 /**
  * @test
  */
 public function submitSetsDefaultTtrIfNotSpecified()
 {
     $this->mockClient->expects($this->once())->method('putInTube')->with(new \PHPUnit_Framework_Constraint_IsAnything(), new \PHPUnit_Framework_Constraint_IsAnything(), new \PHPUnit_Framework_Constraint_IsAnything(), new \PHPUnit_Framework_Constraint_IsAnything(), PheanstalkInterface::DEFAULT_TTR);
     $this->beanstalkdQueue->submit('somePayload');
 }
示例#25
0
 /**
  * Get the number of times the job has been attempted.
  *
  * @return int
  */
 public function attempts()
 {
     $stats = $this->pheanstalk->statsJob($this->job);
     return (int) $stats->reserves;
 }
 /**
  *
  * @param string $identifier
  * @return Message
  */
 public function getMessage($identifier)
 {
     $pheanstalkJob = $this->client->peek($identifier);
     return $this->decodeMessage($pheanstalkJob->getData());
 }
<?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;
    }
}
示例#28
0
<?php

/**
 * This file describes a Pheanstalk stats client.
 */
require_once dirname(__FILE__) . "/vendor/autoload.php";
use Pheanstalk\Pheanstalk;
// Create a new connection to pheanstalk.
$pheanstalk = new Pheanstalk('127.0.0.1');
// Grab stats
$stats = $pheanstalk->stats();
print_r($stats);
示例#29
0
 public function testProcessed()
 {
     $job = new \stdClass();
     $this->beanstalk->expects($this->once())->method('delete')->with($job)->willReturn(true);
     $this->assertTrue($this->driver->processed($job));
 }
示例#30
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);
}