/** * Execute code. * * Each time new payload is consumed from queue, consume() method is called. * When iterations get the limit, process literaly dies * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return int */ protected function execute(InputInterface $input, OutputInterface $output) : int { $this->define(); $iterations = (int) $input->getOption('iterations'); $timeout = (int) $input->getOption('timeout'); $sleep = (int) $input->getOption('sleep'); $iterationsDone = 0; $queuesAlias = array_keys($this->methods); if ($this->shuffleQueues()) { shuffle($queuesAlias); } while ($response = $this->consumer->consume($queuesAlias, $timeout)) { list($queueAlias, $payload) = $response; $method = $this->methods[$queueAlias]; /* * All custom methods must have these parameters * * InputInterface $input An InputInterface instance * OutputInterface $output An OutputInterface instance * Mixed $payload Payload */ $this->{$method}($input, $output, $payload); if ($iterations > 0 && ++$iterationsDone >= $iterations) { break; } sleep($sleep); } return 0; }
/** * Tests consume method. */ public function testConsume() { $queue = 'queue'; $timeout = 0; $payload = ['engonga']; $redis = $this->createMock('\\Redis', ['blPop']); $redis->expects($this->once())->method('blPop')->with($this->equalTo($queue), $this->equalTo($timeout))->will($this->returnValue([$queue, json_encode($payload)])); $serializer = $this->createMock('RSQueue\\Serializer\\JsonSerializer', ['revert']); $serializer->expects($this->once())->method('revert')->with($this->equalTo(json_encode($payload)))->will($this->returnValue($payload)); $queueAliasResolver = $this->getMockBuilder('RSQueue\\Resolver\\QueueAliasResolver')->setMethods(['getQueue'])->disableOriginalConstructor()->getMock(); $queueAliasResolver->method('getQueue')->with($this->equalTo($queue))->will($this->returnValue($queue)); $eventDispatcher = $this->createMock('Symfony\\Component\\EventDispatcher\\EventDispatcher', ['dispatch']); $eventDispatcher->expects($this->once())->method('dispatch'); $consumer = new Consumer($eventDispatcher, $redis, $queueAliasResolver, $serializer); list($givenQueueAlias, $givenPayload) = $consumer->consume($queue, $timeout); $this->assertEquals($queue, $givenQueueAlias); $this->assertEquals($payload, $givenPayload); }