Laravel's queue system has become even more powerful with the new JobQueueing event. This pre-dispatch hook creates fresh opportunities for job monitoring and manipulation right before they enter your queue system.
The event-driven architecture of Laravel's queue system already offers several lifecycle hooks like JobProcessing and JobProcessed. The JobQueueing event extends this capability by allowing access to an earlier phase - precisely when a job is about to be sent to your queue provider.
Here's a simple implementation of this new event:
use Illuminate\Queue\Events\JobQueueing;use Illuminate\Support\Facades\Event; // Register a listener for the JobQueueing eventEvent::listen(function (JobQueueing $event) { // Extract information about the queuing job $job = $event->job; $connectionName = $event->connectionName; $payload = $event->payload; // Execute pre-queue logic here});
This functionality opens up numerous practical applications for queue management and monitoring. For instance, you could implement environment-specific queue logging:
<?php namespace App\Providers; use Illuminate\Queue\Events\JobQueueing;use Illuminate\Support\Facades\Event;use Illuminate\Support\ServiceProvider;use Illuminate\Support\Facades\Log; class AppServiceProvider extends ServiceProvider{ public function boot(): void { // Create a local development job logger Event::listen(function (JobQueueing $event) { if (app()->environment('local')) { Log::info('Queueing job', [ 'job' => get_class($event->job), 'connection' => $event->connectionName, 'queue' => $event->payload['queue'] ?? 'default' ]); } }); }}
This approach allows teams to build comprehensive queue activity dashboards or implement specialized logic that needs to execute just before jobs enter the queue system. You could extend this to add custom metadata to jobs, enforce rate limiting for specific job types, or dynamically redirect jobs to different queues based on system load.