initialize(); use think\facade\Db; $deadline = time() + (int) ($argv[1] ?? 900); while (true) { $now = time(); $active = Db::name('prescription_ai_task')->where('status', 'running')->where('lock_until', '>', $now) ->field('id,batch_id,model_key,attempts,started_at,progress_json')->select()->toArray(); if ($active === [] || $now >= $deadline) { echo json_encode([ 'idle' => $active === [], 'checked_at' => date('c', $now), 'active' => $active, 'tasks' => Db::name('prescription_ai_task')->field('id,batch_id,model_key,status,attempts,error_code,started_at,finished_at')->order('id')->select()->toArray(), 'results' => Db::name('prescription_ai_result')->field('id,batch_id,model_key,score,herb_score,comparison_status,comparison_reason_code,prompt_version')->select()->toArray(), ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n"; exit($active === [] ? 0 : 1); } sleep(20); }