Skip to content

Commit 40943a7

Browse files
committed
refactor
1 parent 787862e commit 40943a7

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

config/nutgram.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,8 @@
2323
// Set log channel
2424
'log_channel' => env('TELEGRAM_LOG_CHANNEL', 'null'),
2525

26-
// Watch configs used by the "nutgram:run --watch" command
27-
'watch' => [
28-
// PHP binary to use
29-
'bin' => PHP_BINARY,
30-
31-
// Interval in microseconds to check for changes
32-
'interval' => 250 * 1000,
33-
34-
// Paths to watch
35-
'paths' => [
36-
app_path('Telegram'),
37-
]
26+
// Watch paths used by the "nutgram:run --watch" command
27+
'watch_paths' => [
28+
app_path('Telegram'),
3829
],
3930
];

src/Console/RunCommand.php

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,94 @@
33
namespace Nutgram\Laravel\Console;
44

55
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;
88
use Psr\Container\ContainerExceptionInterface;
99
use Psr\Container\NotFoundExceptionInterface;
1010
use SergiX44\Nutgram\Nutgram;
1111
use Spatie\Watcher\Watch;
1212

1313
class RunCommand extends Command
1414
{
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}';
1617

1718
protected $description = 'Start the bot in long polling mode';
1819

19-
protected ?InvokedProcess $watchProcess = null;
20+
protected ?Process $runProcess = null;
2021

2122
/**
2223
* @throws ContainerExceptionInterface
2324
* @throws NotFoundExceptionInterface
2425
*/
25-
public function handle(Nutgram $bot): void
26+
public function handle(Nutgram $bot): int
2627
{
27-
if ($this->option('watch')) {
28-
$this->watch();
28+
if (!$this->option('watch')) {
29+
$bot->run();
2930

30-
return;
31+
return Command::SUCCESS;
3132
}
3233

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;
3443
}
3544

36-
protected function watch(): void
45+
protected function startAsyncRun(): bool
3746
{
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+
}
4770
})
4871
->start();
72+
73+
return $this;
74+
}
75+
76+
protected function isPhpFile(string $path): bool
77+
{
78+
return str_ends_with(strtolower($path), '.php');
4979
}
5080

51-
protected function startWatchProcess(): void
81+
protected function restartAsyncRun(): self
5282
{
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')) {
5688
$this->output->write($output);
5789
}
58-
);
90+
});
91+
92+
$this->startAsyncRun();
93+
94+
return $this;
5995
}
6096
}

0 commit comments

Comments
 (0)