desarrolla2/async-event-dispatcher-bundle
Installation
Run composer require desarrolla2/async-event-dispatcher-bundle in your Symfony project root.
Ensure your composer.json includes the package under require.
Bundle Registration
Add the bundle to config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 3):
Desarrolla2\AsyncEventDispatcherBundle\AsyncEventDispatcherBundle::class => ['all' => true],
Basic Configuration
Define minimal config in config/packages/async_event_dispatcher.yaml:
async_event_dispatcher:
num_messages_per_execution: 1 # Default batch size
maximum_num_of_consumers: 4 # Default worker count
First Async Event Dispatch an event asynchronously in a controller/service:
use Desarrolla2\AsyncEventDispatcherBundle\Event\Event;
use Desarrolla2\AsyncEventDispatcherBundle\AsyncEventDispatcher;
public function dispatchExample(AsyncEventDispatcher $dispatcher) {
$event = new Event('my.event', ['data' => 'payload']);
$dispatcher->dispatchAsync($event);
}
Verify Workers Run the consumer command in a separate terminal:
php bin/console async:event:consume
Dispatching Events
AsyncEventDispatcher to queue events:
$dispatcher->dispatchAsync(new Event('user.created', ['userId' => 123]));
Event Structure
Event class or use plain arrays:
$event = new Event('order.processed', ['orderId' => 456, 'status' => 'shipped']);
Consumer Management
php bin/console async:event:consume --workers=8
num_messages_per_execution).Integration with Symfony Events
$dispatcher->dispatchAsync(new Event('kernel.request', ['request' => $request]));
Priority Queues
Extend the bundle’s queue table to add a priority column and filter events in the consumer:
// In consumer service
$events = $queue->getHighPriorityEvents();
Retry Logic
Implement a retry_count field in the queue table and handle failures in the consumer:
if ($event->getRetryCount() >= 3) {
$this->logger->error('Max retries exceeded for event: ' . $event->getName());
}
Delayed Events
Add a scheduled_at column to the queue table and process events based on timestamps:
$events = $queue->getEventsScheduledBefore(now());
Event Listeners as Async Wrap event listeners in async dispatchers:
public function onUserRegistered(UserRegisteredEvent $event) {
$asyncEvent = new Event('user.registered.async', ['user' => $event->getUser()]);
$this->dispatcher->dispatchAsync($asyncEvent);
}
Queue Table Schema
async_event_queue). If using a custom schema, ensure:
id, event_name, payload, created_at, and processed_at columns exist.payload must be serializable (use json_encode/json_decode for arrays).Worker Lifecycle
[program:async-worker]
command=php bin/console async:event:consume --workers=4
autostart=true
autorestart=true
Payload Size Limits
Event Ordering
sequence_id field if order matters.Symfony Dependency Injection
AsyncEventDispatcher service is not autowired by default. Register it explicitly in services.yaml:
services:
Desarrolla2\AsyncEventDispatcherBundle\AsyncEventDispatcher: ~
Check Queue Status
Run php bin/console async:event:list to inspect pending events.
Log Worker Output Add logging to the consumer command:
# config/services.yaml
Desarrolla2\AsyncEventDispatcherBundle\Command\ConsumeCommand:
arguments:
$logger: '@logger'
Test Locally with In-Memory Queue Override the queue service in tests:
$this->container->set('async_event_dispatcher.queue', new InMemoryQueue());
Custom Queue Backend
Replace the default DoctrineQueue with a custom implementation (e.g., RabbitMQ, Redis):
async_event_dispatcher:
queue:
class: App\Queue\RedisQueue
Event Serialization Override the serializer for complex objects:
$event = new Event('complex.event', ['object' => $complexObject]);
$event->setSerializer(function ($payload) {
return serialize($payload);
});
Post-Processing Hooks Extend the consumer to add callbacks:
// In consumer service
$event->afterProcess(function ($event) {
$this->analytics->track($event->getName());
});
Dynamic Worker Scaling
Use environment variables for maximum_num_of_consumers:
async_event_dispatcher:
maximum_num_of_consumers: '%env(int:WORKER_COUNT, 4)%'
How can I help you explore Laravel packages today?