Laravel Tutorials

Boosting Queue Management with Laravel's JobQueueing Event

Published
Boosting Queue Management with Laravel's JobQueueing Event image

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 event
Event::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.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article