VeldoraDocs

12. Background Queues & Jobs

Queues allow you to defer time-consuming tasks (like sending emails, processing images, or calling third-party webhooks) to a background process, dramatically speeding up web request response times.

Queue Drivers

Configure the queue driver in .env:

.env
INI
QUEUE_DRIVER=database   # Options: sync, database

DriverDescriptionBest For
syncRuns jobs immediately in the same processLocal debugging & testing
databaseStores jobs in the jobs database table and processes asynchronouslyProduction apps

Creating a Job

Terminal • bash
php veldora make:job ProcessVideoEncoding

example.php
PHP
// app/Jobs/ProcessVideoEncoding.php
namespace App\Jobs;

use Veldora\Framework\Queue\Job;

class ProcessVideoEncoding extends Job
{
    public int $maxTries = 3;     // Maximum attempts before failing
    public int $retryAfter = 60;  // Delay in seconds between retries

    public function __construct(
        public readonly int $videoId,
        public readonly string $format = 'mp4'
    ) {}

    public function handle(): void
    {
        // Heavy processing logic runs in background worker
        log_info("Encoding video {$this->videoId} to {$this->format}");

        // Perform encoding...
    }
}

Dispatching Jobs

example.php
PHP
use App\Jobs\ProcessVideoEncoding;

// Dispatch immediately to default queue
ProcessVideoEncoding::dispatch($video->id);

// Dispatch with a delay (runs after 2 minutes)
ProcessVideoEncoding::dispatch($video->id)->delay(120);

// Dispatch to a specific queue channel
ProcessVideoEncoding::dispatch($video->id)->onQueue('media');

Running the Queue Worker

Run the background worker via the CLI:

Terminal • bash
# Process jobs continuously on default queue
php veldora queue:work

# Specify queue channel and sleep interval
php veldora queue:work --queue=media,default --sleep=3 --tries=3


Building with AI Assistants?
Copy the Veldora AI Developer Prompt to teach ChatGPT, Claude, Cursor, or Gemini all framework APIs for error-free coding.