Skip to content

Commit

Permalink
Merge pull request #1 from tejerka/master
Browse files Browse the repository at this point in the history
First bundle release
  • Loading branch information
bobey committed Feb 23, 2016
2 parents bfafcfe + 40e3203 commit 9334c87
Show file tree
Hide file tree
Showing 21 changed files with 1,271 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Queue Client Bundle Changelog

## v1.0.0

- Initial release
65 changes: 65 additions & 0 deletions Command/AddMessagesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ReputationVIP\Bundle\QueueClientBundle\Command;

use Psr\Log\LoggerInterface;
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
use ReputationVIP\QueueClient\QueueClientInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class AddMessagesCommand extends ContainerAwareCommand
{
/**
* @var Output $output
*/
private $output;

protected function configure()
{
$this
->setName('queue-client:add-messages')
->setDescription('Add message in queue')
->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Add in queue with specific priority')
->addArgument('queueName', InputArgument::REQUIRED, 'queue')
->addArgument('messages', InputArgument::IS_ARRAY, 'messages to add')
->setHelp('This command add messages in queue.');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
/** @var LoggerInterface $logger */
$logger = $this->getContainer()->get('logger');
} catch (ServiceNotFoundException $e) {
$logger = null;
}
$this->output = new Output($logger, $output);
/** @var QueueClientInterface $queueClient */
$queueClient = $this->getContainer()->get('queue_client');

$priority = null;
if ($input->getOption('priority')) {
$priority = $input->getOption('priority');
if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) {
throw new \InvalidArgumentException('Priority "' . $priority . '" not found.');
}
}

$queueName = $input->getArgument('queueName');
$messages = $input->getArgument('messages');

$queueClient->addMessages($queueName, $messages, $priority);

return 0;
}
}
141 changes: 141 additions & 0 deletions Command/CreateQueuesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace ReputationVIP\Bundle\QueueClientBundle\Command;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
use ReputationVIP\QueueClient\QueueClientInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Yaml\Yaml;

class CreateQueuesCommand extends ContainerAwareCommand
{
/**
* @var Output $output
*/
private $output;

protected function configure()
{
$this
->setName('queue-client:create-queues')
->setDescription('Create queues')
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File to read')
->addArgument('queues', InputArgument::IS_ARRAY, 'queues to create')
->setHelp(<<<HELP
This command creates queues.
Specify file in config file:
queue_client:
queues_file: path/to/file.yml
Or specify file with file option:
--file=path/to/file.yml
Or list queues to create:
queue-client:create-queues queue1 queue2 queue3
HELP
);
}

/**
* @param QueueClientInterface $queueClient
* @param string $fileName
* @return int
*/
private function createFromFile($queueClient, $fileName)
{
try {
$processor = new Processor();
$configuration = new QueuesConfiguration();
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));

} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::CRITICAL);

return 1;
}
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
$this->output->write('Start create queue.', Output::INFO);
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
try {
$queueClient->createQueue($queueName);
$this->output->write('Queue ' . $queueName . ' created.', Output::INFO);
} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::WARNING);
}
foreach ($queue[QueuesConfiguration::QUEUE_ALIASES_NODE] as $alias) {
try {
$queueClient->addAlias($queueName, $alias);
$this->output->write('Queue alias ' . $alias . ' -> ' . $queueName . ' found.', Output::INFO);
} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::WARNING);
}
}
}
$this->output->write('End create queue.', Output::INFO);

return 0;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
/** @var LoggerInterface $logger */
$logger = $this->getContainer()->get('logger');
} catch (ServiceNotFoundException $e) {
$logger = null;
}
$this->output = new Output($logger, $output);
try {
/** @var QueueClientInterface $queueClient */
$queueClient = $this->getContainer()->get('queue_client');
} catch (ServiceNotFoundException $e) {
$this->output->write('No queue client service found.', Output::CRITICAL);

return 1;
}
if ($input->getOption('file')) {
$fileName = $input->getOption('file');

return $this->createFromFile($queueClient, $fileName);
} else {
$queues = $input->getArgument('queues');
if (count($queues)) {
foreach ($queues as $queue) {
try {
$queueClient->createQueue($queue);
$this->output->write('Queue ' . $queue . ' created.', Output::INFO);
} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::WARNING);
}
}

return 0;
}
try {
$fileName = $this->getContainer()->getParameter('queue_client.queues_file');

return $this->createFromFile($queueClient, $fileName);
} catch (InvalidArgumentException $e) {
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);

return 1;
}
}
}
}
149 changes: 149 additions & 0 deletions Command/DeleteQueuesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace ReputationVIP\Bundle\QueueClientBundle\Command;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
use ReputationVIP\QueueClient\QueueClientInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Yaml\Yaml;

class DeleteQueuesCommand extends ContainerAwareCommand
{
/**
* @var Output $output
*/
private $output;

protected function configure()
{
$this
->setName('queue-client:delete-queues')
->setDescription('Delete queues')
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File to read')
->addOption('force', null, InputOption::VALUE_NONE, 'If set, the task will not ask for confirm delete')
->addArgument('queues', InputArgument::IS_ARRAY, 'queues to delete')
->setHelp(<<<HELP
This command deletes queues.
Specify file in config file:
queue_client:
queues_file: path/to/file.yml
Or specify file with file option:
--file=path/to/file.yml
Or list queues to delete:
queue-client:delete-queues queue1 queue2 queue3
HELP
);
}

/**
* @param QueueClientInterface $queueClient
* @param string $fileName
* @return int
*/
private function deleteFromFile($queueClient, $fileName)
{
try {
$processor = new Processor();
$configuration = new QueuesConfiguration();
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));

} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::CRITICAL);

return 1;
}
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
$this->output->write('Start delete queue.', Output::INFO);
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
try {
$queueClient->deleteQueue($queueName);
$this->output->write('Queue ' . $queueName . ' deleted.', Output::INFO);
} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::WARNING);
}
}
$this->output->write('End delete queue.', Output::INFO);

return 0;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$force = $input->getOption('force') ? true : false;
try {
/** @var LoggerInterface $logger */
$logger = $this->getContainer()->get('logger');
} catch (ServiceNotFoundException $e) {
$logger = null;
}
$this->output = new Output($logger, $output);
try {
/** @var QueueClientInterface $queueClient */
$queueClient = $this->getContainer()->get('queue_client');
} catch (ServiceNotFoundException $e) {
$this->output->write('No queue client service found.', Output::CRITICAL);

return 1;
}
if ($input->getOption('file')) {
$fileName = $input->getOption('file');
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) {

return 0;
}

return $this->deleteFromFile($queueClient, $fileName);
} else {
$queues = $input->getArgument('queues');
if (count($queues)) {
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion(implode("\n", $queues) . "\nDelete queues list above?" , false)))) {

return 0;
}
foreach ($queues as $queue) {
try {
$queueClient->deleteQueue($queue);
$this->output->write('Queue ' . $queue . ' deleted.', Output::INFO);
} catch (\Exception $e) {
$this->output->write($e->getMessage(), Output::WARNING);
}
}

return 0;
}
try {
$fileName = $this->getContainer()->getParameter('queue_client.queues_file');
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) {

return 0;
}

return $this->deleteFromFile($queueClient, $fileName);
} catch (InvalidArgumentException $e) {
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);

return 1;
}
}
}
}
Loading

0 comments on commit 9334c87

Please sign in to comment.