How to implement Queue with Supervisor
1. Create Job table
php artisan queue:table
php artisan migrate
2. Create Job in Laravel Project
php artisan make:job testQueue
QUEUE_CONNECTION=database
3. Add Job to Queue
See example below :
function addJob(){
$call = (new \App\Jobs\testQueue("Hello I'm running new job"))
->delay(now()->addSeconds(2))->onQueue('testQueue');
dispatch($call);
}
4. Config Supervisor
4.1 Install Supervisor
sudo apt update && sudo apt install supervisor
4.2 Add Config to Laravel-worker
nano /etc/supervisor/conf.d/laravel-worker.conf
Example below is to add multi Queues (testQueue1 and testQueue2) :
[program:testQueue1]
process_name=%(program_name)s_%(process_num)02d
command=php your_project_path/artisan queue:testQueue1 --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=your_project_path/storage/logs/worker-laravel.log
[program:testQueue2]
process_name=%(program_name)s_%(process_num)02d
command=php your_project_path/artisan queue:work --queue=testQueue2 --sleep=3 --tries=2
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=your_project_path/storage/logs/worker-laravel.log
stopwaitsecs=3600
[group:laravel-worker]
programs=testQueue1,testQueue2
priority=999
4.3 Restart Supervisor & Queue
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*php artisan queue:restartphp artisan cache:clearphp artisan config:cache