同步线上直接修改的代码:处方 AI 队列派发与重试额度、角色 13 处方权限
以下改动此前只在生产服务器上直接修改,未进仓库,现按线上文件原样收回: - 处方 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]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
580444731b
commit
5ea3417e4c
@@ -169,23 +169,30 @@ final class PrescriptionAiLogic
|
||||
if (!$task) {
|
||||
throw new DomainException('资料尚未准备完成,请查看分析状态');
|
||||
}
|
||||
if ($task['status'] === 'success' || in_array($task['status'], Policy::ACTIVE_TASKS, true)) {
|
||||
return ['batch_id' => $batchId, 'status' => $task['status']];
|
||||
}
|
||||
if ($task['status'] !== 'failed') {
|
||||
$now = time();
|
||||
$newRetry = $task['status'] === 'failed';
|
||||
if ($newRetry) {
|
||||
if ((int) $task['manual_retries'] >= (int) config('prescription_analysis.max_manual_retries', 2)) {
|
||||
throw new DomainException('该模型已达手动重试上限,请检查失败原因');
|
||||
}
|
||||
$changes = [
|
||||
'status' => 'retry_wait', 'attempts' => 0, 'next_run_at' => $now,
|
||||
'manual_retries' => (int) $task['manual_retries'] + 1,
|
||||
'lock_token' => '', 'lock_until' => 0, 'error_code' => '', 'updated_at' => $now,
|
||||
];
|
||||
$task = array_replace($task, $changes);
|
||||
} elseif ($task['status'] !== 'success' && !in_array($task['status'], Policy::ACTIVE_TASKS, true)) {
|
||||
throw new DomainException('该任务不能重试');
|
||||
}
|
||||
if ((int) $task['manual_retries'] >= (int) config('prescription_analysis.max_manual_retries', 2)) {
|
||||
throw new DomainException('该模型已达手动重试上限,请检查失败原因');
|
||||
$state = self::taskStates([$task], $now)[0];
|
||||
if ($newRetry) {
|
||||
// Keep audit history and the encrypted resume checkpoint across retry rounds.
|
||||
$changes['next_run_at'] = $state['progress']['next_run_at'];
|
||||
$changes['error_code'] = $state['error_code'];
|
||||
Db::name('prescription_ai_task')->where('id', $task['id'])->update($changes);
|
||||
Store::refreshBatch($batchId);
|
||||
}
|
||||
// Attempt history and lifetime counter remain immutable across retry rounds.
|
||||
Db::name('prescription_ai_task')->where('id', $task['id'])->update([
|
||||
'status' => 'retry_wait', 'attempts' => 0, 'next_run_at' => time(), 'lock_token' => '',
|
||||
'manual_retries' => (int) $task['manual_retries'] + 1,
|
||||
'lock_until' => 0, 'error_code' => '', 'updated_at' => time(),
|
||||
]);
|
||||
Store::refreshBatch($batchId);
|
||||
return ['batch_id' => $batchId, 'status' => 'retry_wait'];
|
||||
return ['batch_id' => $batchId, 'model_key' => $model] + $state;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -280,12 +287,54 @@ final class PrescriptionAiLogic
|
||||
'metric_label' => '药味与剂量一致度(不代表临床准确率)'];
|
||||
}
|
||||
|
||||
/** Project the retry response and polled model states from the same task, ledger and attempt history. */
|
||||
private static function taskStates(array $tasks, int $now): array
|
||||
{
|
||||
$budgetModels = [];
|
||||
$attemptTaskIds = [];
|
||||
foreach ($tasks as $task) {
|
||||
if ($task['status'] !== 'retry_wait') { continue; }
|
||||
if ($task['error_code'] !== 'BUDGET_PAUSED' || (int) $task['next_run_at'] <= $now) {
|
||||
$budgetModels[] = $task['model_key'];
|
||||
}
|
||||
if ((int) $task['total_attempts'] > 0) {
|
||||
$attemptTaskIds[] = $task['id'];
|
||||
}
|
||||
}
|
||||
$exhausted = Store::exhaustedModels($budgetModels, $now);
|
||||
$lastErrors = $attemptTaskIds ? Db::name('prescription_ai_attempt')->alias('a')
|
||||
->join('prescription_ai_task t', 't.id = a.task_id AND t.total_attempts = a.attempt_no')
|
||||
->whereIn('a.task_id', $attemptTaskIds)->whereIn('a.status', ['failed', 'expired'])
|
||||
->column('a.error_code', 'task_id') : [];
|
||||
$maxRetries = (int) config('prescription_analysis.max_manual_retries', 2);
|
||||
$states = [];
|
||||
foreach ($tasks as $task) {
|
||||
$code = $task['error_code'];
|
||||
$lastError = $task['status'] === 'failed' ? $code : ($lastErrors[$task['id']] ?? '');
|
||||
if ($task['status'] === 'retry_wait') {
|
||||
if (in_array($task['model_key'], $exhausted, true)) {
|
||||
$code = 'BUDGET_PAUSED';
|
||||
$task['next_run_at'] = max((int) $task['next_run_at'], strtotime('tomorrow', $now));
|
||||
} elseif ($code === 'BUDGET_PAUSED' && (int) $task['next_run_at'] <= $now) {
|
||||
$code = ''; // Yesterday's pause has expired; eligibility still awaits a real claim.
|
||||
}
|
||||
}
|
||||
$task['error_code'] = $code;
|
||||
$states[] = ['status' => $task['status'], 'error_code' => $code,
|
||||
'error_message' => $code !== '' ? Policy::errorMessage($code) : '',
|
||||
'last_error_code' => $lastError, 'last_error_message' => $lastError !== '' ? Policy::errorMessage($lastError) : '',
|
||||
'manual_retries' => (int) $task['manual_retries'], 'max_manual_retries' => $maxRetries,
|
||||
'progress' => Progress::task($task, $now)];
|
||||
}
|
||||
return $states;
|
||||
}
|
||||
|
||||
private static function models(array $batchIds, bool $full, bool $agreementVisible = true): array
|
||||
{
|
||||
if ($batchIds === []) {
|
||||
return [];
|
||||
}
|
||||
$taskFields = ['batch_id', 'model_key', 'status', 'error_code', 'attempts', 'total_attempts', 'result_id',
|
||||
$taskFields = ['id', 'batch_id', 'model_key', 'status', 'error_code', 'attempts', 'total_attempts', 'manual_retries', 'result_id',
|
||||
'started_at', 'finished_at', 'updated_at', 'next_run_at'];
|
||||
if (Store::supportsProgress()) {
|
||||
$taskFields[] = 'progress_json';
|
||||
@@ -298,14 +347,11 @@ final class PrescriptionAiLogic
|
||||
}
|
||||
$results = Db::name('prescription_ai_result')->whereIn('batch_id', $batchIds)->field($fields)->select()->toArray();
|
||||
$map = [];
|
||||
foreach ($tasks as $task) {
|
||||
$map[(int) $task['batch_id']][$task['model_key']] = [
|
||||
'status' => $task['status'], 'score' => null, 'herb_score' => null,
|
||||
'error_code' => $task['error_code'], 'error_message' => $task['error_code'] !== '' ? Policy::errorMessage($task['error_code']) : '',
|
||||
'reason' => $task['error_code'] !== '' ? Policy::errorMessage($task['error_code']) : '',
|
||||
'report_id' => (int) $task['result_id'],
|
||||
'progress' => Progress::task($task),
|
||||
];
|
||||
$taskStates = self::taskStates($tasks, time());
|
||||
foreach ($tasks as $index => $task) {
|
||||
$state = $taskStates[$index];
|
||||
$map[(int) $task['batch_id']][$task['model_key']] = $state + ['score' => null, 'herb_score' => null,
|
||||
'reason' => $state['error_message'], 'report_id' => (int) $task['result_id']];
|
||||
}
|
||||
foreach ($results as $result) {
|
||||
$body = $full ? (new Cipher())->decrypt($result['body_cipher'], 'result:' . $result['batch_id'] . ':' . $result['model_key']) : [];
|
||||
|
||||
Reference in New Issue
Block a user