How to save a Laravel queue system on a server

I recently installed the Laravel queue system. Basics - cronjob calls a command that adds jobs to the queue and calls a second command that sends an email.

The system works when I transfer ssh to my server and start the php artisan queue: listen, but if I close my terminal, the listener will turn off and the tasks will stack and queue until I return to ssh and start listening again.

What is the best way to keep the queue system in the background without having to open my connection through ssh?

I tried to run php artisan queue:work --daemon and it completed the jobs in the queue, but when I closed the terminal, it closed the connection and the background process.

+47
crontab laravel artisan
source share
12 answers

Team

 nohup php artisan queue:work --daemon & 

was correct, this would allow to continue the process after closing the SSH connection; however, this is only a short-term solution. As soon as your server is rebooted or any problem will stop the process, you will need to go back and run the command again. When this happens, you will never know. This can happen on Friday night, so it’s best to implement a long-term solution.

I switched to Supervisord, it can be installed on Ubuntu as easy as

 sudo apt-get install supervisor 

For AWS-AMI or RedHat users, you can follow the set of instructions described in this question:

Configuring Supervisord on AWS AMI Linux Server

+12
source share

Performance

 nohup php artisan queue:work --daemon & 

Prevents the exit of the command when exiting the system.

The final ampersand (&) forces the process to run in the background, so you can continue to use the shell and not wait for the script to complete.

See nohup

nohup - run a command that does not depend on freezes with access to non-tty

This displays information in a file called nohup.out in the directory in which you run the command. If you have no interest in exiting, you can redirect stdout and stderr to / dev / null, or in the same way, you can output it to your regular laravel log. for example

 nohup php artisan queue:work --daemon > /dev/null 2>&1 & nohup php artisan queue:work --daemon > app/storage/logs/laravel.log & 

But you should also use something like Supervisord to make sure the service remains running and restarts after crashes / crashes.

+54
source share

You must use Linux Manager

Installation is simple, and on Ubuntu I can install it with the following command:

 apt-get install supervisor 

Supervisor configuration files are located in the /etc/supervisor/conf.d directory.

 [program:email-queue] process_name=%(program_name)s_%(process_num)02d command=php /var/www/laravel-example/artisan queue:work redis --queue=emailqueue --sleep=3 --tries=3 autostart=true autorestart=true user=forge numprocs=2 redirect_stderr=true stdout_logfile=/var/www/laravel-example//storage/logs/supervisord.log 

For each process, you must create a new process configuration file. With this configuration, the listener will repeat each task three times. In addition, Supervisor will restart the listener if it does not work or the system restarts.

+20
source share

From https://gist.github.com/ivanvermeyen/b72061c5d70c61e86875

 <?php namespace App\Console\Commands; use Illuminate\Console\Command; class EnsureQueueListenerIsRunning extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'queue:checkup'; /** * The console command description. * * @var string */ protected $description = 'Ensure that the queue listener is running.'; /** * Create a new command instance. */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return void */ public function handle() { if ( ! $this->isQueueListenerRunning()) { $this->comment('Queue listener is being started.'); $pid = $this->startQueueListener(); $this->saveQueueListenerPID($pid); } $this->comment('Queue listener is running.'); } /** * Check if the queue listener is running. * * @return bool */ private function isQueueListenerRunning() { if ( ! $pid = $this->getLastQueueListenerPID()) { return false; } $process = exec("ps -p $pid -opid=,cmd="); //$processIsQueueListener = str_contains($process, 'queue:listen'); // 5.1 $processIsQueueListener = ! empty($process); // 5.6 - see comments return $processIsQueueListener; } /** * Get any existing queue listener PID. * * @return bool|string */ private function getLastQueueListenerPID() { if ( ! file_exists(__DIR__ . '/queue.pid')) { return false; } return file_get_contents(__DIR__ . '/queue.pid'); } /** * Save the queue listener PID to a file. * * @param $pid * * @return void */ private function saveQueueListenerPID($pid) { file_put_contents(__DIR__ . '/queue.pid', $pid); } /** * Start the queue listener. * * @return int */ private function startQueueListener() { //$command = 'php-cli ' . base_path() . '/artisan queue:listen --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.1 $command = 'php-cli ' . base_path() . '/artisan queue:work --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.6 - see comments $pid = exec($command); return $pid; } } 
+5
source share

The best way is PM2 (Advanced, the production process manager for Node.js), which allows you to track your queues and view their logs.

with the command below in your project directory, run the worker queue:

 pm2 start artisan --name laravel-worker --interpreter php -- queue:work --daemon 
+4
source share

1) sudo apt install supervisor or

 sudo apt-get install supervisor 

2) cd /etc/supervisor/conf.d 3) create a new file inside

 sudo vim queue-worker.conf 

File contents

 [program:email-queue] process_name=%(program_name)s_%(process_num)02d command=php /var/www/html/laravelproject/artisan queue:work autostart=true autorestart=true user=root numprocs=2 redirect_stderr=true stdout_logfile=/var/www/html/laravelproject/storage/logs/supervisord.log 

4) sudo supervisorctl reread

when you run this command, get the output worker queue: available

5) sudo supervisorctl update

when this command is run, get the exit worker queue: a process group has been added

another team

1) sudo supervisorctl reload

when running this command get output restarted supervisor

2) sudo service supervisor restart

+4
source share

For those who already operate NodeJS in their production environments. I use PM2 to control application processes.

 # install npm install -g pm2 # in project dir with your CI or dev setup tool # --name gives task a name so that you can later manage it # -- delimits arguments that get passed to the script pm2 start artisan --interpreter php --name queue-worker -- queue:work --daemon 

I use Vagrant to develop and configure NodeJS and this process using only built-in roaming scripts.

When you use PM2 in development, you can use one of many observers to control the reboot. Just run pm2 restart queue-worker when you get the change. In production, I do not recommend this approach, but prefer a building tool that can follow this process.

 # 1. stop pm task to ensure that no unexpected behaviour occurs during build pm2 stop queue-worker # 2. do your build tasks ... # 3. restart queue so that it loads the new code pm2 restart queue-worker 
+3
source share

What if you start listening on the screen? See here: http://aperiodic.net/screen/quick_reference Then even if you log out, the screen will still be active and working. Not sure why demonization doesn't work.

+2
source share

Using pm2

I had a JS script working with pm2 (Advanced, process manager for Node.js). It was the only one I ran. But now that I’ve got another process to keep working.

I created process.yml to run as a single command. Check if the first php artisan queue: listen

 # process.yml at /var/www/ which is root dir of the project apps: # Run php artisan queue:listen to execute queue job - script : 'artisan' name : 'artisan-queue-listen' cwd : '/var/www/' args : 'queue:listen' # or queue:work interpreter : 'php' # same way add any other script if any. 

Now run:

 > sudo pm2 start process.yml 

Check advanced parameters and pm2 function

+2
source share

Since this was a specific question related to Laravel, I thought I was offering a specific answer to Lravel. Since you are already using cronjob on this server, I would recommend that you configure the shell command as a repeating cronjob to always check that the worker is working. You can either configure the shell command to run initially through cron on your server, or you can use the Laravel console kernel to manage the command and add logic, for example, check if you already have a working worker and, if not, go ahead and run it .

Depending on how often you need to carry out your team, you can do this infrequently, once a week, or even once a minute. This will give you the opportunity to make sure that your employees are constantly working without adding any overhead to your server, such as Supervisor. Granting permissions for a third-party package, such as a supervisor, is fine if you trust it, but if you can avoid having to rely on it, you might want to consider this approach.

An example of using this to do what you want will be a cronjob that runs every hour. It will do the following in sequential order from the Laravel console user command:

\ Artisan :: call ('queues: reboot');

\ Artisan :: call ('queue: work --daemon');

Please note that this applies to older versions of Laravel (prior to 5.3), but I have not tested them in newer versions.

+2
source share

You can use the monit tool. It is very small and useful for any type of process control and monitoring.

After downloading the binary package from this link, you can extract it to a folder on your system and then copy two files from the package on your system to install it:

 cd /path/to/monit/folder cp ./bin/monit /usr/sbin/monit cp ./conf/monitrc /etc/monitrc 

Now edit the base /etc/monitrc according to your needs ( reference document ). then create an init control file to enable monit at startup. now start monit as follows:

 initctl reload-configuration start monit 
+1
source share

I just used php artisan queue:work --tries=3 & , which supports the process in the background. But sometimes it stops. I do not know why this is happening

Edit

I solved this problem with a supervisor. Put a supervisor script that runs this php script and that will run every time the server starts

-one
source share

All Articles