Skip to content

Commit 69463ca

Browse files
committed
Merge pull request #117 from passkey1510/feat-dev-purge
Add purge command
2 parents f52b46a + e277938 commit 69463ca

File tree

6 files changed

+132
-20
lines changed

6 files changed

+132
-20
lines changed

Command/PurgeConsumerCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace OldSound\RabbitMqBundle\Command;
4+
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
/**
11+
* Command to purge a queue
12+
*/
13+
class PurgeConsumerCommand extends ConsumerCommand
14+
{
15+
protected function configure()
16+
{
17+
$this->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
18+
->addOption('no-confirmation', null, InputOption::VALUE_NONE, 'Whether it must be confirmed before purging');
19+
20+
$this->setName('rabbitmq:purge');
21+
}
22+
23+
/**
24+
* @param InputInterface $input
25+
* @param OutputInterface $output
26+
*
27+
* @return void
28+
*/
29+
protected function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$noConfirmation = (bool) $input->getOption('no-confirmation');
32+
33+
if (!$noConfirmation && $input->isInteractive()) {
34+
$confirmation = $this->getHelper('dialog')->askConfirmation($output, sprintf('<question>Are you sure you wish to purge "%s" queue? (y/n)</question>', $input->getArgument('name')), false);
35+
if (!$confirmation) {
36+
$output->writeln('<error>Purging cancelled!</error>');
37+
38+
return 1;
39+
}
40+
}
41+
42+
$this->consumer = $this->getContainer()
43+
->get(sprintf($this->getConsumerService(), $input->getArgument('name')));
44+
$this->consumer->purge($input->getArgument('name'));
45+
}
46+
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ If you want to establish a consumer memory limit, you can do it by using flag `-
265265
$ ./app/console rabbitmq:consumer -l 256
266266
```
267267
268+
If you want to remove all the messages awaiting in a queue, you can execute this command to purge this queue:
269+
270+
```bash
271+
$ ./app/console rabbitmq:purge --no-confirmation upload_picture
272+
```
273+
268274
#### Idle timeout ####
269275
270276
If you need to set a timeout when there are no messages from your queue during a period of time, you can set the `idle_timeout` in seconds:

RabbitMq/Consumer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public function consume($msgAmount)
4949
}
5050
}
5151

52+
/**
53+
* Purge the queue
54+
*/
55+
public function purge()
56+
{
57+
$this->ch->queue_purge($this->queueOptions['name'], true);
58+
}
59+
5260
public function processMessage(AMQPMessage $msg)
5361
{
5462

Tests/Command/BaseCommandTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace OldSound\RabbitMqBundle\Tests\Command;
4+
5+
abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase
6+
{
7+
protected $application;
8+
protected $definition;
9+
protected $helperSet;
10+
protected $command;
11+
12+
protected function setUp()
13+
{
14+
$this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
15+
->disableOriginalConstructor()
16+
->getMock();
17+
$this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
18+
->disableOriginalConstructor()
19+
->getMock();
20+
$this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
21+
22+
$this->application->expects($this->any())
23+
->method('getDefinition')
24+
->will($this->returnValue($this->definition));
25+
$this->definition->expects($this->any())
26+
->method('getArguments')
27+
->will($this->returnValue(array()));
28+
}
29+
}

Tests/Command/ConsumerCommandTest.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,11 @@
66

77
use Symfony\Component\Console\Input\InputOption;
88

9-
class ConsumerCommandTest extends \PHPUnit_Framework_TestCase
9+
class ConsumerCommandTest extends BaseCommandTest
1010
{
11-
private $application;
12-
private $definition;
13-
private $helperSet;
14-
private $command;
15-
1611
protected function setUp()
1712
{
18-
$this->application = $this->getMockBuilder('Symfony\\Bundle\\FrameworkBundle\\Console\\Application')
19-
->disableOriginalConstructor()
20-
->getMock();
21-
$this->definition = $this->getMockBuilder('Symfony\\Component\\Console\\Input\\InputDefinition')
22-
->disableOriginalConstructor()
23-
->getMock();
24-
$this->helperSet = $this->getMock('Symfony\\Component\\Console\\Helper\\HelperSet');
25-
26-
$this->application->expects($this->any())
27-
->method('getDefinition')
28-
->will($this->returnValue($this->definition));
29-
$this->definition->expects($this->any())
30-
->method('getArguments')
31-
->will($this->returnValue(array()));
13+
parent::setUp();
3214
$this->definition->expects($this->any())
3315
->method('getOptions')
3416
->will($this->returnValue(array(

Tests/Command/PurgeCommandTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace OldSound\RabbitMqBundle\Tests\Command;
4+
5+
use OldSound\RabbitMqBundle\Command\ConsumerCommand;
6+
7+
use OldSound\RabbitMqBundle\Command\PurgeConsumerCommand;
8+
use Symfony\Component\Console\Input\InputOption;
9+
10+
class PurgeCommandTest extends BaseCommandTest
11+
{
12+
protected function setUp()
13+
{
14+
parent::setUp();
15+
$this->definition->expects($this->any())
16+
->method('getOptions')
17+
->will($this->returnValue(array(
18+
new InputOption('--no-confirmation', null, InputOption::VALUE_NONE, 'Switches off confirmation mode.'),
19+
)));
20+
$this->application->expects($this->once())
21+
->method('getHelperSet')
22+
->will($this->returnValue($this->helperSet));
23+
24+
$this->command = new PurgeConsumerCommand();
25+
$this->command->setApplication($this->application);
26+
}
27+
28+
/**
29+
* testInputsDefinitionCommand
30+
*/
31+
public function testInputsDefinitionCommand()
32+
{
33+
// check argument
34+
$this->assertTrue($this->command->getDefinition()->hasArgument('name'));
35+
$this->assertTrue($this->command->getDefinition()->getArgument('name')->isRequired()); // Name is required to find the service
36+
37+
//check options
38+
$this->assertTrue($this->command->getDefinition()->hasOption('no-confirmation'));
39+
$this->assertFalse($this->command->getDefinition()->getOption('no-confirmation')->acceptValue()); // It shouldn't accept value because it is a true/false input
40+
}
41+
}

0 commit comments

Comments
 (0)