public function testOnJobPopPopsTriggersIdleAndStopPropagation()
 {
     $this->event->getQueue()->expects($this->once())->method('pop')->will($this->returnValue(null));
     $called = false;
     $this->event->getTarget()->getEventManager()->attach(WorkerEvent::EVENT_PROCESS_IDLE, function (WorkerEvent $e) use(&$called) {
         $called = true;
     });
     $this->listener->onJobPop($this->event);
     $this->assertTrue($called);
     $this->assertNull($this->event->getJob());
     $this->assertEquals(WorkerEvent::JOB_STATUS_UNKNOWN, $this->event->getResult());
     $this->assertTrue($this->event->propagationIsStopped());
 }
 public function testAttachQueueListenersThrowsExceptionWhenNoListenersHaveBeenAttachedListeningToWorkerEventProcess()
 {
     $workerMock = $this->event->getTarget();
     $eventManagerMock = $workerMock->getEventManager();
     $eventManagerMock->expects($this->any())->method('getEvents')->will($this->returnValue([WorkerEvent::EVENT_PROCESS_IDLE]));
     $this->setExpectedException('SlmQueue\\Exception\\RunTimeException');
     $this->listener->attachQueueListeners($this->event);
 }
 public function testAttachQueueListenersBootstrapEventIsTriggeredOnlyOnce()
 {
     $workerMock = $this->event->getTarget();
     $eventManagerMock = $workerMock->getEventManager();
     $eventManagerMock->expects($this->any())->method('getEvents')->will($this->returnValue(array(WorkerEvent::EVENT_PROCESS_QUEUE)));
     $eventManagerMock->expects($this->once())->method('trigger')->with(WorkerEvent::EVENT_BOOTSTRAP, $this->logicalNot($this->identicalTo($this->event)));
     $this->listener->attachQueueListeners($this->event);
 }
 /**
  * @param  WorkerEvent $e
  * @return void
  */
 public function onJobProcess(WorkerEvent $e)
 {
     $job = $e->getJob();
     $queue = $e->getQueue();
     /** @var AbstractWorker $worker */
     $worker = $e->getTarget();
     $result = $worker->processJob($job, $queue);
     $e->setResult($result);
 }
    /**
     * @param WorkerEvent $e
     * @throws \SlmQueue\Exception\RuntimeException
     */
    public function attachQueueListeners(WorkerEvent $e)
    {
        /** @var AbstractWorker $worker */
        $worker       = $e->getTarget();
        $name         = $e->getQueue()->getName();
        $eventManager = $worker->getEventManager();

        $eventManager->detachAggregate($this);

        if (!isset($this->strategyConfig[$name])) {
            $name = 'default'; // We want to make sure the default process queue is always attached
        }

        $strategies = $this->strategyConfig[$name];

        foreach ($strategies as $strategy => $options) {
            // no options given, name stored as value
            if (is_numeric($strategy) && is_string($options)) {
                $strategy = $options;
                $options = array();
            }

            if (!is_string($strategy) || !is_array($options)) {
                continue;
            }

            $priority = null;
            if (isset($options['priority'])) {
                $priority = $options['priority'];
                unset($options['priority']);
            }

            $listener = $this->pluginManager->get($strategy, $options);

            if (!is_null($priority)) {
                $eventManager->attachAggregate($listener, $priority);
            } else {
                $eventManager->attachAggregate($listener);
            }
        }

        if (!in_array(WorkerEvent::EVENT_PROCESS_QUEUE, $eventManager->getEvents())) {
            throw new RuntimeException(sprintf(
                "No worker strategy has been registered to respond to the '%s' event.",
                WorkerEvent::EVENT_PROCESS_QUEUE
            ));
        }

        $e->stopPropagation();
        $eventManager->trigger(WorkerEvent::EVENT_BOOTSTRAP, $e);
    }
 public function testWorkerEventSetsWorkerAsTarget()
 {
     $event = new WorkerEvent($this->worker, $this->queue);
     $this->assertEquals($this->worker, $event->getTarget());
 }