PHP Infinite script with Laravel

I have table users that I need to load continuously, so after updating I would like to restart the command directly.

In fact, I use cron, which runs every minute with Laravel ( $schedule->command('update:users')->everyMinute();), but I lose some time if the work is faster than one minute. I will overload my server if it is more than one minute.

I thought I could use the queue, and as soon as the script exits, relauch itself, like this:

// Do My stuff
Queue::push(new UpdateUsers($));

But if the script crashes, it will not restart, and I need to run it at least once. I know that I could use the pcntl_fork function, but I would like to have a turnkey function with Laravel. How can I do it?

+4
source share
2 answers

I would suggest running the Cli command, at the location of the a command

while (true)

so that it works forever. After creating this script, you can run it with supervisord

this service launches the command you tell him, and when it fails, it will restart it automatically. Just keep in mind that after X crashes, it will stop, it depends on how you configured it.

An example for a conf file in:

/etc/supervisord/conf.d/my_infinite_script.conf

and the content may be:

[program:laravel_queue]
command=php artisan your_command:here
directory=/path/to/laravel
autostart=true
autorestart=true
stderr_logfile=/var/log/your_command.err.log
stdout_logfile=/var/log/your_command.out.log
+9
source

In some cases, I used the approach suggested by Tzook Bar Noy, but also used a slightly ugly method, which can avoid script looping problems forever if this can cause problems. This can be called every minute in a cronjob:

$runForSeconds = 55;
$runMinute = date('i');
do {
    ....code....
} while (date('i') == $runMinute && intval(date('s')) < $runForSeconds);

But the best solution would be to use the job queue and start it using the dispatcher and:

command=php artisan queue:listen
+2
source

Source: https://habr.com/ru/post/1626884/


All Articles