Created
September 6, 2024 12:24
-
-
Save lpheller/acea41f9ec5d3473a823b312db720457 to your computer and use it in GitHub Desktop.
Laravel Command to run multiple local dev related commands with one call
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; | |
use Illuminate\Support\Facades\Process; | |
use Symfony\Component\Console\Command\SignalableCommandInterface; | |
class RunDevCommand extends Command implements SignalableCommandInterface | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'run:dev'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
/** | |
* Execute the console command. | |
*/ | |
public function handle() | |
{ | |
$this->info('Running development server...'); | |
$processes = [ | |
'Development server' => Process::start('php artisan serve'), | |
'Scheduler' => Process::start('php artisan schedule:work'), | |
'Queue listener' => Process::start('php artisan queue:listen'), | |
'NPM run dev' => Process::start('npm run dev'), | |
]; | |
while (true) { | |
foreach ($processes as $name => $process) { | |
if (! $process->running()) { | |
$this->error("The $name has stopped unexpectedly."); | |
$this->terminateProcesses($processes); | |
break 2; | |
} | |
} | |
sleep(1); | |
} | |
} | |
protected function terminateProcesses(array $processes) | |
{ | |
foreach ($processes as $process) { | |
if ($process->running()) { | |
$process->signal(SIGINT); | |
} | |
} | |
} | |
public function getSubscribedSignals( | |
): array { | |
return [SIGINT, SIGTERM]; | |
} | |
public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false | |
{ | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment