|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Horizon\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use InvalidArgumentException; |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Symfony\Component\Process\ExecutableFinder; |
| 9 | +use Symfony\Component\Process\Process; |
| 10 | + |
| 11 | +#[AsCommand(name: 'horizon:listen')] |
| 12 | +class ListenCommand extends Command |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'horizon:listen |
| 20 | + {--environment= : The environment name} |
| 21 | + {--poll : Use polling for file watching}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $description = 'Run Horizon and automatically restart workers on file changes'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The Horizon process instance. |
| 32 | + * |
| 33 | + * @var \Symfony\Component\Process\Process|null |
| 34 | + */ |
| 35 | + protected $horizonProcess; |
| 36 | + |
| 37 | + /** |
| 38 | + * The file watcher process instance. |
| 39 | + * |
| 40 | + * @var \Symfony\Component\Process\Process|null |
| 41 | + */ |
| 42 | + protected $watcherProcess; |
| 43 | + |
| 44 | + /** |
| 45 | + * Indicates if a termination signal has been received. |
| 46 | + * |
| 47 | + * @var int|null |
| 48 | + */ |
| 49 | + protected $trappedSignal = null; |
| 50 | + |
| 51 | + /** |
| 52 | + * Execute the console command. |
| 53 | + * |
| 54 | + * @return int |
| 55 | + */ |
| 56 | + public function handle() |
| 57 | + { |
| 58 | + $this->components->info('Starting Horizon and watching for file changes...'); |
| 59 | + |
| 60 | + $this->watcherProcess = $this->startWatcher(); |
| 61 | + |
| 62 | + if ($this->watcherProcess->isTerminated()) { |
| 63 | + return $this->watcherFailed(); |
| 64 | + } |
| 65 | + |
| 66 | + if (! $this->startHorizon()) { |
| 67 | + return Command::FAILURE; |
| 68 | + } |
| 69 | + |
| 70 | + $this->listenForChanges(); |
| 71 | + |
| 72 | + return Command::SUCCESS; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Start the file watcher process. |
| 77 | + * |
| 78 | + * @return \Symfony\Component\Process\Process |
| 79 | + */ |
| 80 | + protected function startWatcher() |
| 81 | + { |
| 82 | + if (empty($paths = config('horizon.watch'))) { |
| 83 | + throw new InvalidArgumentException( |
| 84 | + 'List of directories / files to watch not found. Please update your "config/horizon.php" configuration file.', |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + $nodeExecutable = (new ExecutableFinder)->find('node'); |
| 89 | + |
| 90 | + if (! $nodeExecutable) { |
| 91 | + throw new InvalidArgumentException( |
| 92 | + 'Node could not be found. Please ensure Node is installed and available in your system PATH.', |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + $process = new Process([ |
| 97 | + $nodeExecutable, |
| 98 | + 'file-watcher.cjs', |
| 99 | + json_encode(collect($paths)->map(fn ($path) => base_path($path))->values()->all()), |
| 100 | + $this->option('poll') ? '1' : '', |
| 101 | + ], __DIR__.'/../../bin', ['NODE_PATH' => base_path('node_modules')], null, null); |
| 102 | + |
| 103 | + $process->start(); |
| 104 | + |
| 105 | + sleep(1); |
| 106 | + |
| 107 | + return $process; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Start the Horizon process. |
| 112 | + * |
| 113 | + * @return bool |
| 114 | + */ |
| 115 | + protected function startHorizon() |
| 116 | + { |
| 117 | + $command = 'php artisan horizon'; |
| 118 | + |
| 119 | + if ($environment = $this->option('environment')) { |
| 120 | + $command .= ' --environment='.$environment; |
| 121 | + } |
| 122 | + |
| 123 | + $this->horizonProcess = Process::fromShellCommandline($command) |
| 124 | + ->setTimeout(null); |
| 125 | + |
| 126 | + $this->trap([SIGINT, SIGTERM, SIGQUIT], function ($signal) { |
| 127 | + $this->trappedSignal = $signal; |
| 128 | + |
| 129 | + $this->horizonProcess->stop(signal: $signal); |
| 130 | + $this->horizonProcess->wait(); |
| 131 | + |
| 132 | + if ($this->watcherProcess) { |
| 133 | + $this->watcherProcess->stop(); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + $this->horizonProcess->start(); |
| 138 | + |
| 139 | + usleep(100000); |
| 140 | + |
| 141 | + return ! $this->horizonProcess->isTerminated(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Listen for file changes and restart Horizon when detected. |
| 146 | + * |
| 147 | + * @return void |
| 148 | + */ |
| 149 | + protected function listenForChanges() |
| 150 | + { |
| 151 | + while (! $this->trappedSignal) { |
| 152 | + if ($this->watcherProcess->getIncrementalOutput()) { |
| 153 | + $this->restartHorizon(); |
| 154 | + } |
| 155 | + |
| 156 | + $this->output->write($this->horizonProcess->getIncrementalOutput()); |
| 157 | + |
| 158 | + if (! $this->horizonProcess->isRunning()) { |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + usleep(500000); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Restart the Horizon process. |
| 168 | + * |
| 169 | + * @return void |
| 170 | + */ |
| 171 | + protected function restartHorizon() |
| 172 | + { |
| 173 | + $this->components->info('File changed. Restarting Horizon...'); |
| 174 | + |
| 175 | + $this->horizonProcess->stop(); |
| 176 | + $this->horizonProcess->wait(); |
| 177 | + |
| 178 | + $this->startHorizon(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Handle watcher process failure. |
| 183 | + * |
| 184 | + * @return int |
| 185 | + */ |
| 186 | + protected function watcherFailed() |
| 187 | + { |
| 188 | + $this->components->error( |
| 189 | + 'Unable to start file watcher. Please ensure Node.js and the chokidar npm package are installed.', |
| 190 | + ); |
| 191 | + |
| 192 | + $this->output->writeln($this->watcherProcess->getErrorOutput()); |
| 193 | + |
| 194 | + return Command::FAILURE; |
| 195 | + } |
| 196 | +} |
0 commit comments