Mastering Background Job Processing in Laravel: A Comprehensive Guide
Introduction
In modern web applications, tasks that take a significant amount of time to complete should be processed asynchronously in the background. Laravel’s robust queue system allows you to achieve this by dispatching jobs to be processed outside of the main application flow. In this guide, we’ll explore how to set up background job processing in Laravel using queues, covering everything from configuration to execution.
Step 1: Configure Queues
- Open the
.env
file in your Laravel project. - Locate the
QUEUE_CONNECTION
parameter and set it todatabase
,redis
,beanstalkd
, orsqs
.
QUEUE_CONNECTION=database
- Run the following commands to create the required database table for queues
php artisan queue:table php artisan migrate
Step 2: Create a Job
- Generate a job class using the Artisan command
php artisan make:job SendPushNotification
- In the
app/Jobs/SendPushNotification.php
file, implement theShouldQueue
interface and define thehandle
method. This method will execute the background task.
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class SendPushNotification implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $smPayload; // Push notification payload as per you need. public function __construct(SMPushPayload $smPayload) { $this->smPayload = $smPayload; } public function handle() { $smPush = new SMPush(); $response = $smPush->send($this->smPayload); // Handle the response, log, update database, etc. } }
Step 3: Dispatch the Job
- In your controller or service, create an instance of the payload class.
$smPayload = new SMPushPayload(/* ... */);
- Dispatch the job using Laravel’s
dispatch
method
use App\Jobs\SendPushNotification; dispatch(new SendPushNotification($smPayload));
Step 4: Run the Queue Worker
- Open a new terminal window.
- Run the following command to start the queue worker
php artisan queue:work
Step 5: Setting up cronjob
- Create a Dedicated Queue Worker Script
- Instead of directly using the
php artisan queue:work
command, create a dedicated shell script that checks if a queue worker is already running and starts it only if it’s not. Create a new file namedrun_queue.sh
and replace/path/to/your/laravel/project
with the actual path to yourLaravel project's
directory.
- Instead of directly using the
#!/bin/bash # Path to Laravel project directory PROJECT_DIR="/path/to/your/laravel/project" # Check if queue worker is already running if ! pgrep -f "artisan queue:work"; then cd "$PROJECT_DIR" php artisan queue:work --daemon fi
- Make the Script Executable:
- If you are using
shared cPanel
, you don’t need to worry about this.
- If you are using
chmod +x run_queue.sh
- Set Up a Cron Job:
- Set up a cron job that runs the
run_queue.sh
script every 10 minute. - Replace
/path/to/your/laravel/project
with the actual path to your Laravel project’s directory. - Type
crontab -e
to add cronjob from terminal and add the script provided bellowcrontab -e
.
- Set up a cron job that runs the
crontab -e
*/10 * * * * /bin/sh /path/to/your/laravel/project/run_queue.sh >> '/dev/null' 2>&1
With this setup, the cron job will execute the run_queue.sh
script every minute. The script checks if a queue worker process is already running using the pgrep
command. If no queue worker process is found, it will start a new one using the php artisan queue:work --daemon
command.
- If you want to terminate the existing queue worker process type following command in your terminal.
pkill -f "artisan queue:work"
- If you want to see the list of all running processes and then filter that list to only display lines that contain the string use following command.
ps aux | grep "php artisan queue:work"
This approach ensures that only one instance of the queue worker is running at any given time. If a queue worker is already active, the script will skip starting a new one.
Conclusion
By utilizing Laravel’s powerful queue system, you can enhance your application’s performance and responsiveness by processing time-consuming tasks in the background. Background job processing ensures a seamless user experience and improved scalability. With the configuration, job creation, dispatch, and execution covered in this guide, you’re well on your way to mastering background job processing in Laravel.