Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Infinite script with Laravel

Tags:

php

laravel

I have a table users that I need to upload continuously, so once updated, I would like to relaunch the command directly.

Actually, I'm using a cron that launch itself each minute with Laravel ($schedule->command('update:users')->everyMinute();), but I'm losing some time if the job is quicker than one minute of I will overload my server if it is more than one minute.

I was thinking to maybe use a queue, and once the script terminated, relauch itself, like this:

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

But if the script crash, it will not reload itself, and I need to launch it at least once. I know that I could use a pcntl_fork function, but I would like to have a turnkey function with Laravel. How should I do ?

like image 458
Kevin Avatar asked Dec 07 '22 23:12

Kevin


1 Answers

I would suggest running a Command the Cli, in the command place a

while (true)

loop so it will run forever. After you created this script you can run it with supervisord

this service runs the command you tell him, and when it fails it will relaunch it automaticlly. Just be aware that after X failures it will stop, it depends on how you configured it.

Example for conf file in:

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

and contents could 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
like image 76
Tzook Bar Noy Avatar answered Dec 11 '22 12:12

Tzook Bar Noy