Forked from ivanvermeyen/EnsureQueueListenerIsRunning.php
Created
October 28, 2017 17:40
-
-
Save KABBOUCHI/2e5ba2f3dc25d0ba7bcab0cc74e74d5b to your computer and use it in GitHub Desktop.
Ensure that the Laravel 5.1 queue listener is running with "php artisan queue:checkup" and restart it if necessary. You can run this automatically with a cron job: http://laravel.com/docs/scheduling
This file contains 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
#!/bin/bash | |
# | |
# You can run this bash script with a cron job | |
# or just run the command below. | |
# | |
# I had to use php-cli for both the cron job and the | |
# call to queue:listen in "EnsureQueueListenerIsRunning.php" | |
# to avoid getting the exception: | |
# 'ErrorException' with message 'Invalid argument supplied for foreach()' | |
# in /path/to/vendor/symfony/console/Input/ArgvInput.php:283 | |
# | |
php-cli /path/to/artisan schedule:run >/dev/null 2>&1 |
This file contains 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\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'); | |
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 $!'; | |
$pid = exec($command); | |
return $pid; | |
} | |
} |
This file contains 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 App\Console\Commands\EnsureQueueListenerIsRunning; | |
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 = [ | |
EnsureQueueListenerIsRunning::class | |
]; | |
/** | |
* Define the application's command schedule. | |
* | |
* @param \Illuminate\Console\Scheduling\Schedule $schedule | |
* | |
* @return void | |
*/ | |
protected function schedule(Schedule $schedule) | |
{ | |
$schedule->command('queue:checkup')->everyFiveMinutes(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment