|
3 | 3 | namespace Nutgram\Laravel\Console; |
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command; |
6 | | -use Illuminate\Process\InvokedProcess; |
7 | | -use Illuminate\Support\Facades\Process; |
| 6 | +use Symfony\Component\Process\PhpSubprocess; |
| 7 | +use Symfony\Component\Process\Process; |
8 | 8 | use Psr\Container\ContainerExceptionInterface; |
9 | 9 | use Psr\Container\NotFoundExceptionInterface; |
10 | 10 | use SergiX44\Nutgram\Nutgram; |
11 | 11 | use Spatie\Watcher\Watch; |
12 | 12 |
|
13 | 13 | class RunCommand extends Command |
14 | 14 | { |
15 | | - protected $signature = 'nutgram:run {--watch : Watch for changes and restart the bot}'; |
| 15 | + protected $signature = 'nutgram:run {--watch : Watch for changes and restart the bot} |
| 16 | + {--without-tty : Disable output to TTY}'; |
16 | 17 |
|
17 | 18 | protected $description = 'Start the bot in long polling mode'; |
18 | 19 |
|
19 | | - protected ?InvokedProcess $watchProcess = null; |
| 20 | + protected ?Process $runProcess = null; |
20 | 21 |
|
21 | 22 | /** |
22 | 23 | * @throws ContainerExceptionInterface |
23 | 24 | * @throws NotFoundExceptionInterface |
24 | 25 | */ |
25 | | - public function handle(Nutgram $bot): void |
| 26 | + public function handle(Nutgram $bot): int |
26 | 27 | { |
27 | | - if ($this->option('watch')) { |
28 | | - $this->watch(); |
| 28 | + if (!$this->option('watch')) { |
| 29 | + $bot->run(); |
29 | 30 |
|
30 | | - return; |
| 31 | + return Command::SUCCESS; |
31 | 32 | } |
32 | 33 |
|
33 | | - $bot->run(); |
| 34 | + $this->info('Watching for changes...'); |
| 35 | + |
| 36 | + if(!$this->startAsyncRun()){ |
| 37 | + return Command::FAILURE; |
| 38 | + } |
| 39 | + |
| 40 | + $this->listenForChanges(); |
| 41 | + |
| 42 | + return Command::SUCCESS; |
34 | 43 | } |
35 | 44 |
|
36 | | - protected function watch(): void |
| 45 | + protected function startAsyncRun(): bool |
37 | 46 | { |
38 | | - $this->info('Watching for changes...'); |
39 | | - $this->startWatchProcess(); |
40 | | - |
41 | | - Watch::paths(...config('nutgram.watch.paths', [])) |
42 | | - ->setIntervalTime(config('nutgram.watch.interval', 250 * 1000)) |
43 | | - ->onAnyChange(function () { |
44 | | - $this->warn('Restarting the bot...'); |
45 | | - $this->watchProcess?->stop(); |
46 | | - $this->startWatchProcess(); |
| 47 | + $this->runProcess = (new PhpSubprocess(['artisan', 'nutgram:run', '--ansi'])) |
| 48 | + ->setTty(Process::isTtySupported() && !$this->option('without-tty')) |
| 49 | + ->setTimeout(null); |
| 50 | + |
| 51 | + $this->runProcess->start(function (string $type, string $output) { |
| 52 | + if(Process::isTtySupported() && !$this->option('without-tty')) { |
| 53 | + $this->output->write($output); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + sleep(1); |
| 58 | + |
| 59 | + return ! $this->runProcess->isTerminated(); |
| 60 | + } |
| 61 | + |
| 62 | + protected function listenForChanges(): self |
| 63 | + { |
| 64 | + Watch::paths(...config('nutgram.watch_paths', [])) |
| 65 | + ->setIntervalTime(200 * 1000) |
| 66 | + ->onAnyChange(function (string $event, string $path) { |
| 67 | + if ($this->isPhpFile($path)) { |
| 68 | + $this->restartAsyncRun(); |
| 69 | + } |
47 | 70 | }) |
48 | 71 | ->start(); |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + protected function isPhpFile(string $path): bool |
| 77 | + { |
| 78 | + return str_ends_with(strtolower($path), '.php'); |
49 | 79 | } |
50 | 80 |
|
51 | | - protected function startWatchProcess(): void |
| 81 | + protected function restartAsyncRun(): self |
52 | 82 | { |
53 | | - $this->watchProcess = Process::tty(Process::supportsTty())->start( |
54 | | - command: config('nutgram.watch.bin', PHP_BINARY).' artisan nutgram:run', |
55 | | - output: function (string $type, string $output) { |
| 83 | + $this->components->info('Change detected! Restarting bot...'); |
| 84 | + |
| 85 | + $this->runProcess->stop(); |
| 86 | + $this->runProcess->wait(function (string $type, string $output) { |
| 87 | + if(Process::isTtySupported() && !$this->option('without-tty')) { |
56 | 88 | $this->output->write($output); |
57 | 89 | } |
58 | | - ); |
| 90 | + }); |
| 91 | + |
| 92 | + $this->startAsyncRun(); |
| 93 | + |
| 94 | + return $this; |
59 | 95 | } |
60 | 96 | } |
0 commit comments