Skip to content

Add --watch option to nutgram:run command #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"require": {
"php": "^8.2",
"nunomaduro/termwind": "^1.0|^2.0",
"nutgram/nutgram": "^4.17.0"
"nutgram/nutgram": "^4.17.0",
"spatie/file-system-watcher": "^1.2"
},
"require-dev": {
"illuminate/testing": "^9.0|^10.0|^11.0|^12.0",
Expand Down
5 changes: 5 additions & 0 deletions config/nutgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@

// Set log channel
'log_channel' => env('TELEGRAM_LOG_CHANNEL', 'null'),

// Watch paths used by the "nutgram:run --watch" command
'watch_paths' => [
app_path('Telegram'),
],
];
72 changes: 69 additions & 3 deletions src/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,88 @@
namespace Nutgram\Laravel\Console;

use Illuminate\Console\Command;
use Symfony\Component\Process\PhpSubprocess;
use Symfony\Component\Process\Process;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use SergiX44\Nutgram\Nutgram;
use Spatie\Watcher\Watch;

class RunCommand extends Command
{
protected $signature = 'nutgram:run';
protected $signature = 'nutgram:run {--watch : Watch for changes and restart the bot}
{--without-tty : Disable output to TTY}';

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

protected ?Process $runProcess = null;

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(Nutgram $bot): void
public function handle(Nutgram $bot): int
{
if (!$this->option('watch')) {
$bot->run();

return Command::SUCCESS;
}

$this->info('Watching for changes...');

if(!$this->startAsyncRun()){
return Command::FAILURE;
}

$this->listenForChanges();

return Command::SUCCESS;
}

protected function startAsyncRun(): bool
{
$this->runProcess = (new PhpSubprocess(['artisan', 'nutgram:run', '--ansi']))
->setTty(Process::isTtySupported() && !$this->option('without-tty'))
->setTimeout(null);

$this->runProcess->start(function (string $type, string $output) {
if(Process::isTtySupported() && !$this->option('without-tty')) {
$this->output->write($output);
}
});

return ! $this->runProcess->isTerminated();
}

protected function listenForChanges(): self
{
Watch::paths(...config('nutgram.watch_paths', []))
->setIntervalTime(200 * 1000)
->onAnyChange(function (string $event, string $path) {
if ($this->isPhpFile($path)) {
$this->restartAsyncRun();
}
})
->start();

return $this;
}

protected function isPhpFile(string $path): bool
{
$bot->run();
return str_ends_with(strtolower($path), '.php');
}

protected function restartAsyncRun(): self
{
$this->components->info('Changes detected! Restarting bot...');

$this->runProcess->stop();
$this->runProcess->wait();

$this->startAsyncRun();

return $this;
}
}