Esempio n. 1
0
 public static function init(Di $di, $defaultTube = 'default')
 {
     $config = $di->get('config');
     if (!isset($config->queue)) {
         return;
     }
     $params = (array) $config->queue;
     $di->setShared(self::SERVICE_NAME, function () use($params, $defaultTube) {
         $beanstalk = new Beanstalk($params);
         $beanstalk->choose($defaultTube);
         return $beanstalk;
     });
 }
 /**
  * Read message
  *
  * @param array $credentials
  * @throws \Sonar\Exceptions\BeanstalkMapperException
  * @return array
  */
 public function read(array $credentials)
 {
     try {
         while (($job = $this->connect->peekReady()) !== false) {
             $message = unserialize($job->getBody());
             if ($message['hash'] === $credentials['hash']) {
                 // hash found! Remove job from queue and return message
                 $job->delete();
                 unset($message['hash']);
                 return $message;
             }
         }
     } catch (\Exception $e) {
         throw new BeanstalkMapperException($e->getMessage());
     }
 }
Esempio n. 3
0
 /**
  * Removes a job from the server entirely
  *
  * @return boolean
  */
 public function delete()
 {
     $this->_queue->write('delete ' . $this->_id);
     $response = $this->_queue->readStatus();
     if ($response[0] === 'DELETED') {
         return true;
     }
     return false;
 }
Esempio n. 4
0
 /**
  * Handle queue piecemeal for periodic launch with cron 
  *
  * @param integer $limit
  */
 public function handleQueue($limit = 50)
 {
     while ((is_null($limit) || --$limit >= 0) && ($job = $this->queue->peekReady()) !== false) {
         $data = json_decode($job->getBody(), true);
         // 		    $segments = explode(':', $data['job']);
         // 		    if (count($segments) !== 2) continue;
         call_user_func_array([$this, 'sendSwiftMessage'], [unserialize($data['message'])]);
         $job->delete();
     }
 }
Esempio n. 5
0
 public function __construct($options = null)
 {
     parent::__construct($options);
     if (!empty($options['host'])) {
         $this->host = $options['host'];
     }
     if (!empty($options['port'])) {
         $this->port = $options['port'];
     }
     $this->connect();
 }
Esempio n. 6
0
 /**
  * Reserves/locks a ready job from the specified tube.
  *
  * @param  string                               $tube
  * @param  integer                              $timeout
  * @return boolean|\Phalcon\Queue\Beanstalk\Job
  */
 public function reserveFromTube($tube, $timeout = null)
 {
     $this->watch($this->getTubeName($tube));
     return parent::reserve($timeout);
 }
Esempio n. 7
0
#!/usr/bin/env php
<?php 
use Phalcon\Queue\Beanstalk;
use Phalcon\Queue\Beanstalk\Job;
use Phalcon\DI;
use SlowProg\Mailer\MailerService;
// Подключаем конфиг приложения (исправить на свой)
$config = (require __DIR__ . '/app/config/config.php');
$di = new DI();
$di->set('config', $config);
$queue = new Beanstalk();
$queue->choose('mailer');
$di['queue'] = $queue;
/**
 * Register Mailer Service
 */
$di['mailer'] = function () {
    $service = new MailerService();
    return $service->mailer();
};
/** @var Job $job */
while (($job = $queue->peekReady()) !== false) {
    $data = json_decode($job->getBody(), true);
    $segments = explode(':', $data['job']);
    if (count($segments) !== 2) {
        continue;
    }
    call_user_func_array([$di[$segments[0]], $segments[1]], [$job, $data['data']]);
}
Esempio n. 8
0
 public function disconnect()
 {
     return parent::disconnect();
 }
Esempio n. 9
0
 /**
  * Queue a new e-mail message for sending
  *
  * @param string|array $view
  * @param array $data
  * @param \Closure|string $callback
  *
  * @return mixed
  */
 public function queue($view, array $data, $callback)
 {
     $callback = $this->buildQueueCallable($callback);
     $this->queue->choose('mailer');
     return $this->queue->put(json_encode(['job' => 'mailer:handleQueuedMessage', 'data' => ['view' => $view, 'data' => $data, 'callback' => $callback]]));
 }