Development

Creating and running jobs and worker queues in Laravel 10

Laravel jobs and worker queues are for doing tasks that are too long or intensive to do during a typical web request such as processing or parsing an uploaded file.

These jobs can then be processed through a queue in the background without slowing down your web application and its users.

The driver

There are several choices for what driver you want to use for your queues. For the straightforward database driver, in the .env file set as:

QUEUE_CONNECTION=database

Then pull the jobs queue table migration and migrate it.

php artisan queue:table
php artisan migrate

This creates the table called jobs.

Creating a job

Use artisan to create the job ProcessUpload

php artisan make:job ProcessUpload

This will create /app/jobs/ProcessUpload.php

In this file the Media resource gets passed in with the constructor and the handle() function is what gets called when it is run from the worker queue.

namespace App\Jobs;

use App\Models\Media;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

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

    public function __construct(
        public Media $media,
    )
    {
    }

    public function handle(): void
    {
        //Process upload functions in here...
        Log::debug("Called ProcessUpload job for {$media->id}");
    }
    
}

Dispatching the job

Dispatching the job means sending it to the queue for it to be eventually called

//Example upload
$media = new Media();
$media->original_filename = $file->getClientOriginalName();
$media->type = $file->getClientMimeType();
$media->save();

$file = $file->storeAs("uploads", "{$media->id}.{$file->extension()}", 'private'); 

//Send to the queue
ProcessUpload::dispatch($media);//The App\Jobs\ProcessUpload file

You can also define a queue type:

ProcessUpload::dispatch($media)->onQueue('uploads');

This can also be done in the job’s constructor with:

$this->onQueue('uploads');

There can also be conditions for the job to dispatch:

ProcessUpload::dispatchIf($user_verified, $media);
 
ProcessUpload::dispatchUnless($user_banned, $media);

The job can also be delayed and not available in the worker queue:

ProcessUpload::dispatch($media)->delay(now()->addMinutes(20));

Dispatch and run the job immediately, skipping the queue:

ProcessUpload::dispatchSync($media);

Running the queue

Start the queue worker and process any jobs in it with this artisan command:

php artisan queue:work

This will run until you stop it or close the terminal.

You can also keep the queue worker continuously listening for new jobs to be published. This is said to not be efficient.

php artisan queue:listen

Process just one job:

php artisan queue:work --once

Process a maximum of 20 jobs:

php artisan queue:work --max-jobs=20

Process all jobs and exit:

php artisan queue:work --stop-when-empty

Process jobs for an 1 hour and exit:

php artisan queue:work --max-time=3600

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago