Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

A Practical Guide to Mastering Laravel Jobs and Queues

Published

Laravel’s job and queue system is a powerful feature that helps you manage tasks that take a lot of time to complete. By offloading these tasks to run in the background, you can significantly improve your application’s performance and ensure it stays responsive to users.

Overview

In Laravel, jobs allow you to bundle up tasks that need to be done asynchronously—meaning they don’t have to happen immediately and can run in the background. This is especially useful for tasks like sending emails, processing large files, or generating reports, which can take time and might slow down your application if done directly within the main workflow.

Queues come into play by holding these jobs until the system is ready to process them. Instead of making users wait for a task to complete, Laravel queues the task and processes it later when the system has the resources. This way, your application can handle other requests without delay, keeping it fast and responsive.

By using Laravel’s job and queue system, you ensure that your application can efficiently manage background tasks without compromising user experience. This approach makes your application more robust, as it can handle multiple tasks in the background while staying quick and efficient on the front end.

Step-by-Step Guide to Implementing Jobs in Laravel

Now, Let’s get started with the step-by-step tutorial. First of all, we have to set up the queue driver. Note that Laravel allows you to select one from many different queue drivers supported. So, It is very important to make sure you are using the correct queue driver before moving forward. If the queue driver is not configured correctly, Your jobs might fail.

Step 1: Setting Up a Queue Driver

Choose a Queue Driver:

Laravel supports several queue drivers, including Database, Redis, and more. For this example, we will use the database driver. Update your .env file to set the queue driver:

QUEUE_CONNECTION=database

Run Queue Migration:

Laravel provides a migration to create the necessary database tables for the database queue driver. Run the migration:

php artisan queue:table
php artisan migrate

Note: If your project requires heavy background processes, We recommend you to avoid using Database as a queue driver. If possible, always use Redis queue driver as it is fastest as the pending jobs will be stored in an in-memory redis database.

Step 2: Creating a Job

A job is a specific task that you want to process on a specific model. Creating a job in Laravel is a very simple task. Execute the following artisan command to create new jobs as required.

php artisan make:job ProcessTask

This command creates a file named ProcessTask.php in the app/Jobs directory.

Define the Job Logic:

Open app/Jobs/ProcessTask.php and define the logic to be executed when the job is processed. Here’s an example:

// app/Jobs/ProcessTask.php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;

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

    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function handle()
    {
        // Task processing logic
        // For example, sending an email
        Mail::to($this->data['email'])->send(new \App\Mail\TaskProcessed($this->data));
    }
}

Here is some explaination on the new words you might see in the Job class.

ShouldQueue: Indicates that this job should be queued.
Constructor: Accepts any data you need to pass to the job.
handle method: Contains the logic to be executed when the job is processed.

Once the job logic is defined, We can execute the job for any model. For that, we will dispatch jobs as required. Let’s learn how to do that.

Step 3: Dispatching the Job

You can dispatch the job from anywhere in your application. For example, dispatch the ProcessTask job from a controller:

// app/Http/Controllers/TaskController.php

use App\Jobs\ProcessTask;
use Illuminate\Http\Request;

public function store(Request $request)
{
    $data = $request->all();

    // Dispatch the job
    ProcessTask::dispatch($data);

    return response()->json(['status' => 'Task is being processed!']);
}

Step 4: Implementing Delayed Jobs

Laravel allows you to delay the execution of a job. To delay the ProcessTask job by 5 minutes, you can use the delay method when dispatching the job:

// app/Http/Controllers/TaskController.php

use App\Jobs\ProcessTask;
use Carbon\Carbon;

public function store(Request $request)
{
    $data = $request->all();

    // Dispatch the job with a delay
    ProcessTask::dispatch($data)->delay(Carbon::now()->addMinutes(5));

    return response()->json(['status' => 'Task will be processed in 5 minutes!']);
}

Delaying jobs might help you in some cases. For example, You want to send an email 3-5 hours after a user has registered on your system, It is possible with the delayed jobs.

Step 5: Running the Queue Worker

Start the Queue Worker:

To process jobs, you need to start a queue worker. This command will start processing jobs from the queue. Run the following Artisan command:

php artisan queue:work

The above given command will execute all the pending jobs in all the queues. In real world, You will not manually execute the queue:work command to start processing pending jobs. In the next steps, we will learn How to automate task executing with the help of Supervisor.

Supervising the Queue Worker:

Ensure you supervise this process using tools like Supervisor to keep it running. Supervisor is a process control system that allows you to monitor and control a number of processes on UNIX-like operating systems.

Installing Supervisor:

Run the following command to install Supervisor:

sudo apt-get install supervisor

It will only take a few minutes to install supervisor as it is a very simple application. Once done, You can create a supervisor file for all your queues. Let’s learn how.

Configuring Supervisor:

Create a configuration file for your Laravel queue worker:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Add the following configuration:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-your-project/artisan queue:work --daemon
autostart=true
autorestart=true
user=your-username
numprocs=1
redirect_stderr=true
stdout_logfile=/path-to-your-project/storage/logs/worker.log

Replace /path-to-your-project/ with the actual path to your Laravel application and your-username with your system username.

This file/configuration is the beating heart of your processes. It is very simple to understand. There is one major directive you can see named numprocs. It is the number of processes you want to start for any specific queue. If you want to process more jobs at once, increase the numprocs directive according to the requirement. In simple terms, It is the number of processes you want to run simultaneously/parallelly.

Start Supervisor:

Run the following commands to update and start Supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

The above given three commands are essential when it comes to updating your queues. Whenever you change anything in your supervisor configuration file, You have to execute these commands to restart the supervisor with correct configuration.

Benefits of Using Laravel Jobs and Queues

  • Improved Application Performance: You keep your application responsive by offloading time-consuming tasks to a background process.
  • Scalability: Easily scale your application by adding more workers to handle an increased number of jobs.
  • Decoupled Architecture: Separates the logic of task handling from the main application flow, making your code cleaner and more maintainable.

Conclusion

Laravel’s job and queue system is a powerful feature that enhances your application’s performance and scalability. By following these steps, you can efficiently manage and process background tasks, ensuring your application remains responsive and robust. Implementing jobs and queues in Laravel not only improves user experience but also helps maintain a clean and organized codebase.

Account services are unavailable until the production backend is configured.