How to Run Heavy Process in Background Using Queue in Laravel

How to Run Heavy Process in Background Using Queue in Laravel - Laravel Queue Tutorial

In Laravel, you can use queues to handle heavy processing tasks in the background, improving performance and user experience.

Step 1: Install & Configure Queue Driver

By default, Laravel uses the sync driver. To process jobs in the background, configure a queue driver.

    
    // Open .env and set queue connection to database
    QUEUE_CONNECTION=database
    
    // Run migration to create queue tables
    php artisan queue:table
    php artisan migrate
    
    

Step 2: Create a Job

Run the following command to create a new job:

php artisan make:job HeavyProcessJob

Modify the generated job file (app/Jobs/HeavyProcessJob.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 Illuminate\Support\Facades\Log;

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

        protected $data;

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

        public function handle()
        {
            sleep(10); // Simulating a heavy process
            Log::info("Heavy process completed for: " . json_encode($this->data));
        }
    }
    
    

Step 3: Dispatch the Job

Dispatch the job from a controller or any service:

    
    use App\Jobs\HeavyProcessJob;
    
    $data = ['task' => 'Process large file', 'user_id' => 1];
    dispatch(new HeavyProcessJob($data));
    
    

Step 4: Process the Queue

Run the following command to process queued jobs:

php artisan queue:work

For background execution:

nohup php artisan queue:work --daemon > /dev/null 2>&1 &

Step 5: Using Supervisor for Queue Management (Production)

Install Supervisor and configure it:

    
    sudo apt update
    sudo apt install supervisor
    sudo nano /etc/supervisor/conf.d/laravel-worker.conf
    
    

Add the following configuration:

    
    [program:laravel-worker]
    command=php /path-to-your-project/artisan queue:work --tries=3
    autostart=true
    autorestart=true
    user=www-data
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/var/log/laravel-worker.log
    
    

Then reload and start Supervisor:

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

Step 6: Monitoring & Debugging

Check queued jobs:

php artisan queue:failed

Retry failed jobs:

php artisan queue:retry all

Clear queued jobs:

php artisan queue:flush

Conclusion

Now, your Laravel application can handle heavy tasks asynchronously using queues. This improves performance and user experience significantly.

0 Comments