Install Dependencies
Ensure RabbitMQ is running with the rabbitmq_delayed_message_exchange plugin enabled.
sudo rabbitmq-plugins enable rabbitmq_delayed_message_exchange
Composer Installation
composer require docplanner/tasks-bundle
Register Bundles
Add to app/AppKernel.php:
new OldSound\RabbitMqBundle\OldSoundRabbitMqBundle(),
new DocPlanner\TasksBundle\DocPlannerTasksBundle(),
Configure RabbitMQ Connection
Update config.yml with RabbitMQ credentials (see RabbitMqBundle docs).
Define Task Configuration
Add docplanner_tasks config in config.yml:
docplanner_tasks:
connection_name: default
names_prefix: app_
First Use Case: Send a Task Create a payload and task class, then dispatch:
$payload = new MyTaskPayload();
$this->get('docplanner_tasks.dispatcher')->dispatch(new MyTask(), $payload);
Task Creation
BaseTaskPayload for payloads (serialized via serialize()).BaseTask for jobs, implementing:
supports($payload): Validate payload compatibility.execute(): Task logic (return true on success, false on failure).Dispatching Tasks
Use the dispatcher service to enqueue tasks:
$dispatcher = $this->get('docplanner_tasks.dispatcher');
$dispatcher->dispatch(new MyTask(), $payload);
Consuming Tasks
docplanner_tasks.task:
services:
my_custom_task:
class: AppBundle\Task\MyCustomTask
tags:
- { name: docplanner_tasks.task }
Delayed Tasks Leverage RabbitMQ’s delayed exchange (if plugin is enabled) to schedule tasks:
$dispatcher->dispatchDelayed(new MyTask(), $payload, 3600); // Delay in seconds
execute() in try-catch to handle failures gracefully.$this->logger->info('Task executed', ['task' => get_class($this), 'payload' => $payload]);
dispatcher service in unit tests to avoid RabbitMQ dependencies.Serialization Issues
serialize()/unserialize()-compatible data (e.g., arrays, simple objects, or JSON-encoded strings).Delayed Message Plugin
rabbitmq_delayed_message_exchange, delayed tasks won’t work.sudo rabbitmq-plugins enable rabbitmq_delayed_message_exchange
sudo systemctl restart rabbitmq-server
Connection Failures
OldSoundRabbitMqBundle's connection recovery features.Task Tagging
docplanner_tasks.task) means it won’t be consumed.services.yml or as an annotation.Return Value Misinterpretation
execute() returning false may not trigger retries or dead-letter queues by default.rabbitmqctl list_queues or the RabbitMQ Management UI to verify tasks are enqueued.OldSoundRabbitMqBundle:
old_sound_rabbit_mq:
connections:
default:
logger: true
$this->logger->debug('Serialized payload', ['data' => serialize($payload)]);
Custom Dispatcher Override the default dispatcher to add middleware or logging:
$dispatcher = $this->get('docplanner_tasks.dispatcher');
$dispatcher->addMiddleware(function ($task, $payload) {
// Pre-dispatch logic
});
Task Priorities Use RabbitMQ’s priority queues (if supported) by extending the bundle’s configuration:
docplanner_tasks:
priority_queues: true
Dead-Letter Queues Configure RabbitMQ to route failed tasks to a dead-letter queue:
old_sound_rabbit_mq:
consumers:
my_consumer:
queue_name: my_queue
exchange_options:
dead_letter_exchange: dlx
How can I help you explore Laravel packages today?