Skip to content

Commit 5ed9339

Browse files
authored
Merge pull request #23 from trandangtri/feature/amount-consumed
Add more argument: amount of messages to consume
2 parents ebdab6b + 33dccab commit 5ed9339

File tree

5 files changed

+50
-27
lines changed

5 files changed

+50
-27
lines changed

Command/QueueWorkerCommand.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
89
use Symfony\Component\Console\Output\OutputInterface;
910
use Symfony\Component\Console\Style\SymfonyStyle;
1011
use TriTran\SqsQueueBundle\Service\BaseQueue;
@@ -23,12 +24,8 @@ protected function configure()
2324
{
2425
$this
2526
->setName('tritran:sqs_queue:worker')
26-
->addArgument(
27-
'name',
28-
InputArgument::REQUIRED,
29-
'Queue Name',
30-
null
31-
)
27+
->addArgument('name', InputArgument::REQUIRED, 'Queue Name', null)
28+
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', 0)
3229
->setDescription('Start a worker that will listen to a specified SQS queue');
3330
}
3431

@@ -41,6 +38,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
4138
if (!$this->getContainer()->has(sprintf('tritran.sqs_queue.%s', $queueName))) {
4239
throw new \InvalidArgumentException(sprintf('Queue [%s] does not exist.', $queueName));
4340
}
41+
$amount = $input->getOption('messages');
42+
if ($amount < 0) {
43+
throw new \InvalidArgumentException("The -m option should be null or greater than 0");
44+
}
4445

4546
$io = new SymfonyStyle($input, $output);
4647
$io->title(sprintf('Start listening to queue <comment>%s</comment>', $queueName));
@@ -50,6 +51,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
5051

5152
/** @var BaseWorker $worker */
5253
$worker = $this->getContainer()->get('tritran.sqs_queue.queue_worker');
53-
$worker->start($queue);
54+
$worker->start($queue, $amount);
5455
}
5556
}

Service/BaseWorker.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,33 @@ class BaseWorker
1212
{
1313
use LoggerAwareTrait;
1414

15+
/**
16+
* @var int
17+
*/
18+
private $consumed;
19+
1520
/**
1621
* @param BaseQueue $queue
22+
* @param int $amount
1723
* @param int $limit Zero is all
1824
*/
19-
public function start(BaseQueue $queue, int $limit = 1)
25+
public function start(BaseQueue $queue, int $amount = 0, int $limit = 1)
2026
{
21-
$this->consume($queue, $limit);
27+
$this->consumed = 0;
28+
$this->consume($queue, $amount, $limit);
2229
}
2330

2431
/**
2532
* @param BaseQueue $queue
33+
* @param int $amount
2634
* @param int $limit
2735
*/
28-
private function consume(BaseQueue $queue, int $limit = 1)
36+
private function consume(BaseQueue $queue, int $amount = 0, int $limit = 1)
2937
{
3038
while (true) {
39+
if ($amount && $this->consumed >= $amount) {
40+
break;
41+
}
3142
$this->fetchMessage($queue, $limit);
3243
}
3344
}
@@ -45,6 +56,8 @@ private function fetchMessage(BaseQueue $queue, int $limit = 1)
4556

4657
$messages->rewind();
4758
while ($messages->valid()) {
59+
$this->consumed++;
60+
4861
/** @var Message $message */
4962
$message = $messages->current();
5063

Tests/Functional/Command/QueueAttCommandTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ class QueueAttCommandTest extends KernelTestCase
2222
*/
2323
public function setUp()
2424
{
25-
if ($this->queueManager === null) {
26-
$this->queueManager = $this->getMockBuilder(QueueManager::class)
27-
->disableOriginalConstructor()
28-
->getMock();
29-
$this->queueManager
30-
->expects($this->any())
31-
->method('getQueueAttributes')
32-
->with('my-queue-url')
33-
->willReturn(['att1' => 'value1', 'att2' => 'value2']);
34-
35-
$this->getContainer()->set('tritran.sqs_queue.queue_manager', $this->queueManager);
36-
}
25+
$this->queueManager = $this->getMockBuilder(QueueManager::class)
26+
->disableOriginalConstructor()
27+
->getMock();
28+
$this->queueManager
29+
->expects($this->any())
30+
->method('getQueueAttributes')
31+
->with('my-queue-url')
32+
->willReturn(['att1' => 'value1', 'att2' => 'value2']);
33+
34+
$this->getContainer()->set('tritran.sqs_queue.queue_manager', $this->queueManager);
3735
}
3836

3937
/**

Tests/Functional/Command/QueueWorkerCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public function testExecuteWithNonExistingQueue()
4848
]);
4949
}
5050

51+
/**
52+
* Test: start a worker with an invalid value of amount of messages
53+
*/
54+
public function testExecuteWithInvalidAmountMessages()
55+
{
56+
$commandTester = $this->createCommandTester(new QueueWorkerCommand());
57+
58+
$this->expectException(\InvalidArgumentException::class);
59+
$commandTester->execute([
60+
'name' => 'basic_queue',
61+
'--messages' => -1
62+
]);
63+
}
64+
5165
/**
5266
* Test: Start a worker for listening to a queue
5367
*/

Tests/app/KernelTestCase.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace TriTran\SqsQueueBundle\Tests\app;
44

55
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6-
use Symfony\Bundle\FrameworkBundle\Console\Application;
76
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as SymfonyKernelTestCase;
7+
use Symfony\Component\Console\Application;
88
use Symfony\Component\Console\Tester\CommandTester;
99
use Symfony\Component\DependencyInjection\ContainerInterface;
1010

@@ -54,10 +54,7 @@ protected function getContainer($reinitialize = false, array $kernelOptions = []
5454
*/
5555
public function createCommandTester(ContainerAwareCommand $command)
5656
{
57-
$kernel = static::createKernel();
58-
$kernel->boot();
59-
60-
$application = new Application($kernel);
57+
$application = new Application();
6158
$command->setContainer($this->getContainer());
6259
$application->add($command);
6360

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy