状态灯双重核对: docker只读接口查runner真实进程, 强停不再假显示运行中(新增'已停止'态); 手动停止单列

This commit is contained in:
mzl92
2026-08-27 18:17:59 +08:00
parent e3cdadab88
commit 5e9928652e
3 changed files with 32 additions and 13 deletions
+27 -11
View File
@@ -286,22 +286,38 @@ app.post('/api/users/:id/resetpwd', requireAuth, requireSuper, (req, res) => {
res.json({ ok: true });
});
// ---------- 筛选任务状态(所有登录用户, 读runner状态文件) ----------
app.get('/api/task-status', requireAuth, (req, res) => {
// ---------- 筛选任务状态(所有登录用户) ----------
// 状态文件 + 进程存活双重核对: runner被强停时不写状态文件,仅信文件会假"运行中"
const http = require('http');
function hermesRunnerProcs() {
return new Promise((resolve) => {
const req = http.request(
{ socketPath: '/var/run/docker.sock', path: '/containers/hermes/top?ps_args=-eo pid,args', method: 'GET', timeout: 3000 },
(r) => { let d = ''; r.on('data', (c) => d += c); r.on('end', () => { try { resolve(JSON.parse(d).Processes || []); } catch (_) { resolve([]); } }); }
);
req.on('error', () => resolve([]));
req.end();
});
}
app.get('/api/task-status', requireAuth, async (req, res) => {
const stateDir = process.env.STATE_DIR || '/state';
const bjToday = new Date(Date.now() + 8 * 3600e3).toISOString().slice(0, 10);
const doneSet = new Set(['time_cutoff', 'chat_exhausted', 'done']);
const errSet = new Set(['login_expired', 'alert', 'tunnel_failed', 'platform_blocked', 'qr_expired', 'error', 'stopped_by_user']);
const errSet = new Set(['login_expired', 'alert', 'tunnel_failed', 'platform_blocked', 'qr_expired', 'error']);
const procs = await hermesRunnerProcs();
const alive = (acct) => procs.some((p) => /daily-runner(\.js)?\s/.test(String(p)) && String(p).includes(acct));
const out = {};
for (const acct of ['hr1', 'hr2']) {
try {
const s = JSON.parse(fs.readFileSync(path.join(stateDir, `state-${acct}.json`), 'utf8'));
if (s.date !== bjToday) { out[acct] = { state: 'idle', text: '今日未运行' }; continue; }
if (s.status === 'running') out[acct] = { state: 'running', text: '运行中', chatLeft: s.chatLeft };
else if (doneSet.has(s.status)) out[acct] = { state: 'done', text: '已收工', chatLeft: s.chatLeft };
else if (errSet.has(s.status)) out[acct] = { state: 'error', text: '异常停止', detail: s.status };
else out[acct] = { state: 'done', text: '已收工', chatLeft: s.chatLeft };
} catch (_) { out[acct] = { state: 'idle', text: '今日未运行' }; }
let s = null;
try { s = JSON.parse(fs.readFileSync(path.join(stateDir, `state-${acct}.json`), 'utf8')); } catch (_) {}
const isAlive = alive(acct);
if (isAlive) { out[acct] = { state: 'running', text: '运行中', chatLeft: s && s.chatLeft }; continue; }
if (!s || s.date !== bjToday) { out[acct] = { state: 'idle', text: '今日未运行' }; continue; }
if (s.status === 'running') out[acct] = { state: 'stopped', text: '已停止', detail: '状态未更新(进程已退出)' };
else if (s.status === 'stopped_by_user') out[acct] = { state: 'stopped', text: '手动停止' };
else if (doneSet.has(s.status)) out[acct] = { state: 'done', text: '已收工', chatLeft: s.chatLeft };
else if (errSet.has(s.status)) out[acct] = { state: 'error', text: '异常停止', detail: s.status };
else out[acct] = { state: 'done', text: '已收工', chatLeft: s.chatLeft };
}
res.json(out);
});