public function testRequestStopWhileThresholdExceeded()
 {
     $this->listener->setMaxMemory(1024);
     $this->listener->onStopConditionCheck($this->event);
     $this->assertContains('memory threshold of 1kB exceeded (usage: ', $this->listener->onReportQueueState($this->event));
     $this->assertTrue($this->event->shouldExitWorkerLoop());
 }
Ejemplo n.º 2
0
 /**
  * Checks for the stop condition of this strategy
  *
  * @param WorkerEvent $event
  * @return string
  */
 public function onStopConditionCheck(WorkerEvent $event)
 {
     if ($this->interrupted) {
         $event->exitWorkerLoop();
         $this->state = sprintf("interrupt by an external signal on '%s'", $event->getName());
     }
 }
 public function testOnStopConditionCheckHandler_SIGINT()
 {
     $this->listener->onPCNTLSignal(SIGTERM);
     $this->listener->onStopConditionCheck($this->event);
     $this->assertContains('interrupt by an external signal', $this->listener->onReportQueueState($this->event));
     $this->assertTrue($this->event->shouldExitWorkerLoop());
 }
Ejemplo n.º 4
0
 /**
  * Ensure that an existing (previously processed) job can be removed from the event
  */
 public function testEventJobCanBeCleared()
 {
     $event = new WorkerEvent($this->worker, $this->queue);
     $job = new SimpleJob();
     $event->setJob($job);
     $this->assertNotNull($event->getJob());
     $event->setJob(null);
     $this->assertNull($event->getJob());
 }
Ejemplo n.º 5
0
 /**
  * @param WorkerEvent $e
  */
 public function onLogJobProcessStart(WorkerEvent $e)
 {
     $job = $e->getJob();
     $name = $job->getMetadata('name');
     if (null === $name) {
         $name = get_class($job);
     }
     $this->console->write(sprintf('Processing job %s...', $name));
 }
 /**
  * @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);
 }
Ejemplo n.º 7
0
 public function onStopConditionCheck(WorkerEvent $event)
 {
     $this->runCount++;
     if ($this->maxRuns && $this->runCount >= $this->maxRuns) {
         $event->exitWorkerLoop();
         $this->state = sprintf('maximum of %s jobs processed', $this->runCount);
     } else {
         $this->state = sprintf('%s jobs processed', $this->runCount);
     }
 }
Ejemplo n.º 8
0
 /**
  * @param WorkerEvent $event
  */
 public function onSomeListener($event)
 {
     $this->runCount++;
     if ($this->maxRuns && $this->runCount >= $this->maxRuns) {
         $event->exitWorkerLoop();
         $this->state = sprintf('maximum of %s jobs processed', $this->runCount);
     } else {
         $this->state = sprintf('%s jobs processed', $this->runCount);
     }
 }
 public function setUp()
 {
     $queue = $this->getMockBuilder('SlmQueue\\Queue\\AbstractQueue')->disableOriginalConstructor()->getMock();
     $worker = $this->getMock('SlmQueue\\Worker\\WorkerInterface');
     $ev = new WorkerEvent($worker, $queue);
     $job = new SimpleJob();
     $ev->setJob($job);
     $this->listener = new MaxPollingFrequencyStrategy();
     $this->event = $ev;
 }
Ejemplo n.º 10
0
 public function testOnStopConditionCheckHandler()
 {
     $this->listener->setMaxRuns(3);
     $this->listener->onStopConditionCheck($this->event);
     $this->assertContains('1 jobs processed', $this->listener->onReportQueueState($this->event));
     $this->assertFalse($this->event->shouldExitWorkerLoop());
     $this->listener->onStopConditionCheck($this->event);
     $this->assertContains('2 jobs processed', $this->listener->onReportQueueState($this->event));
     $this->assertFalse($this->event->shouldExitWorkerLoop());
     $this->listener->onStopConditionCheck($this->event);
     $this->assertContains('maximum of 3 jobs processed', $this->listener->onReportQueueState($this->event));
     $this->assertTrue($this->event->shouldExitWorkerLoop());
 }
Ejemplo n.º 11
0
    /**
     * @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);
    }
Ejemplo n.º 12
0
 /**
  * {@inheritDoc}
  */
 public function processQueue(QueueInterface $queue, array $options = [])
 {
     $eventManager = $this->eventManager;
     $workerEvent = new WorkerEvent($this, $queue);
     $workerEvent->setOptions($options);
     $eventManager->trigger(WorkerEvent::EVENT_BOOTSTRAP, $workerEvent);
     while (!$workerEvent->shouldExitWorkerLoop()) {
         $eventManager->trigger(WorkerEvent::EVENT_PROCESS_QUEUE, $workerEvent);
     }
     $eventManager->trigger(WorkerEvent::EVENT_FINISH, $workerEvent);
     $queueState = $eventManager->trigger(WorkerEvent::EVENT_PROCESS_STATE, $workerEvent);
     $queueState = array_filter(ArrayUtils::iteratorToArray($queueState));
     return $queueState;
 }
Ejemplo n.º 13
0
 /**
  * @param  WorkerEvent $event
  * @return void
  */
 public function onStopConditionCheck(WorkerEvent $event)
 {
     // @see http://php.net/manual/en/features.gc.collecting-cycles.php
     if (gc_enabled()) {
         gc_collect_cycles();
     }
     $usage = memory_get_usage();
     if ($this->maxMemory && $usage > $this->maxMemory) {
         $event->exitWorkerLoop();
         $this->state = sprintf("memory threshold of %s exceeded (usage: %s)", $this->humanFormat($this->maxMemory), $this->humanFormat($usage));
     } else {
         $this->state = sprintf('%s memory usage', $this->humanFormat($usage));
     }
 }
Ejemplo n.º 14
0
    public function testStopConditionCheckIdlingThrottling()
    {
        // builds a file list
        if (!is_dir('tests/build')) {
            mkdir('tests/build', 0755, true);
        }
        file_put_contents('tests/build/filewatch.txt', 'hi');

        $this->listener->setPattern('/^\.\/(tests\/build).*\.(txt)$/');
        $this->listener->setIdleThrottleTime(1);

        $this->event->setName(WorkerEvent::EVENT_PROCESS_IDLE);

        // records last time based when idle event
        $this->listener->onStopConditionCheck($this->event);

        // file has changed
        file_put_contents('tests/build/filewatch.txt', 'hello');

        $this->listener->onStopConditionCheck($this->event);
        $this->assertFalse($this->event->shouldExitWorkerLoop());
        sleep(1);

        $this->listener->onStopConditionCheck($this->event);
        $this->assertTrue($this->event->shouldExitWorkerLoop());
    }
 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);
 }
 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);
 }
Ejemplo n.º 17
0
 /**
  * @param  WorkerEvent $event
  * @return void
  */
 public function onStopConditionCheck(WorkerEvent $event)
 {
     if ($event->getName() == WorkerEvent::EVENT_PROCESS_IDLE) {
         if ($this->previousIdlingTime + $this->idleThrottleTime > microtime(true)) {
             return;
         } else {
             $this->previousIdlingTime = microtime(true);
         }
     }
     if (!count($this->files)) {
         $this->constructFileList();
         $this->state = sprintf("watching %s files for modifications", count($this->files));
     }
     foreach ($this->files as $checksum => $file) {
         if (!file_exists($file) || !is_readable($file) || (string) $checksum !== hash_file('crc32', $file)) {
             $event->exitWorkerLoop();
             $this->state = sprintf("file modification detected for '%s'", $file);
         }
     }
 }
Ejemplo n.º 18
0
 /**
  * Callback facilitating the worker loop
  *
  * @param WorkerEvent $e
  */
 public function callbackProcessQueueSetOptionsOnWorkerEvent(WorkerEvent $e)
 {
     $e->exitWorkerLoop();
     $this->eventOptions = $e->getOptions();
 }
Ejemplo n.º 19
0
 public function testOnLogJobProcessDone_DoesNotHaltPropagation()
 {
     $this->listener->onLogJobProcessDone($this->event);
     $this->assertFalse($this->event->shouldExitWorkerLoop());
 }
Ejemplo n.º 20
0
 public function testOnJobProcessHandlerEventGetsJobResult()
 {
     $this->listener->onJobProcess($this->event);
     $this->assertTrue($this->event->getResult() == 'result');
 }