Files
zyt/server/app/command/FollowupAudioWork.php
T

60 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace app\command;
use app\common\service\followupaudio\FollowupAudioStore;
use app\common\service\followupaudio\FollowupAudioGate;
use app\common\service\followupaudio\FollowupAudioWorker;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\facade\Db;
final class FollowupAudioWork extends Command
{
protected function configure()
{
$this->setName('followup-audio:work')->setDescription('独立随访音频消费者;默认关闭,未知结果不重发')
->addOption('once', null, Option::VALUE_NONE, '只处理一轮');
}
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; });
}
$database = (array) config('database');
$connection = (string) ($database['default'] ?? 'mysql');
if (isset($database['connections'][$connection])) {
$database['connections'][$connection]['break_reconnect'] = true;
Config::set($database, 'database');
}
$worker = new FollowupAudioWorker();
do {
$worked = false;
try {
$worked = $worker->runOnce();
if ($input->getOption('once') || $worked) {
$output->writeln('FOLLOWUP_AUDIO ' . json_encode(['enabled' => FollowupAudioStore::enabled(),
'audio_verified' => FollowupAudioStore::verified(),
'preview_only' => FollowupAudioGate::previewOnly(),
'preview_ready' => FollowupAudioGate::previewOnly() && FollowupAudioStore::ready(), 'processed' => $worked]));
}
} catch (\Throwable $e) {
$output->writeln('FOLLOWUP_AUDIO storage_or_configuration_error');
try { Db::connect()->close(); } catch (\Throwable $ignored) {}
if ($input->getOption('once')) { return 1; }
}
if (!$input->getOption('once') && $running) { usleep($worked ? 100000 : 1000000); }
} while (!$input->getOption('once') && $running);
return 0;
}
}