Skip to content

Commit 9334c87

Browse files
committed
Merge pull request #1 from tejerka/master
First bundle release
2 parents bfafcfe + 40e3203 commit 9334c87

21 files changed

+1271
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Queue Client Bundle Changelog
2+
3+
## v1.0.0
4+
5+
- Initial release

Command/AddMessagesCommand.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
4+
5+
use Psr\Log\LoggerInterface;
6+
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
7+
use ReputationVIP\QueueClient\QueueClientInterface;
8+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
14+
15+
class AddMessagesCommand extends ContainerAwareCommand
16+
{
17+
/**
18+
* @var Output $output
19+
*/
20+
private $output;
21+
22+
protected function configure()
23+
{
24+
$this
25+
->setName('queue-client:add-messages')
26+
->setDescription('Add message in queue')
27+
->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Add in queue with specific priority')
28+
->addArgument('queueName', InputArgument::REQUIRED, 'queue')
29+
->addArgument('messages', InputArgument::IS_ARRAY, 'messages to add')
30+
->setHelp('This command add messages in queue.');
31+
}
32+
33+
/**
34+
* @param InputInterface $input
35+
* @param OutputInterface $output
36+
* @return int
37+
*/
38+
protected function execute(InputInterface $input, OutputInterface $output)
39+
{
40+
try {
41+
/** @var LoggerInterface $logger */
42+
$logger = $this->getContainer()->get('logger');
43+
} catch (ServiceNotFoundException $e) {
44+
$logger = null;
45+
}
46+
$this->output = new Output($logger, $output);
47+
/** @var QueueClientInterface $queueClient */
48+
$queueClient = $this->getContainer()->get('queue_client');
49+
50+
$priority = null;
51+
if ($input->getOption('priority')) {
52+
$priority = $input->getOption('priority');
53+
if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) {
54+
throw new \InvalidArgumentException('Priority "' . $priority . '" not found.');
55+
}
56+
}
57+
58+
$queueName = $input->getArgument('queueName');
59+
$messages = $input->getArgument('messages');
60+
61+
$queueClient->addMessages($queueName, $messages, $priority);
62+
63+
return 0;
64+
}
65+
}

Command/CreateQueuesCommand.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
4+
5+
use InvalidArgumentException;
6+
use Psr\Log\LoggerInterface;
7+
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
8+
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
9+
use ReputationVIP\QueueClient\QueueClientInterface;
10+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11+
use Symfony\Component\Config\Definition\Processor;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
17+
use Symfony\Component\Yaml\Yaml;
18+
19+
class CreateQueuesCommand extends ContainerAwareCommand
20+
{
21+
/**
22+
* @var Output $output
23+
*/
24+
private $output;
25+
26+
protected function configure()
27+
{
28+
$this
29+
->setName('queue-client:create-queues')
30+
->setDescription('Create queues')
31+
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File to read')
32+
->addArgument('queues', InputArgument::IS_ARRAY, 'queues to create')
33+
->setHelp(<<<HELP
34+
This command creates queues.
35+
36+
Specify file in config file:
37+
queue_client:
38+
queues_file: path/to/file.yml
39+
40+
Or specify file with file option:
41+
--file=path/to/file.yml
42+
43+
Or list queues to create:
44+
queue-client:create-queues queue1 queue2 queue3
45+
HELP
46+
);
47+
}
48+
49+
/**
50+
* @param QueueClientInterface $queueClient
51+
* @param string $fileName
52+
* @return int
53+
*/
54+
private function createFromFile($queueClient, $fileName)
55+
{
56+
try {
57+
$processor = new Processor();
58+
$configuration = new QueuesConfiguration();
59+
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));
60+
61+
} catch (\Exception $e) {
62+
$this->output->write($e->getMessage(), Output::CRITICAL);
63+
64+
return 1;
65+
}
66+
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
67+
$this->output->write('Start create queue.', Output::INFO);
68+
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
69+
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
70+
try {
71+
$queueClient->createQueue($queueName);
72+
$this->output->write('Queue ' . $queueName . ' created.', Output::INFO);
73+
} catch (\Exception $e) {
74+
$this->output->write($e->getMessage(), Output::WARNING);
75+
}
76+
foreach ($queue[QueuesConfiguration::QUEUE_ALIASES_NODE] as $alias) {
77+
try {
78+
$queueClient->addAlias($queueName, $alias);
79+
$this->output->write('Queue alias ' . $alias . ' -> ' . $queueName . ' found.', Output::INFO);
80+
} catch (\Exception $e) {
81+
$this->output->write($e->getMessage(), Output::WARNING);
82+
}
83+
}
84+
}
85+
$this->output->write('End create queue.', Output::INFO);
86+
87+
return 0;
88+
}
89+
90+
/**
91+
* @param InputInterface $input
92+
* @param OutputInterface $output
93+
* @return int
94+
*/
95+
protected function execute(InputInterface $input, OutputInterface $output)
96+
{
97+
try {
98+
/** @var LoggerInterface $logger */
99+
$logger = $this->getContainer()->get('logger');
100+
} catch (ServiceNotFoundException $e) {
101+
$logger = null;
102+
}
103+
$this->output = new Output($logger, $output);
104+
try {
105+
/** @var QueueClientInterface $queueClient */
106+
$queueClient = $this->getContainer()->get('queue_client');
107+
} catch (ServiceNotFoundException $e) {
108+
$this->output->write('No queue client service found.', Output::CRITICAL);
109+
110+
return 1;
111+
}
112+
if ($input->getOption('file')) {
113+
$fileName = $input->getOption('file');
114+
115+
return $this->createFromFile($queueClient, $fileName);
116+
} else {
117+
$queues = $input->getArgument('queues');
118+
if (count($queues)) {
119+
foreach ($queues as $queue) {
120+
try {
121+
$queueClient->createQueue($queue);
122+
$this->output->write('Queue ' . $queue . ' created.', Output::INFO);
123+
} catch (\Exception $e) {
124+
$this->output->write($e->getMessage(), Output::WARNING);
125+
}
126+
}
127+
128+
return 0;
129+
}
130+
try {
131+
$fileName = $this->getContainer()->getParameter('queue_client.queues_file');
132+
133+
return $this->createFromFile($queueClient, $fileName);
134+
} catch (InvalidArgumentException $e) {
135+
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);
136+
137+
return 1;
138+
}
139+
}
140+
}
141+
}

Command/DeleteQueuesCommand.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
4+
5+
use InvalidArgumentException;
6+
use Psr\Log\LoggerInterface;
7+
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
8+
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
9+
use ReputationVIP\QueueClient\QueueClientInterface;
10+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11+
use Symfony\Component\Config\Definition\Processor;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Question\ConfirmationQuestion;
17+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
18+
use Symfony\Component\Yaml\Yaml;
19+
20+
class DeleteQueuesCommand extends ContainerAwareCommand
21+
{
22+
/**
23+
* @var Output $output
24+
*/
25+
private $output;
26+
27+
protected function configure()
28+
{
29+
$this
30+
->setName('queue-client:delete-queues')
31+
->setDescription('Delete queues')
32+
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File to read')
33+
->addOption('force', null, InputOption::VALUE_NONE, 'If set, the task will not ask for confirm delete')
34+
->addArgument('queues', InputArgument::IS_ARRAY, 'queues to delete')
35+
->setHelp(<<<HELP
36+
This command deletes queues.
37+
38+
Specify file in config file:
39+
queue_client:
40+
queues_file: path/to/file.yml
41+
42+
Or specify file with file option:
43+
--file=path/to/file.yml
44+
45+
Or list queues to delete:
46+
queue-client:delete-queues queue1 queue2 queue3
47+
HELP
48+
);
49+
}
50+
51+
/**
52+
* @param QueueClientInterface $queueClient
53+
* @param string $fileName
54+
* @return int
55+
*/
56+
private function deleteFromFile($queueClient, $fileName)
57+
{
58+
try {
59+
$processor = new Processor();
60+
$configuration = new QueuesConfiguration();
61+
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));
62+
63+
} catch (\Exception $e) {
64+
$this->output->write($e->getMessage(), Output::CRITICAL);
65+
66+
return 1;
67+
}
68+
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
69+
$this->output->write('Start delete queue.', Output::INFO);
70+
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
71+
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
72+
try {
73+
$queueClient->deleteQueue($queueName);
74+
$this->output->write('Queue ' . $queueName . ' deleted.', Output::INFO);
75+
} catch (\Exception $e) {
76+
$this->output->write($e->getMessage(), Output::WARNING);
77+
}
78+
}
79+
$this->output->write('End delete queue.', Output::INFO);
80+
81+
return 0;
82+
}
83+
84+
/**
85+
* @param InputInterface $input
86+
* @param OutputInterface $output
87+
* @return int
88+
*/
89+
protected function execute(InputInterface $input, OutputInterface $output)
90+
{
91+
$helper = $this->getHelper('question');
92+
$force = $input->getOption('force') ? true : false;
93+
try {
94+
/** @var LoggerInterface $logger */
95+
$logger = $this->getContainer()->get('logger');
96+
} catch (ServiceNotFoundException $e) {
97+
$logger = null;
98+
}
99+
$this->output = new Output($logger, $output);
100+
try {
101+
/** @var QueueClientInterface $queueClient */
102+
$queueClient = $this->getContainer()->get('queue_client');
103+
} catch (ServiceNotFoundException $e) {
104+
$this->output->write('No queue client service found.', Output::CRITICAL);
105+
106+
return 1;
107+
}
108+
if ($input->getOption('file')) {
109+
$fileName = $input->getOption('file');
110+
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) {
111+
112+
return 0;
113+
}
114+
115+
return $this->deleteFromFile($queueClient, $fileName);
116+
} else {
117+
$queues = $input->getArgument('queues');
118+
if (count($queues)) {
119+
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion(implode("\n", $queues) . "\nDelete queues list above?" , false)))) {
120+
121+
return 0;
122+
}
123+
foreach ($queues as $queue) {
124+
try {
125+
$queueClient->deleteQueue($queue);
126+
$this->output->write('Queue ' . $queue . ' deleted.', Output::INFO);
127+
} catch (\Exception $e) {
128+
$this->output->write($e->getMessage(), Output::WARNING);
129+
}
130+
}
131+
132+
return 0;
133+
}
134+
try {
135+
$fileName = $this->getContainer()->getParameter('queue_client.queues_file');
136+
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) {
137+
138+
return 0;
139+
}
140+
141+
return $this->deleteFromFile($queueClient, $fileName);
142+
} catch (InvalidArgumentException $e) {
143+
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);
144+
145+
return 1;
146+
}
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)