Installation:
composer require enqueue/enqueue-bundle
Add to config/bundles.php:
return [
// ...
Enqueue\Bundle\EnqueueBundle::class => ['all' => true],
];
Configure a Transport (e.g., Redis):
# config/packages/enqueue.yaml
enqueue:
clients:
default:
dsn: 'redis://localhost'
First Use Case:
Define a job class (e.g., app/Jobs/SendEmailJob.php):
use Enqueue\Client\Job;
use Enqueue\Util\JSON;
class SendEmailJob implements Job
{
public function __construct(private string $email) {}
public function run(): void
{
// Logic to send email
}
public function serialize(): string
{
return JSON::encode(['email' => $this->email]);
}
public static function deserialize(string $data): self
{
$result = JSON::decode($data, true);
return new self($result['email']);
}
}
Dispatch the job in a controller:
use Enqueue\Client\ProducerInterface;
public function __construct(private ProducerInterface $producer) {}
public function sendEmail(): void
{
$this->producer->send(new SendEmailJob('user@example.com'));
}
Producer-Consumer Pattern:
ProducerInterface (e.g., in controllers, commands, or services).
$this->producer->send(new ProcessOrderJob($orderId));
enqueue.consumer tag:
services:
App\Consumer\OrderConsumer:
tags: ['enqueue.consumer']
class OrderConsumer implements ConsumerInterface
{
public function run(): void
{
while ($job = $this->client->receive()) {
$job->run();
$this->client->acknowledge($job);
}
}
}
Job Serialization:
Job interface with serialize()/deserialize() methods.Enqueue\Util\JSON for simple types or custom serializers for complex objects.Retry Logic:
enqueue.yaml:
enqueue:
clients:
default:
dsn: 'redis://localhost'
retry_strategy:
max_attempts: 3
delay: 1000
Delayed Jobs:
$this->producer->send(new SendEmailJob('user@example.com'), 60); // Delay: 60 seconds
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ProcessQueueCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->container->get('enqueue.consumer')->run();
return Command::SUCCESS;
}
}
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class OrderConsumer implements ConsumerInterface
{
public function __construct(private EventDispatcherInterface $dispatcher) {}
public function run(): void
{
while ($job = $this->client->receive()) {
$job->run();
$this->dispatcher->dispatch(new JobProcessedEvent($job));
$this->client->acknowledge($job);
}
}
}
Connection Issues:
enqueue.yaml for correct dsn configuration.php bin/console debug:container enqueue.client
Job Serialization Failures:
Enqueue\Util\JSON or a custom serializer for complex objects.Consumer Stuck in Loop:
Duplicate Job Processing:
acknowledge()), jobs may be reprocessed.acknowledge() after successful processing.Symfony Cache Conflicts:
enqueue.yaml:
php bin/console cache:clear
enqueue:
clients:
default:
dsn: 'redis://localhost'
logger: '@monolog.logger.enqueue'
Custom Transports:
Enqueue\Client\Transport\TransportInterface for custom backends (e.g., AWS SQS).Middleware:
enqueue:
clients:
default:
middleware:
- '@App\Middleware\LoggingMiddleware'
Dynamic Routing:
$producer->send(new Job(), 'high_priority_queue');
Monitoring:
default client is auto-configured but can be overridden in enqueue.yaml.%env(resolve: QUEUE_DSN)% for environment variables:
enqueue:
clients:
default:
dsn: '%env(resolve: QUEUE_DSN)%'
How can I help you explore Laravel packages today?