Ejemplo n.º 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 \vm\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 \vm\queue\Job(['route' => $route, 'data' => $data]));
 }
Ejemplo n.º 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());
 }
Ejemplo n.º 4
0
 /**
  * Initialize the queue.
  * @return void
  * @throws \yii\base\InvalidConfigException If the strategy doesn't implement
  * vm\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 vm\\queue\\Strategies\\Strategy');
         }
     }
     $this->strategy->setQueue($this);
 }
Ejemplo n.º 5
0
 public function testOn()
 {
     \yii\base\Event::on(\vm\queue\Queue::className(), \vm\queue\Queue::EVENT_AFTER_POST, function ($event) {
         $this->counter += 1;
     });
     \yii\base\Event::on(\vm\queue\Queue::className(), \vm\queue\Queue::EVENT_AFTER_FETCH, function ($event) {
         $this->counter += 2;
     });
     \yii\base\Event::on(\vm\queue\Queue::className(), \vm\queue\Queue::EVENT_AFTER_DELETE, function ($event) {
         $this->counter += 3;
     });
     $queue = Yii::createObject(['class' => '\\vm\\queue\\Queues\\MemoryQueue']);
     $this->assertEquals($this->counter, 0);
     /* @var $queue \vm\queue\Queues\MemoryQueue */
     $queue->post(new vm\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 The event to process.
  *
  * @return void
  * @throws \Exception When the sender is not DeferredEventInterface.
  */
 public function postDeferredEvent(\yii\base\Event $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 \vm\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());
 }
Ejemplo n.º 8
0
 /**
  * Initialize the queue component.
  * @return void
  */
 public function init()
 {
     parent::init();
     $this->_client = SqsClient::factory($this->config);
 }
Ejemplo n.º 9
0
 /**
  * @return void
  */
 public function init()
 {
     parent::init();
     $this->db = \yii\di\Instance::ensure($this->db, Connection::className());
 }