Skip to content

Commit f0e775f

Browse files
committed
refactor ListenCommand and RunCommand to support polling timeout and single update mode
1 parent df01409 commit f0e775f

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Console/ListenCommand.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Nutgram\Laravel\Console;
44

55
use Illuminate\Console\Command;
6-
use SergiX44\Nutgram\Nutgram;
7-
use SergiX44\Nutgram\RunningMode\SingleUpdate;
6+
use Illuminate\Support\Facades\Process;
7+
use function Illuminate\Support\artisan_binary;
8+
use function Orchestra\Testbench\php_binary;
89

910
class ListenCommand extends Command
1011
{
@@ -16,15 +17,16 @@ public function handle(): void
1617
{
1718
$this->warn('This running mode is very inefficient and only suitable for development purposes. DO NOT USE IN PRODUCTION!');
1819
$this->info('Listening...');
19-
config()?->set('nutgram.config.polling.timeout', $this->option('pollingTimeout'));
2020
while (true) {
21-
if (function_exists('opcache_reset')) {
22-
opcache_reset();
21+
$result = Process::run([
22+
php_binary(), artisan_binary(), 'nutgram:run', '--once',
23+
'--pollingTimeout='.$this->option('pollingTimeout'),
24+
]);
25+
if ($result->exitCode() !== 0) {
26+
$this->line($result->output());
27+
$this->error($result->errorOutput());
28+
break;
2329
}
24-
app()->forgetInstance(Nutgram::class);
25-
$bot = app(Nutgram::class);
26-
$bot->setRunningMode(SingleUpdate::class);
27-
$bot->run();
2830
}
2931
}
3032
}

src/Console/RunCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use Psr\Container\ContainerExceptionInterface;
77
use Psr\Container\NotFoundExceptionInterface;
88
use SergiX44\Nutgram\Nutgram;
9+
use SergiX44\Nutgram\RunningMode\SingleUpdate;
910

1011
class RunCommand extends Command
1112
{
12-
protected $signature = 'nutgram:run';
13+
protected $signature = 'nutgram:run {--once} {--pollingTimeout=}';
1314

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

@@ -19,6 +20,15 @@ class RunCommand extends Command
1920
*/
2021
public function handle(Nutgram $bot): void
2122
{
23+
if ($pollingTimeout = $this->option('pollingTimeout')) {
24+
config()?->set('nutgram.config.polling.timeout', (int)$pollingTimeout);
25+
$this->info("Polling set to {$pollingTimeout} seconds.");
26+
}
27+
28+
if ($this->option('once')) {
29+
$bot->setRunningMode(SingleUpdate::class);
30+
}
31+
2232
$bot->run();
2333
}
2434
}

0 commit comments

Comments
 (0)