laravel-queues

fusengine 的头像作者 fusengine

Implement background jobs with queues, workers, batches, chains, events, broadcasting, and failure handling. Use when processing async tasks, sending emails, or handling long-running operations.

📖 中文导读:Implement background jobs with queues, workers, batches, chains, events, broadcasting, and failure handling. Use when processing async tasks, sending emails, or handling long-running operations. 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: laravel-queues description: Implement background jobs with queues, workers, batches, chains, events, broadcasting, and failure handling. Use when processing async tasks, sending emails, or handling long-running operations.

Laravel Queues & Events

Documentation

Queues & Jobs

Events & Broadcasting

Notifications & Mail

Caching & Storage

Monitoring

Job Class

<?php

declare(strict_types=1);

namespace App\Jobs;

final class SendWelcomeEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 3;
    public int $backoff = 60;

    public function __construct(
        public readonly User $user,
    ) {}

    public function handle(EmailService $emailService): void
    {
        $emailService->sendWelcome($this->user);
    }

    public function failed(\Throwable $exception): void
    {
        Log::error('Welcome email failed', ['user_id' => $this->user->id]);
    }
}

Events & Listeners

// Event
class OrderShipped
{
    public function __construct(
        public readonly Order $order,
    ) {}
}

// Listener
class SendShipmentNotification
{
    public function handle(OrderShipped $event): void
    {
        $event->order->user->notify(new OrderShippedNotification($event->order));
    }
}

// Dispatch
OrderShipped::dispatch($order);

Job Batches

Bus::batch([
    new ProcessPodcast($podcast1),
    new ProcessPodcast($podcast2),
])
->then(fn (Batch $batch) => Log::info('All completed!'))
->catch(fn (Batch $batch, Throwable $e) => Log::error('Failed'))
->dispatch();