diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a9875b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..adfa2cc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Queue Client Bundle Changelog + +## v1.0.0 + +- Initial release diff --git a/Command/AddMessagesCommand.php b/Command/AddMessagesCommand.php new file mode 100644 index 0000000..fd1f9a8 --- /dev/null +++ b/Command/AddMessagesCommand.php @@ -0,0 +1,65 @@ +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; + } +} diff --git a/Command/CreateQueuesCommand.php b/Command/CreateQueuesCommand.php new file mode 100644 index 0000000..a6b1ae7 --- /dev/null +++ b/Command/CreateQueuesCommand.php @@ -0,0 +1,141 @@ +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(<<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; + } + } + } +} diff --git a/Command/DeleteQueuesCommand.php b/Command/DeleteQueuesCommand.php new file mode 100644 index 0000000..98da8db --- /dev/null +++ b/Command/DeleteQueuesCommand.php @@ -0,0 +1,149 @@ +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(<<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; + } + } + } +} diff --git a/Command/GetMessagesCommand.php b/Command/GetMessagesCommand.php new file mode 100644 index 0000000..7c8690b --- /dev/null +++ b/Command/GetMessagesCommand.php @@ -0,0 +1,79 @@ +setName('queue-client:get-messages') + ->setDescription('Get messages from queue') + ->addOption('number-messages', 'c', InputOption::VALUE_OPTIONAL, 'Number of messages to be retrieved') + ->addOption('pop', null, InputOption::VALUE_NONE, 'If set, the task will remove ask messages from queue') + ->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Get messages from specific priority') + ->addArgument('queueName', InputArgument::REQUIRED, 'queue') + ->setHelp('This command get messages from 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); + 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; + } + $queueName = $input->getArgument('queueName'); + $numberMessages = $input->getOption('number-messages') ?: 1; + $priority = null; + if ($input->getOption('priority')) { + $priority = $input->getOption('priority'); + if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) { + throw new \InvalidArgumentException('Priority "' . $priority . '" not found.'); + } + } + $messages = $queueClient->getMessages($queueName, $numberMessages, $priority); + foreach ($messages as $message) { + if (is_array($message['Body'])) { + $output->writeln(json_encode($message['Body'])); + } else { + $output->writeln($message['Body']); + } + } + if ($input->getOption('pop')) { + $queueClient->deleteMessages($queueName, $messages); + } + + return 0; + } +} diff --git a/Command/ListPrioritiesCommand.php b/Command/ListPrioritiesCommand.php new file mode 100644 index 0000000..cd0aa00 --- /dev/null +++ b/Command/ListPrioritiesCommand.php @@ -0,0 +1,56 @@ +setName('queue-client:list-priorities') + ->setDescription('List priorities') + ->setHelp('This command list available message priorities.'); + } + + /** + * @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; + } + foreach ($queueClient->getPriorityHandler()->getAll() as $priority) { + $output->writeln($priority); + } + + return 0; + } +} diff --git a/Command/PurgeQueuesCommand.php b/Command/PurgeQueuesCommand.php new file mode 100644 index 0000000..19376d6 --- /dev/null +++ b/Command/PurgeQueuesCommand.php @@ -0,0 +1,150 @@ +setName('queue-client:purge-queues') + ->setDescription('Purge 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 purge') + ->addArgument('queues', InputArgument::IS_ARRAY, 'queues to purge') + ->setHelp(<<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('Purge queues in file "' . $fileName . '"?', false)))) { + + return 0; + } + + return $this->purgeFromFile($queueClient, $fileName); + } else { + $queues = $input->getArgument('queues'); + if (count($queues)) { + if (!($force || $helper->ask($input, $output, new ConfirmationQuestion(implode("\n", $queues) . "\nPurge queues list above?" , false)))) { + + return 0; + } + foreach ($queues as $queue) { + try { + $queueClient->purgeQueue($queue); + $this->output->write('Queue ' . $queue . ' purged.', 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('Purge queues in file "' . $fileName . '"?', false)))) { + + return 0; + } + + return $this->purgeFromFile($queueClient, $fileName); + } catch (InvalidArgumentException $e) { + $this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL); + + return 1; + } + } + } +} diff --git a/Command/QueuesInfoCommand.php b/Command/QueuesInfoCommand.php new file mode 100644 index 0000000..26cf3ad --- /dev/null +++ b/Command/QueuesInfoCommand.php @@ -0,0 +1,140 @@ +setName('queue-client:queues-info') + ->setDescription('Display queues information') + ->addOption('no-header', null, InputOption::VALUE_NONE, 'Skip header display') + ->addOption('alias', 'a', InputOption::VALUE_NONE, 'Display queues aliases info') + ->addOption('count', 'c', InputOption::VALUE_NONE, 'Display count messages in queue info') + ->addOption('priority', 'p', InputOption::VALUE_NONE, 'Display count message for each priorities') + ->addArgument('queues', InputArgument::IS_ARRAY, 'select info from specific queues') + ->setHelp('This command display queues information.'); + } + + /** + * @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; + } + $queues = $input->getArgument('queues'); + $queuesList = $queueClient->listQueues(); + if (0 === count($queues)) { + try { + $queues = $queuesList; + } catch (\Exception $e) { + $this->output->write($e->getMessage(), Output::ERROR); + + return 1; + } + } + + $table = new Table($output); + $arrayRows = []; + $queuesAliases = []; + $priorities = []; + + if ($input->getOption('alias')) { + $queuesAliases = $queueClient->getAliases(); + } + if ($input->getOption('priority')) { + $priorities = $queueClient->getPriorityHandler()->getAll(); + } + foreach ($queues as $queue) { + if (in_array($queue, $queuesList)) { + $row = [$queue]; + if ($input->getOption('count')) { + if ($input->getOption('priority')) { + foreach ($priorities as $priority) { + $count = $queueClient->getNumberMessages($queue, $priority); + $row[] = $count; + } + } else { + $count = $queueClient->getNumberMessages($queue); + $row[] = $count; + } + } + if ($input->getOption('alias')) { + $aliases = []; + foreach ($queuesAliases as $keyAlias => $alias) { + $keys = array_keys($alias, $queue); + foreach ($keys as $key) { + $aliases[] = $keyAlias; + } + } + $row[] = implode(',', $aliases); + } + $arrayRows[] = $row; + } else { + $this->output->write('Queue "' . $queue . '" does not exists.', Output::WARNING); + } + } + if (empty($queues)) { + $this->output->write('No queue found.', Output::NOTICE); + + return 0; + } + $table->setRows($arrayRows); + if (!$input->getOption('no-header')) { + $headers = []; + $headers['main'] = ['Name']; + if ($input->getOption('count')) { + if ($input->getOption('priority')) { + $headers['main'][] = new TableCell('Messages number', array('colspan' => 3)); + $headers['sub'][] = ''; + foreach ($priorities as $priority) { + $headers['sub'][] = $priority; + } + } else { + $headers['main'][] = 'Messages number'; + } + } + if ($input->getOption('alias')) { + $headers['main'][] = 'Aliases'; + } + $table->setHeaders($headers); + } + $table->render(); + + return 0; + } +} diff --git a/Configuration/QueuesConfiguration.php b/Configuration/QueuesConfiguration.php new file mode 100644 index 0000000..c62cb2c --- /dev/null +++ b/Configuration/QueuesConfiguration.php @@ -0,0 +1,39 @@ +root(static::ROOT_NODE); + + $rootNode + ->children() + ->arrayNode(static::QUEUES_NODE) + ->isRequired() + ->prototype('array') + ->children() + ->scalarNode(static::QUEUE_NAME_NODE) + ->isRequired() + ->end() + ->arrayNode(static::QUEUE_ALIASES_NODE) + ->prototype('scalar')->end() + ->end() + ->end() + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..2a1f78e --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,48 @@ +root('queue_client'); + + $rootNode + ->children() + ->arrayNode('adapter') + ->isRequired() + ->children() + ->scalarNode('type') + ->isRequired() + ->cannotBeEmpty() + ->end() + ->scalarNode('key')->end() + ->scalarNode('secret')->end() + ->scalarNode('region') + ->defaultValue('eu-west-1') + ->end() + ->scalarNode('version') + ->defaultValue('2012-11-05') + ->end() + ->scalarNode('repository') + ->defaultValue('/tmp/queues') + ->end() + ->end() + ->end() + ->scalarNode('queues_file') + ->defaultValue(__DIR__.'/../Resources/config/queues.yml') + ->end() + ->scalarNode('priority_handler') + ->defaultValue('ReputationVIP\QueueClient\PriorityHandler\StandardPriorityHandler') + ->end() + ->end(); + + return $treeBuilder; + } +} diff --git a/DependencyInjection/QueueClientExtension.php b/DependencyInjection/QueueClientExtension.php new file mode 100644 index 0000000..820247c --- /dev/null +++ b/DependencyInjection/QueueClientExtension.php @@ -0,0 +1,50 @@ + 'ReputationVIP\QueueClient\Adapter\SQSAdapter', + 'file' => 'ReputationVIP\QueueClient\Adapter\FileAdapter', + 'memory' => 'ReputationVIP\QueueClient\Adapter\MemoryAdapter', + 'null' => 'ReputationVIP\QueueClient\Adapter\NullAdapter' + ); + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + + $loader = new YamlFileLoader( + $container, + new FileLocator(__DIR__.'/../Resources/config') + ); + $loader->load('services.yml'); + + if (!isset($adapterChoice[$config['adapter']['type']])) { + throw new \InvalidArgumentException('Unknown handler type : ' . $config['adapter']['type']); + } + + $container->setParameter( + 'queue_client.adapter.priority_handler.class', + $config['priority_handler'] + ); + $container->setParameter( + 'queue_client.queues_file', + $config['queues_file'] + ); + $container->setParameter( + 'queue_client.adapter.class', + $adapterChoice[$config['adapter']['type']] + ); + $container->setParameter( + 'queue_client.config', + $config['adapter'] + ); + } +} diff --git a/QueueClientAdapterFactory.php b/QueueClientAdapterFactory.php new file mode 100644 index 0000000..9597a4d --- /dev/null +++ b/QueueClientAdapterFactory.php @@ -0,0 +1,47 @@ + $config['region'], + 'version' => $config['version'], + 'credentials' => array( + 'key' => $config['key'], + 'secret' => $config['secret'], + ), + )), $priorityHandler); + break; + case 'memory': + $adapter = new MemoryAdapter($priorityHandler); + break; + } + + return $adapter; + } +} diff --git a/QueueClientBundle.php b/QueueClientBundle.php new file mode 100644 index 0000000..980baae --- /dev/null +++ b/QueueClientBundle.php @@ -0,0 +1,9 @@ +getParameter($param), $item); + } else { + throw new \InvalidArgumentException('Empty parameter!'); + } + } + } + } + + /** + * @param ContainerInterface $container + * @param AdapterInterface $adapter + * @param string $queuesFile + * @return null|QueueClientInterface + * @throws \ErrorException + */ + public function get($container, $adapter, $queuesFile) + { + $queueClient = new QueueClient($adapter); + $processor = new Processor(); + $configuration = new QueuesConfiguration(); + $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($queuesFile))); + + array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $container); + foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) { + $queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE]; + foreach ($queue[QueuesConfiguration::QUEUE_ALIASES_NODE] as $alias) { + try { + $queueClient->addAlias($queueName, $alias); + } catch (\ErrorException $e) { + if ($e->getSeverity() === E_ERROR) { + throw $e; + } + } + } + } + + return $queueClient; + } +} diff --git a/README.md b/README.md index 3ab99a5..e515c50 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,71 @@ -# queue-client-bundle -An easy way to use queue client library in Symfony project with his own Symfony bundle. +# Queue Client Bundle + +[![Join the chat at https://gitter.im/ReputationVIP/queue-client](https://badges.gitter.im/ReputationVIP/queue-client.svg)](https://gitter.im/ReputationVIP/queue-client?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +An easy way to use [queue client library](https://github.com/ReputationVIP/queue-client) in [Symfony](http://symfony.com) project with its own [Symfony](http://symfony.com) bundle. + +## Available commands + +- `queue-client:add-messages` Add message in queue +- `queue-client:create-queues` Create queues +- `queue-client:delete-queues` Delete queues +- `queue-client:get-messages` Get messages from queue +- `queue-client:list-priorities` List priorities +- `queue-client:purge-queues` Purge queues +- `queue-client:queues-info` Display queues information + +Use `--help` option for command usage. + +## Configuration + +Add queue client configuration in config.yml and retrieve the service using Symfony container. + +``` +container->get('queue-client') +``` + +`queue_client` node needs an ```adapter``` node to define the adapter to be used. +`adapter` node must define a ```type``` parameter (see "Available adapter types") + +Then add specific configuration for each ```type```. + +``` +queue_client: + adapter: + type: queue type +``` + +Sample configuration: +``` +queue_client: + queues_file: %kernel.root_dir%/config/queues.yml + adapter: + type: file + repository: /tmp/queues + priority_handler: 'ReputationVIP\QueueClient\PriorityHandler\ThreeLevelPriorityHandler' + +``` + +### General configuration + +- ```queues_file``` specifies the default [queues configuration file](doc/queues-configuration-file.md). +- ```priority_handler``` specifies the priority handler. Default is the `ReputationVIP\QueueClient\PriorityHandler\StandardPriorityHandler`. + +### Available adapter types + +- ```null``` a black hole type. +- ```memory``` a memory type. +- ```file``` a file queue type. +- ```sqs``` a SQS queue type. + +### File type Configuration + +- ```repository```: this config value set the absolute path of the repository which contains queues files (default `/tmp/queues`). + +### SQS type Configuration + +- ```key:``` this config value set the SQS key. +- ```secret:``` this config value set the SQS secret. +- ```region:``` this config value set the SQS region (default `eu-west-1`). +- ```version:``` this config value set the SQS version (default `2012-11-05`). + diff --git a/Resources/config/queues.yml b/Resources/config/queues.yml new file mode 100644 index 0000000..945d877 --- /dev/null +++ b/Resources/config/queues.yml @@ -0,0 +1,2 @@ +queue_client: + queues: diff --git a/Resources/config/services.yml b/Resources/config/services.yml new file mode 100644 index 0000000..354d445 --- /dev/null +++ b/Resources/config/services.yml @@ -0,0 +1,24 @@ +services: + queue_client_adapter_priority_handler: + class: "%queue_client.adapter.priority_handler.class%" + + queue_client_adapter_factory: + class: ReputationVIP\Bundle\QueueClientBundle\QueueClientAdapterFactory + + queue_client_factory: + class: ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory + + queue_client_adapter: + class: "%queue_client.adapter.class%" + factory: ["@queue_client_adapter_factory", get] + arguments: + - "%queue_client.config%" + - "@queue_client_adapter_priority_handler" + + queue_client: + class: ReputationVIP\QueueClient\QueueClient + factory: ["@queue_client_factory", get] + arguments: + - "@service_container" + - "@queue_client_adapter" + - "%queue_client.queues_file%" diff --git a/Utils/Output.php b/Utils/Output.php new file mode 100644 index 0000000..878af3d --- /dev/null +++ b/Utils/Output.php @@ -0,0 +1,75 @@ +logger = $logger; + $this->output = $output; + } + + public function write($msg, $level) { + if (null !== $this->logger) { + switch ($level) { + case self::ERROR : + $this->logger->error($msg); + break; + case self::CRITICAL : + $this->logger->critical($msg); + break; + case self::INFO : + $this->logger->info($msg); + break; + case self::NOTICE : + $this->logger->notice($msg); + break; + case self::WARNING : + $this->logger->warning($msg); + break; + default : + $this->logger->debug($msg); + } + } else { + switch ($level) { + case self::ERROR : + $this->output->writeln('' . $msg . ''); + break; + case self::CRITICAL : + $this->output->writeln('' . $msg . ''); + break; + case self::INFO : + $this->output->writeln('' . $msg . ''); + break; + case self::NOTICE : + $this->output->writeln('' . $msg . ''); + break; + case self::WARNING : + $this->output->writeln('' . $msg . ''); + break; + default : + $this->output->writeln($msg); + } + } + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..08f9e95 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "reputation-vip/queue-client-bundle", + "description": "Queue Client Symfony Bundle", + "authors": [ + { + "name": "Nicolas Couet", + "email": "tejerka@gmail.com" + } + ], + "require": { + "reputation-vip/queue-client": "~1.0", + "symfony/http-kernel": ">=2.7", + "symfony/config": ">=2.7", + "symfony/dependency-injection": ">=2.7", + "symfony/framework-bundle": ">=2.7", + "symfony/console": ">=2.7", + "symfony/yaml": ">=2.7", + "psr/log": "^1.0" + }, + "autoload": { + "psr-4": { + "ReputationVIP\\Bundle\\QueueClientBundle\\": "/" + } + } +} diff --git a/doc/queues-configuration-file.md b/doc/queues-configuration-file.md new file mode 100644 index 0000000..024ab66 --- /dev/null +++ b/doc/queues-configuration-file.md @@ -0,0 +1,28 @@ +# Queues configuration file + +## Usage + +this file describes your queues structure. +It requires a main node `queues` with one sub node for each queue. + +## Sample + +``` +queue_client: + queues: + queue1: + name: queue1 + aliases: + - queue1-alias1 + - queue1-alias2 + queue2: + name: queue2 + aliases: + - queue2-alias1 + - queue2-alias2 + queue3: + name: queue3 + aliases: + - queue3-alias1 + - queue3-alias2 +```