Вот что я писал для достижения этой цели:
приложение/Console/Команды/QueueProcessListener.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class QueueProcessListener extends Command
{
/**
* The name of the command/process we want to monitor. This string will be used both to check to see if the process
* is currently running and to spawn it (The arguments are appended to it).
*
* @var string
*/
protected $command = 'php artisan queue:listen';
/**
* The arguments to pass to the process when spawning it.
*
* @var string
*/
protected $arguments = '--tries=3';
/**
* The signature of the console command. We use the signature when running it through Artisan: php artisan $signature
*
* @var string
*/
protected $signature = 'queue-process-listener';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Monitor the queue listener process to ensure it is always running.';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (!$this->isProcessRunning($this->command)) {
$this->info("Starting queue listener.");
$this->executeShellCommand($this->command, $this->arguments, true);
} else {
$this->info("Queue listener is running.");
}
}
/**
* Execute a shell command, with the provided arguments, and optionally in the background. Commands that are not run
* in the background will return their output/response.
*
* @param $command
* @param string $arguments
* @param bool $background
* @return string
*/
public function executeShellCommand($command, $arguments = '', $background = false)
{
$command = trim($command);
if (!is_string($command) || empty($command)) {
return null;
}
$arguments = trim($arguments);
$cmd = trim($command . ' ' . $arguments) . ($background ? ' > /dev/null 2>/dev/null &' : '');
return shell_exec($cmd);
}
/**
* Check if a process is running using pgrep.
*
* @param $process
* @return bool
*/
public function isProcessRunning($process)
{
$output = $this->executeShellCommand('pgrep -f "' . $process . '"');
return !empty(trim($output));
}
}
приложение/Console/Kernel.php
<?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 = [
Commands\QueueProcessListener::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// Schedule my process listener to run every 5 minutes.
$schedule->command('queue-process-listener')->everyFiveMinutes();
}
}
Я достиг этого, написав задачу типа слушателя процесса, которую я планировал запускать каждую минуту. Что это значит, проверьте, работает ли очередь с помощью 'pgrep', и запускает ее, если обнаруживает, что она не запущена. – Jonathon
@Jonathon Задача как в cronjob? У меня также была эта идея в моей голове, добавляя «queue: listen» в мой график laravel для запуска каждый день, но добавляя 'withoutOverlapping()'. Он хотел бы посмотреть: '$ schedule-> command ('queue: listen') -> daily() -> withoutOverlapping();'. –
Да, я создал консольную команду 'App \ Console \ Commands \ QueueProcessListener'. Затем я добавил его в «Kernel.php», чтобы сделать его доступным для Artisan, и в конце концов добавил его в метод расписания Kernel, чтобы он запускался каждые 5 минут: '$ schedule-> command ('queue-process- listener ') -> everyFiveMinutes(); ' – Jonathon