Since: 2015.02.24
Author: Petra Barus (petra.barus@gmail.com)
Inheritance: extends yii\base\Component
Example #1
0
 /**
  * Endpoint to post a job to queue.
  * @return mixed
  * @throws \yii\web\ServerErrorHttpException When failed to post.
  */
 public function actionPost()
 {
     $job = $this->createJobFromRequest();
     /* @var $queue \UrbanIndo\Yii2\Queue\Queue */
     if ($this->queue->post($job)) {
         return ['status' => 'okay', 'jobId' => $job->id];
     } else {
         throw new \yii\web\ServerErrorHttpException('Failed to post job');
     }
 }
 /**
  * @param \yii\base\Event $event The event to handle.
  * @return void
  */
 public function routeEvent(\yii\base\Event $event)
 {
     $eventName = $event->name;
     $handler = $this->events[$eventName];
     if (is_callable($handler)) {
         $handler = call_user_func($handler, $this->owner);
     }
     $route = $handler[0];
     unset($handler[0]);
     $data = $handler;
     $this->queue->post(new \UrbanIndo\Yii2\Queue\Job(['route' => $route, 'data' => $data]));
 }
Example #3
0
 /**
  * @return void
  */
 public function init()
 {
     parent::init();
     if (!is_numeric($this->sleepTimeout)) {
         throw new InvalidParamException('($sleepTimeout) must be an number');
     }
     if ($this->sleepTimeout < 0) {
         throw new InvalidParamException('($sleepTimeout) must be greater or equal than 0');
     }
     $this->queue = \yii\di\Instance::ensure($this->queue, Queue::className());
 }
 /**
  * Initialize the queue.
  */
 public function init()
 {
     parent::init();
     $queueObjects = [];
     foreach ($this->queues as $id => $queue) {
         $queueObjects[$id] = \Yii::createObject($queue);
     }
     $this->queues = $queueObjects;
     if (!isset($this->strategy)) {
         $this->strategy = ['class' => Strategies\RandomStrategy::class];
     }
     $this->strategy = \Yii::createObject($this->strategy);
     $this->strategy->setQueue($this);
 }
 /**
  * @param Queue $queue Queue the job located.
  * @param Job   $job   Job to be executed.
  * @return array
  */
 protected function executeJob(Queue $queue, Job $job)
 {
     $start = time();
     $return = ['jobId' => $job->id, 'route' => $job->isCallable() ? 'callable' : $job->route, 'data' => $job->data, 'time' => date('Y-m-d H:i:s', $start)];
     try {
         ob_start();
         $queue->run($job);
         $output = ob_get_clean();
         $return2 = ['status' => 'success', 'level' => 'info'];
     } catch (\Exception $exc) {
         $output = ob_get_clean();
         Yii::$app->getResponse()->statusCode = 500;
         $return2 = ['status' => 'failed', 'level' => 'error', 'reason' => $exc->getMessage(), 'trace' => $exc->getTraceAsString()];
     }
     $return3 = ['stdout' => $output, 'duration' => time() - $start];
     return array_merge($return, $return2, $return3);
 }
Example #6
0
 /**
  * Initialize the queue.
  * @return void
  * @throws \yii\base\InvalidConfigException If the strategy doesn't implement
  * UrbanIndo\Yii2\Queue\Strategies\Strategy.
  */
 public function init()
 {
     parent::init();
     $queueObjects = [];
     foreach ($this->queues as $id => $queue) {
         $queueObjects[$id] = \Yii::createObject($queue);
     }
     $this->queues = $queueObjects;
     if (is_array($this->strategy)) {
         $this->strategy = \Yii::createObject($this->strategy);
     } else {
         if ($this->strategy instanceof Strategy) {
             throw new \yii\base\InvalidConfigException('The strategy field have to implement UrbanIndo\\Yii2\\Queue\\Strategies\\Strategy');
         }
     }
     $this->strategy->setQueue($this);
 }
Example #7
0
 public function testOn()
 {
     \yii\base\Event::on(\UrbanIndo\Yii2\Queue\Queue::className(), \UrbanIndo\Yii2\Queue\Queue::EVENT_AFTER_POST, function ($event) {
         $this->counter += 1;
     });
     \yii\base\Event::on(\UrbanIndo\Yii2\Queue\Queue::className(), \UrbanIndo\Yii2\Queue\Queue::EVENT_AFTER_FETCH, function ($event) {
         $this->counter += 2;
     });
     \yii\base\Event::on(\UrbanIndo\Yii2\Queue\Queue::className(), \UrbanIndo\Yii2\Queue\Queue::EVENT_AFTER_DELETE, function ($event) {
         $this->counter += 3;
     });
     $queue = Yii::createObject(['class' => '\\UrbanIndo\\Yii2\\Queue\\Queues\\MemoryQueue']);
     $this->assertEquals($this->counter, 0);
     /* @var $queue \UrbanIndo\Yii2\Queue\Queues\MemoryQueue */
     $queue->post(new UrbanIndo\Yii2\Queue\Job(['route' => function () {
         //Do something
     }]));
     $this->assertEquals($this->counter, 1);
     $job = $queue->fetch();
     $this->assertEquals($this->counter, 3);
     $queue->delete($job);
     $this->assertEquals($this->counter, 6);
 }
 /**
  * Call the behavior owner to handle the deferred event.
  * @param \yii\base\Event $event
  */
 public function postDeferredEvent($event)
 {
     $object = clone $this->owner;
     if (!$this->_hasEventHandlers && !$object instanceof DeferredEventInterface) {
         throw new \Exception("Model is not instance of DeferredEventInterface");
     }
     $handlers = $this->_hasEventHandlers ? $this->events : false;
     $eventName = $event->name;
     if (isset($this->_serializer)) {
         $serializer = $this->_serializer;
     } else {
         $serializer = null;
     }
     $this->queue->post(new \UrbanIndo\Yii2\Queue\Job(['route' => function () use($object, $eventName, $handlers, $serializer) {
         if ($handlers) {
             $handler = $handlers[$eventName];
             if ($serializer !== null) {
                 try {
                     $unserialized = $serializer->unserialize($handler);
                     $unserialized($object);
                 } catch (\Exception $exc) {
                     return call_user_func([$object, $handler]);
                 }
             } else {
                 return call_user_func([$object, $handler]);
             }
         } else {
             if ($object instanceof DeferredEventInterface) {
                 /* @var $object DeferredEventInterface */
                 return $object->handleDeferredEvent($eventName);
             } else {
                 throw new \Exception("Model doesn't have handlers for the event or is not instance of DeferredEventInterface");
             }
         }
     }]));
 }
 /**
  * @return void
  */
 public function init()
 {
     parent::init();
     $this->queue = \yii\di\Instance::ensure($this->queue, Queue::className());
 }
Example #10
0
 /**
  * @return void
  */
 public function init()
 {
     parent::init();
     $this->db = \yii\di\Instance::ensure($this->db, \yii\db\Connection::className());
 }
Example #11
0
 /**
  * Initialize the queue component.
  * @return void
  */
 public function init()
 {
     parent::init();
     $this->_client = SqsClient::factory($this->config);
 }