-
-
Save muihlinn/bcb17fc0b6425bcb6f26ac907bc91c10 to your computer and use it in GitHub Desktop.
Laravel >=5 Queue monitoring in Shared Hosting (using queue:work) with auto restart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/usr/bin/php /path/to/artisan schedule:run >/dev/null 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console; | |
use Illuminate\Console\Scheduling\Schedule; | |
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; | |
class Kernel extends ConsoleKernel | |
{ | |
/** | |
* The Artisan commands provided by your application. | |
* | |
* @var array | |
*/ | |
protected $commands = [ | |
]; | |
/** | |
* Define the application's command schedule. | |
* | |
* @param \Illuminate\Console\Scheduling\Schedule $schedule | |
* @return void | |
*/ | |
protected function schedule(Schedule $schedule) | |
{ | |
$schedule->call(function() { | |
// Useful variables | |
$op = []; // return from exec() | |
$deadline = 75; // process life, in minutes. | |
$deadline *= 60; // convert to use with time() | |
$path = base_path(); | |
$php_executable = '/usr/bin/php'; // absolute path to php excutable | |
$running = false; // are queues running ? | |
$restart = false; // have to restart queues ? | |
$since = 0; // since when queues are running | |
$pid_file = $path . '/queue.pid'; // File which contains the queue pid | |
$since_file = $path . '/running_since'; // File which contains the start timing | |
// Commands to start workers, or restarting them. | |
$queue_up = "$php_executable -c " . $path .'/php.ini ' . $path . | |
'/artisan queue:work --daemon --tries=3 --sleep=5 > /dev/null & echo $!' ; | |
$queue_restart = "$php_executable -c " . $path .'/php.ini ' . $path . | |
'/artisan queue:restart > /dev/null ' ; | |
// Check if pid file exists && queues are running | |
if (file_exists($pid_file)) { | |
$pid = file_get_contents($pid_file); | |
// --no-headings isn't portable, sed does the same working in more un*xes, like OS X. | |
exec("ps -p $pid | sed 1d | awk '{print $1}'", $op); | |
$result = isset($op[0]) ? (int)end($op) : null; | |
$running = $result != '' ? true : false; // it's running ? | |
} | |
else { | |
$running = false; // queues aren't running | |
} | |
// checks if queues have been running for too long | |
if(file_exists($since_file)) { | |
$since = file_get_contents($since_file); | |
$since = $since == '' ? 0 : $since; // empty values are 0 | |
$restart = ( (time() - ($since + $deadline)) >= 0 ) ? true : false ; | |
} | |
else { | |
$restart = true; // there it wasn't a queue file, so a restart isn't needed | |
} | |
// Act as needed | |
if ($running && $restart){ // Process exists && needs restart | |
exec($queue_restart); // kills processes gracefully | |
exec($queue_up, $op); // ups the process. | |
if(isset($op[0])){ | |
file_put_contents($pid_file, end($op)); | |
file_put_contents($since_file, time()); | |
} | |
\Log::info('Queues have been restarted'); | |
} | |
elseif(!$running) { // process doesn't exists anymore. Restart it. | |
exec($queue_up, $op); | |
if(isset($op[0])){ | |
file_put_contents($pid_file, end($op)); | |
file_put_contents($since_file, time()); | |
\Log::info('Queues weren\'t running'); | |
} | |
} | |
})->name('monitor_queue_listener')->everyFiveMinutes(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
register_argc_argv=On |
Author
muihlinn
commented
Jun 10, 2019
via email
Give me more details, please, as the worker as daemon it's
intended to be a long lived process, it doesn't quit after
processing the pending jobs.
It should be there.
This has been running flawlessly in several servers for a couple
of years now.
burasuk writes:
… This dont't work.
Command in $queue_up is run and save pid to file, but the pid
don't exist in
processes, when next schedule cycle is run.
--
Hell is full of musical amateurs.
--- George Bernard Shaw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment