This commit is contained in:
Your Name
2026-12-03 09:54:46 +08:00
parent 4e0c2daf5c
commit 25070a4889
55 changed files with 8040 additions and 536 deletions
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace app\command;
use app\common\service\prescriptionai\PrescriptionAiLaneGuard;
use app\common\service\prescriptionai\PrescriptionAiStore;
use RuntimeException;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
final class PrescriptionAiDispatch extends Command
{
protected function configure(): void
{
$this->setName('prescription-ai:dispatch')->setDescription('巡检处方 AI 消费者,对缺失的队列派发一轮处理')
->addOption('check', null, Option::VALUE_NONE, '只输出巡检状态');
}
protected function execute(Input $input, Output $output): int
{
$enabled = PrescriptionAiStore::enabled();
$checkOnly = (bool) $input->getOption('check');
if (!$enabled && !$checkOnly) {
$output->writeln('PRESCRIPTION_AI_DISPATCH {"enabled":false}');
return 0;
}
$root = $this->app->getRootPath();
$think = $root . 'think';
if (PHP_SAPI !== 'cli' || !is_readable($think)) {
throw new RuntimeException('处方 AI 巡检需要 PHP CLI 和可读的 think 入口');
}
$present = PHP_OS_FAMILY === 'Linux' ? PrescriptionAiLaneGuard::linuxWorkers($think) : [];
$null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
$streams = [['file', $null, 'r'], ['file', $null, 'w'], ['file', $null, 'w']];
$states = [];
foreach (PrescriptionAiLaneGuard::LANES as $lane) {
if (isset($present[$lane])) {
$states[$lane] = $present[$lane];
continue;
}
if ($checkOnly) {
$states[$lane] = PrescriptionAiLaneGuard::busy($this->app->getRuntimePath(), $lane) ? 'busy' : 'missing';
continue;
}
// Surface runtime permission errors in the admin schedule before detaching the worker.
$probe = PrescriptionAiLaneGuard::acquire($this->app->getRuntimePath(), $lane, LOCK_EX | LOCK_NB);
if ($probe === null) {
$states[$lane] = 'busy';
continue;
}
fclose($probe);
if (!function_exists('proc_open')) {
throw new RuntimeException('处方 AI 单轮接管需要启用 proc_open');
}
$process = proc_open([PHP_BINARY, $think, 'prescription-ai:work', '--lane=' . $lane, '--once', '--scheduled'],
$streams, $pipes, $root);
if (!is_resource($process)) {
throw new RuntimeException('无法启动处方 AI 消费者 ' . $lane);
}
// proc_close waits for the model. Releasing a pipe-free handle lets the cron runner continue immediately.
unset($process);
$states[$lane] = 'dispatched';
}
$output->writeln('PRESCRIPTION_AI_DISPATCH ' . json_encode(['enabled' => $enabled, 'lanes' => $states]));
return 0;
}
}
+28 -2
View File
@@ -4,11 +4,13 @@ declare(strict_types=1);
namespace app\command;
use app\common\service\prescriptionai\PrescriptionAiLaneGuard;
use app\common\service\prescriptionai\PrescriptionAiStore;
use app\common\service\prescriptionai\PrescriptionAiWorker;
use think\console\Command;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -19,16 +21,26 @@ final class PrescriptionAiWork extends Command
{
$this->setName('prescription-ai:work')->setDescription('处方AI独立后台任务;分别运行prepare/qwen/openai')
->addOption('lane', null, Option::VALUE_REQUIRED, 'prepare、qwen、openai', 'prepare')
->addOption('once', null, Option::VALUE_NONE, '只处理一轮');
->addOption('once', null, Option::VALUE_NONE, '只处理一轮')
->addOption('scheduled', null, Option::VALUE_NONE, '定时任务单轮接管,当前队列已占用时跳过');
}
protected function execute(Input $input, Output $output): int
{
$lane = (string) $input->getOption('lane');
if (!in_array($lane, ['prepare', 'qwen', 'openai'], true)) {
if (!in_array($lane, PrescriptionAiLaneGuard::LANES, true)) {
$output->writeln('Invalid lane');
return 1;
}
$scheduled = $input->getOption('scheduled');
if ($scheduled && !$input->getOption('once')) {
$output->writeln('--scheduled requires --once');
return 1;
}
// This handle stays open until execute() returns. Process exit also releases it after a crash.
$laneLock = PrescriptionAiLaneGuard::acquire($this->app->getRuntimePath(), $lane,
$scheduled ? LOCK_EX | LOCK_NB : LOCK_SH);
if ($laneLock === null) { return 0; }
$running = true;
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
@@ -48,6 +60,11 @@ final class PrescriptionAiWork extends Command
$sweepAt = 0;
$sourceCursor = 0;
$rxCursor = 0;
if ($scheduled && $lane === 'prepare') {
$saved = json_decode((string) stream_get_contents($laneLock), true);
$sourceCursor = (int) ($saved['source'] ?? 0);
$rxCursor = (int) ($saved['prescription'] ?? 0);
}
do {
$worked = false;
try {
@@ -60,6 +77,12 @@ final class PrescriptionAiWork extends Command
$sourceCursor = $sweep['selected'] > 0 ? $sweep['last_id'] : 0;
$rx = $worker->reconcile($rxCursor);
$rxCursor = $rx['selected'] > 0 ? $rx['last_id'] : 0;
if ($scheduled) {
rewind($laneLock);
ftruncate($laneLock, 0);
fwrite($laneLock, json_encode(['source' => $sourceCursor, 'prescription' => $rxCursor]));
fflush($laneLock);
}
$sweepAt = time() + 60;
}
}
@@ -73,6 +96,9 @@ final class PrescriptionAiWork extends Command
$detail .= ' ' . $sqlState[0];
}
$output->writeln('PRESCRIPTION_AI storage_or_configuration_error ' . $detail);
if ($scheduled) {
Log::write('PRESCRIPTION_AI ' . $lane . ' storage_or_configuration_error ' . $detail, 'error');
}
// Drop a possibly dead connection so the next round reconnects instead of looping.
try {
Db::connect()->close();