Laravel queues for background job processing

Carlos Mendez Jan 2026
2 tabs
<?php

namespace App\Jobs;

use App\Models\Podcast;
use App\Services\AudioProcessor;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessPodcast implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;
    public $timeout = 300; // 5 minutes

    public function __construct(
        public Podcast $podcast
    ) {}

    public function handle(AudioProcessor $processor): void
    {
        // Process the podcast audio
        $processor->normalize($this->podcast->file_path);
        $processor->generateWaveform($this->podcast);
        $processor->extractMetadata($this->podcast);

        // Update podcast status
        $this->podcast->update(['status' => 'processed']);

        // Dispatch follow-up job
        GeneratePodcastTranscript::dispatch($this->podcast);
    }

    public function backoff(): array
    {
        return [60, 300, 900]; // 1 min, 5 min, 15 min
    }

    public function failed(\Throwable $exception): void
    {
        // Notify admins of failure
        $this->podcast->update(['status' => 'failed']);

        \Log::error('Podcast processing failed', [
            'podcast_id' => $this->podcast->id,
            'error' => $exception->getMessage(),
        ]);
    }
}
2 files · php Explain with highlit

Queues offload time-consuming tasks to background workers, keeping web requests fast. I create job classes that implement the ShouldQueue interface and define a handle() method. Jobs are dispatched with dispatch() or Job::dispatch() and run asynchronously via queue workers. The tries property limits retry attempts, while backoff() defines exponential backoff between retries. Failed jobs go to a failed jobs table for manual inspection. I use onQueue() to route jobs to specific queues—high-priority jobs to a dedicated queue. Job chaining with Bus::chain() runs jobs sequentially, while batching groups related jobs. The queue system supports multiple drivers: database, Redis, SQS, or Beanstalkd. This architecture handles millions of jobs reliably.