A bundle to use swarrot inside your Symfony2 application
The recommended way to install this bundle is through Composer. Just run:
composer require swarrot/swarrot-bundle
Update app/AppKernel.php
:
public function registerBundles()
{
$bundles = array(
// ...
new Swarrot\SwarrotBundle\SwarrotBundle(),
);
return $bundles;
}
swarrot:
provider: pecl # pecl or amqp_lib
default_connection: rabbitmq
default_command: swarrot.command.base # Swarrot\SwarrotBundle\Command\SwarrotCommand
connections:
rabbitmq:
host: "%rabbitmq_host%"
port: "%rabbitmq_port%"
login: "%rabbitmq_login%"
password: "%rabbitmq_password%"
vhost: '/'
consumers:
my_consumer:
processor: my_consumer.processor.service
middleware_stack: # order matter
- configurator: swarrot.processor.signal_handler
# extras:
# signal_handler_signals:
# - SIGTERM
# - SIGINT
# - SIGQUIT
# - configurator: swarrot.processor.insomniac
- configurator: swarrot.processor.max_messages
# extras:
# max_messages: 100
- configurator: swarrot.processor.max_execution_time
# extras:
# max_execution_time: 300
- configurator: swarrot.processor.memory_limit
# extras:
# memory_limit: null
- configurator: swarrot.processor.doctrine_connection
# extras:
# doctrine_ping: true
# doctrine_close_master: true
- configurator: swarrot.processor.doctrine_object_manager
- configurator: swarrot.processor.exception_catcher
- configurator: swarrot.processor.ack
# extras:
# requeue_on_error: false
- configurator: swarrot.processor.retry
# extras:
# retry_exchange: retry
# retry_attempts: 3
# retry_routing_key_pattern: 'retry_%%attempt%%'
# - configurator: swarrot.processor.new_relic
# extras:
# new_relic_app_name: ~
# new_relic_license: ~
# new_relic_transaction_name: ~
# - configurator: swarrot.processor.rpc_server
# extras:
# rpc_exchange: rpc
# - configurator: swarrot.processor.rpc_client
# extras:
# rpc_client_correlation_id: ~
extras:
poll_interval: 500000
messages_types:
my_publisher:
connection: rabbitmq # use the default connection by default
exchange: my_exchange
routing_key: my_routing_key
First step is to retrieve the swarrot publisher service from your controller.
$messagePublisher = $this->get('swarrot.publisher');
After you need to prepare your message with the Message class.
use Swarrot\Broker\Message;
$message = new Message('"My first message with the awesome swarrot lib :)"');
Then you can publish a new message into a predefined configuration (connection, exchange, routing_key, etc.) from your message_types
.
$messagePublisher->publish('webhook.send', $message);
When publishing a message you can override the message_types
configuration by passing a third argument:
$messagePublisher->publish('webhook.send', $message, array(
'exchange' => 'my_new_echange',
'connection' => 'my_second_connection',
'routing_key' => 'my_new_routing_key'
));
Swarrot will automatically create new commands according to your configuration. This command need the queue name to consume as first argument. You can also use a named connection as second argument if you don't want to use the default one.
app/console swarrot:consume:my_consumer_name queue_name [connection_name]
Your processor will automatically be decorated by all processors named in the middleware_stack
section. The order matter.
All this processors are configurable.
You can add extras
key on each configurator definition in your config.yml
. Take a look at configuration reference to see available extras for existing Configurators.
You can also use options of the command line:
- --poll-interval [default: 500000]: Change the polling interval when no message found in broker
- --requeue-on-error (-r): Re-queue the message in the same queue if an error occurred.
- --no-catch (-C): Disable the ExceptionCatcher processor (available only if the processor is in the stack)
- --max-execution-time (-t) [default: 300]: Configure the MaxExecutionTime processor (available only if the processor is in the stack)
- --max-messages (-m) [default: 300]: Configure the MaxMessages processor (available only if the processor is in the stack)
- --no-retry (-R): Disable the Retry processor (available only if the processor is in the stack)
Default values will be override by your config.yml
and use of options will override defaut config values.
Run your command with -h̀
to have the full list of options.
If you want to implement your own provider (like Redis). First, you have to implements the Swarrot\SwarrotBundle\Broker\FactoryInterface
.
Then, you can register it with along the others services and tag it with swarrot.provider_factory
.
services:
app.swarrot.custom_provider_factory:
class: AppBundle\Provider\CustomFactory
tags:
- {name: swarrot.provider_factory}
app.swarrot.redis_provider_factory:
class: AppBundle\Provider\RedisFactory
tags:
- {name: swarrot.provider_factory, alias: redis}
Now you can tell to swarrot to use it in the config.yml
file.
swarrot:
provider: app.swarrot.custom_provider_factory
or with the alias
swarrot:
provider: redis
If you want to use a custom processor, you need two things. The Processor itself and a ProcessorConfigurator.
For the Processor, you can refer to the swarrot/swarrot
documentation.
For the ConfigurationProcessor, you need to implement the ProcessorConfiguratorInterface
and to register it as a service. Once down, just add it to the middleware stack of your consumer:
middleware_stack:
- configurator: swarrot.processor.signal_handler
- configurator: my_own_processor_configurator_service_id
As usual, take care of the order of your middleware_stack.
If you use Swarrot you may not want to really publish messages like in test environment for example. You can use the BlackholePublisher
to achieve this.
Simply override the swarrot.publisher.class
parameter in the DIC with the Swarrot\SwarrotBundle\Broker\PublisherBlackhole
class.
Update config_test.yml
for example:
parameters:
swarrot.publisher.class: Swarrot\SwarrotBundle\Broker\BlackholePublisher
This bundle is released under the MIT License. See the bundled LICENSE file for details.