swarrot/swarrot-bundle
Symfony bundle integrating Swarrot message consumers with RabbitMQ. Configure AMQP connections, define consumers as services, and build ordered middleware stacks (signal handling, max messages/time, memory limits, Doctrine integration). Ships a base console command and logger support.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require swarrot/swarrot-bundle
Register the bundle in config/bundles.php:
return [
// ...
Swarrot\SwarrotBundle\SwarrotBundle::class => ['all' => true],
];
Basic Configuration (config/packages/swarrot.yaml):
swarrot:
provider: pecl # or 'amqp_lib'
default_connection: rabbitmq
connections:
rabbitmq:
url: "amqp://guest:guest@localhost:5672/%2f"
messages_types:
default:
connection: rabbitmq
exchange: my_exchange
routing_key: my_key
First Use Case: Publishing a Message Inject the publisher service in a controller/service:
use Swarrot\Broker\Message;
class MyController extends AbstractController
{
public function publishMessage()
{
$publisher = $this->container->get('swarrot.publisher');
$message = new Message('Hello Swarrot!');
$publisher->publish('default', $message);
}
}
$publisher = $this->get('swarrot.publisher');
$message = new Message(json_encode(['data' => 'value']));
$publisher->publish('message_type_key', $message);
$publisher->publish('message_type_key', $message, [
'exchange' => 'dynamic_exchange',
'routing_key' => 'dynamic_key',
]);
namespace App\Service;
use Swarrot\Processor\ProcessorInterface;
use Swarrot\Broker\Message;
class MyConsumer implements ProcessorInterface
{
public function process(Message $message, array $options)
{
$data = json_decode($message->getBody(), true);
// Process logic here
}
}
config/packages/swarrot.yaml):
swarrot:
consumers:
my_consumer:
processor: app.service.my_consumer
middleware_stack:
- configurator: swarrot.processor.ack
- configurator: swarrot.processor.retry
php bin/console swarrot:consume:my_consumer my_queue [connection_name]
middleware_stack:
- configurator: swarrot.processor.signal_handler
- configurator: swarrot.processor.max_messages
- configurator: swarrot.processor.doctrine_connection
- configurator: swarrot.processor.ack
ProcessorConfiguratorInterface and register as a service:
services:
app.swarrot.custom_middleware:
class: App\Middleware\CustomProcessorConfigurator
tags:
- { name: swarrot.processor_configurator }
swarrot:
connections:
rabbitmq:
url: "amqp://user:pass@rabbitmq:5672/%2f"
redis:
url: "redis://redis:6379"
php bin/console swarrot:consume:my_consumer my_queue redis
middleware_stack:
- configurator: swarrot.processor.doctrine_connection
extras:
doctrine_close_master: true
middleware_stack:
- configurator: swarrot.processor.retry
extras:
retry_attempts: 3
retry_exchange: retry_exchange
php bin/console swarrot:consume:my_consumer my_queue --requeue-on-error
consumers:
my_consumer:
extras:
poll_interval: 100000 # 100ms
php bin/console swarrot:consume:my_consumer my_queue --max-messages=50
# config/packages/test/swarrot.yaml
parameters:
swarrot.publisher.class: Swarrot\SwarrotBundle\Broker\BlackholePublisher
url in connections is correct (use amqp:// for RabbitMQ)..env match the broker configuration.middleware_stack. Example:
middleware_stack:
- configurator: swarrot.processor.ack # Must run after retry
- configurator: swarrot.processor.retry
doctrine_connection middleware with doctrine_ping: true.$message = new Message(json_encode(['key' => 'value']));
command_alias is defined in consumers:
consumers:
my_consumer:
command_alias: app:my:custom:command
Then run:
php bin/console app:my:custom:command my_queue
swarrot:
logger: monolog.logger.debug
retry_log_levels_map in retry middleware for granular control.php bin/console swarrot:consume:my_consumer my_queue --poll-interval=200000
php bin/console swarrot:consume:my_consumer my_queue --no-catch
# config/packages/test/swarrot.yaml
swarrot:
connections:
rabbitmq:
url: "amqp://guest:guest@localhost:5672/%2f?connection_attempts=1"
| Error | Cause | Solution |
|---|---|---|
Connection refused |
Broker unreachable | Check broker URL/credentials. |
No such exchange |
Exchange not declared | Declare exchange in broker (e.g., RabbitMQ). |
Message not acknowledged |
Missing ack middleware |
Add swarrot.processor.ack to stack. |
Class not found |
Incorrect service ID | Verify processor service exists. |
Swarrot\SwarrotBundle\Broker\FactoryInterface.swarrot.provider_factory:
services:
app.swarrot.redis_factory:
class: App\Broker\RedisFactory
tags:
- { name: swarrot.provider_factory, alias: redis }
swarrot.yaml:
swarrot:
provider: redis
ProcessorInterface:
class CustomProcessor implements ProcessorInterface
{
public function process(Message $message, array $options
How can I help you explore Laravel packages today?