47 lines
2.2 KiB
PHP
47 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
// Queues a manual retry for failed model tasks, exactly like the report window's retry button:
|
|
// same permission checks, same manual-retry allowance, original frozen snapshot. Prints task
|
|
// metadata only. Pass --apply to actually queue; without it only previews.
|
|
$serverRoot = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'server' . DIRECTORY_SEPARATOR;
|
|
chdir($serverRoot);
|
|
require $serverRoot . 'vendor/autoload.php';
|
|
$app = new \think\App($serverRoot);
|
|
$app->initialize();
|
|
|
|
use app\adminapi\logic\tcm\PrescriptionAiLogic;
|
|
use think\facade\Db;
|
|
|
|
$apply = in_array('--apply', $argv, true);
|
|
$batchIds = array_values(array_filter(array_map('intval', array_slice($argv, 1)), static fn (int $id): bool => $id > 0));
|
|
|
|
$rows = Db::name('prescription_ai_task')->where('status', 'failed')
|
|
->field('id,batch_id,model_key,attempts,manual_retries,error_code')->order('id')->select()->toArray();
|
|
$out = ['apply' => $apply, 'candidates' => [], 'results' => []];
|
|
foreach ($rows as $row) {
|
|
if ($batchIds !== [] && !in_array((int) $row['batch_id'], $batchIds, true)) {
|
|
continue;
|
|
}
|
|
$batch = Db::name('prescription_ai_batch')->where('id', $row['batch_id'])
|
|
->field('id,prescription_id,actor_id,validity,status')->find();
|
|
$entry = [
|
|
'task_id' => (int) $row['id'], 'batch_id' => (int) $row['batch_id'], 'prescription_id' => (int) ($batch['prescription_id'] ?? 0),
|
|
'model_key' => $row['model_key'], 'error_code' => $row['error_code'], 'manual_retries' => (int) $row['manual_retries'],
|
|
'validity' => $batch['validity'] ?? '',
|
|
];
|
|
$out['candidates'][] = $entry;
|
|
if (!$apply || ($batch['validity'] ?? '') !== 'current') {
|
|
continue;
|
|
}
|
|
$actor = (int) ($batch['actor_id'] ?? 0);
|
|
$info = \app\common\service\prescriptionai\PrescriptionAiAccess::actor($actor);
|
|
try {
|
|
$out['results'][] = $entry + ['queued' => PrescriptionAiLogic::retry(
|
|
(int) $row['batch_id'], (string) $row['model_key'], $actor, (array) $info)];
|
|
} catch (\Throwable $e) {
|
|
$out['results'][] = $entry + ['refused' => $e->getMessage()];
|
|
}
|
|
}
|
|
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
|