69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace LaravelSupportCommand;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
class ScheduleWorkCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'ScheduleWork';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'LaravelSupportCommand - Run artisan schedule:work, and if it exits, kill and restart it.';
|
|
|
|
public function handle()
|
|
{
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
|
|
$this->warn('Killing any existing schedule:work monitor processes using pkill...');
|
|
exec('pkill -f "schedule:work"');
|
|
}
|
|
|
|
$this->info('Starting schedule:work monitor...');
|
|
while (true) {
|
|
$this->warn('Killing any remaining schedule:work processes...');
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
exec('taskkill /IM php.exe /F /FI "WINDOWTITLE eq schedule:work"');
|
|
} else {
|
|
exec('pkill -f "artisan schedule:work"');
|
|
}
|
|
|
|
$this->info('Starting: php artisan schedule:work');
|
|
$process = proc_open(
|
|
'php artisan schedule:work',
|
|
[
|
|
1 => ['pipe', 'w'],
|
|
2 => ['pipe', 'w'],
|
|
],
|
|
$pipes
|
|
);
|
|
if (is_resource($process)) {
|
|
// Read output and error
|
|
while ($line = fgets($pipes[1])) {
|
|
$this->line(trim($line));
|
|
}
|
|
while ($err = fgets($pipes[2])) {
|
|
$this->error(trim($err));
|
|
}
|
|
fclose($pipes[1]);
|
|
fclose($pipes[2]);
|
|
$status = proc_get_status($process);
|
|
$pid = $status['pid'] ?? null;
|
|
proc_terminate($process);
|
|
return $pid;
|
|
} else {
|
|
$this->error('Failed to start schedule:work process. Retrying in 5 seconds...');
|
|
sleep(5);
|
|
}
|
|
}
|
|
}
|
|
}
|