Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iPublicis/7c1d7cf3f82279b17c7f31387b74112d to your computer and use it in GitHub Desktop.
Save iPublicis/7c1d7cf3f82279b17c7f31387b74112d to your computer and use it in GitHub Desktop.
Ensure that the Laravel 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
#!/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 -d register_argc_argv=On /path/to/artisan schedule:run >/dev/null 2>&1 // by @giorgioprovenzale
php-cli /path/to/artisan schedule:run >/dev/null 2>&1
<?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 -d register_argc_argv=On ' . base_path() . '/artisan queue:work --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // another solution by @giorgioprovenzale - see comments
$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;
}
}
<?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();
}
}
@iPublicis
Copy link
Author

iPublicis commented Dec 28, 2018

I found a solution to use php without errors:
instead to use php-cli use php -d register_argc_argv=On

  • giorgioprovenzale, on 14 Jul, added as a commented option in the gists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment