Files
zyt/server/app/command/AssistantServiceAuto.php
2026-11-12 09:07:07 +08:00

45 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace app\command;
use app\common\service\assistantservice\AssistantServiceAutomation;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
final class AssistantServiceAuto extends Command
{
protected function configure()
{
$this->setName('assistant-service:auto')->setDescription('按真实来源自动发现医助分析任务;独立消费者处理模型调用')
->addOption('once', null, Option::VALUE_NONE, '只检查一轮')
->addOption('sleep', null, Option::VALUE_REQUIRED, '检查轮询间隔秒数', '60');
}
protected function execute(Input $input, Output $output): int
{
$running = true;
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
pcntl_signal(SIGTERM, static function () use (&$running): void { $running = false; });
pcntl_signal(SIGINT, static function () use (&$running): void { $running = false; });
}
$pause = max(1, min(60, (int) $input->getOption('sleep')));
$automation = new AssistantServiceAutomation();
do {
$result = $automation->tick();
$output->writeln('ASSISTANT_SERVICE_AUTO state=' . $result['state'] . ' scanned=' . $result['scanned'] . ' enqueued=' . $result['enqueued']);
if ($result['state'] === 'error') {
try { Db::connect()->close(); } catch (\Throwable $ignored) {}
if ($input->getOption('once')) { return 1; }
}
if (!$input->getOption('once') && $running) { sleep($pause); }
} while (!$input->getOption('once') && $running);
return 0;
}
}