Files
mcn-short-video/project/短视频脚本创作/V1.0/mcn-work-shop/poll-create-tasks.cjs
T

53 lines
1.9 KiB
JavaScript
Raw Normal View History

/* poll-create-tasks.cjs — 轮询 6 个创作任务状态,全部完成/失败后退出 */
const TASKS = [
{ videoId: 309, id: 'automation-1788258518460' },
{ videoId: 313, id: 'automation-1788258521485' },
{ videoId: 315, id: 'automation-1788258524508' },
{ videoId: 316, id: 'automation-1788258527521' },
{ videoId: 318, id: 'automation-1788258530542' },
{ videoId: 320, id: 'automation-1788258533562' },
];
const { loadConfig } = require('./load-config');
const CFG = loadConfig();
const API = `http://localhost:${CFG.port}/api/run/status?id=`;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const stateOf = new Map(TASKS.map((t) => [t.videoId, 'queued']));
let lastPrint = '';
async function check() {
const snap = [];
for (const t of TASKS) {
try {
const r = await fetch(API + encodeURIComponent(t.id));
const d = await r.json();
stateOf.set(t.videoId, d.state || 'unknown');
snap.push(`v${t.videoId}:${d.state || '?'}`);
} catch (e) {
snap.push(`v${t.videoId}:ERR`);
}
}
const line = snap.join(' ');
if (line !== lastPrint) {
console.log(new Date().toLocaleTimeString('zh-CN', { hour12: false }) + ' | ' + line);
lastPrint = line;
}
const vals = [...stateOf.values()];
const done = vals.filter((s) => ['done', 'error', 'notfound'].includes(s)).length;
return done;
}
(async () => {
console.log('START polling 6 create tasks: ' + new Date().toLocaleTimeString('zh-CN', { hour12: false }));
const t0 = Date.now();
while (Date.now() - t0 < 3 * 60 * 60 * 1000) { // 最长 3 小时
const done = await check();
if (done >= TASKS.length) {
console.log('\nALL DONE: ' + [...stateOf.entries()].map(([v, s]) => `v${v}=${s}`).join(' '));
process.exit(0);
}
await sleep(30000);
}
console.log('\nTIMEOUT 3h, current: ' + [...stateOf.entries()].map(([v, s]) => `v${v}=${s}`).join(' '));
process.exit(1);
})();