以下改动此前只在生产服务器上直接修改,未进仓库,现按线上文件原样收回: - 处方 AI:新增 prescription-ai:dispatch 每分钟派发单轮队列(PrescriptionAiDispatch、 PrescriptionAiLaneGuard 队列锁),work 命令支持 --scheduled;重试接口返回与轮询一致的 任务状态,额度用尽时提示最早可重试时间;每日模型任务默认额度 200 → 5000 - 定时任务 SQL:database/migrations/2026_12_02_prescription_ai_crontab.sql(线上已执行) - project.php:prescription_library_manage_all_roles / prescription_audit_roles 加入角色 13 Co-Authored-By: Claude Opus 5 <[EMAIL]>
72 lines
3.0 KiB
PHP
72 lines
3.0 KiB
PHP
<?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;
|
|
}
|
|
}
|