Home > Backend Development > PHP Tutorial > Cleaner Queue Chains with Laravel's Enum Integration

Cleaner Queue Chains with Laravel's Enum Integration

Johnathan Smith
Release: 2025-03-10 11:53:11
Original
825 people have browsed it

Cleaner Queue Chains with Laravel's Enum Integration

Laravel now supports the use of enums with fallback values ​​directly in the onQueue method of Bus facade, without manually accessing the value attribute of the enumeration. This improvement creates cleaner and more expressive code when handling job chains and queues.

This enhancement is especially useful when building complex job pipelines that need to be directed to different queues based on priority, resource requirements, or business logic.

use App\Enums\QueueType;

// 直接使用枚举,无需 ->value
Bus::chain($jobs)
    ->onQueue(QueueType::Background)
    ->dispatch();
Copy after login

The following is an actual example of implementing a document processing system:

<?php namespace App\Enums;

enum ProcessingQueue: string
{
    case Immediate = 'realtime';
    case Standard = 'default';
    case Batch = 'batch-process';
    case LowPriority = 'low-priority';
}

namespace App\Services;

use App\Enums\ProcessingQueue;
use App\Jobs\ProcessDocument;
use App\Jobs\GenerateThumbnail;
use App\Jobs\ExtractMetadata;
use App\Jobs\NotifyUser;
use App\Models\Document;
use Illuminate\Support\Facades\Bus;

class DocumentProcessor
{
    public function process(Document $document, bool $isPriority = false)
    {
        $queue = $isPriority
            ? ProcessingQueue::Immediate
            : ProcessingQueue::Standard;

        Bus::chain([
            new ProcessDocument($document),
            new ExtractMetadata($document),
            new GenerateThumbnail($document),
            new NotifyUser($document->user, 'Document processing complete')
        ])
        ->onQueue($queue)
        ->dispatch();

        return $document;
    }

    public function batchProcess(array $documentIds)
    {
        foreach ($documentIds as $id) {
            $document = Document::find($id);

            Bus::chain([
                new ProcessDocument($document),
                new GenerateThumbnail($document)
            ])
            ->onQueue(ProcessingQueue::Batch)
            ->dispatch();
        }
    }
}
Copy after login

This enhancement simplifies queue implementation while maintaining type safety and improving code readability.

The above is the detailed content of Cleaner Queue Chains with Laravel's Enum Integration. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template