feat: 添加工作空间存活管理功能,重置和清理超时任务,使用 dayjs 格式化时间
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { createSkill, tool } from '@kevisual/router';
|
||||
import { app, cnb } from '../../app.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
import { createKeepAlive } from '../../../src/keep.ts';
|
||||
|
||||
type AliveInfo = {
|
||||
@@ -92,12 +92,19 @@ app.route({
|
||||
tags: [],
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const list = Array.from(keepAliveMap.entries()).map(([id, info]) => ({
|
||||
id,
|
||||
wsUrl: (info as any).KeepAlive?.wsUrl,
|
||||
startTime: info.startTime,
|
||||
updatedTime: info.updatedTime
|
||||
}));
|
||||
const list = Array.from(keepAliveMap.entries()).map(([id, info]) => {
|
||||
const now = Date.now();
|
||||
const duration = Math.floor((now - info.startTime) / 60000); // 分钟
|
||||
return {
|
||||
id,
|
||||
wsUrl: (info as any).KeepAlive?.wsUrl,
|
||||
startTime: info.startTime,
|
||||
startTimeStr: dayjs(info.startTime).format('YYYY-MM-DD HH:mm'),
|
||||
updatedTime: info.updatedTime,
|
||||
updatedTimeStr: dayjs(info.updatedTime).format('YYYY-MM-DD HH:mm'),
|
||||
duration,
|
||||
}
|
||||
});
|
||||
ctx.body = { list };
|
||||
}).addTo(app);
|
||||
|
||||
@@ -153,3 +160,55 @@ app.route({
|
||||
ctx.body = { content: `没有找到对应的工作空间保持存活任务` };
|
||||
}
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'reset-keep-workspace-alive',
|
||||
description: '对存活的工作空间,startTime进行重置',
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
tags: [],
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const now = Date.now();
|
||||
for (const info of keepAliveMap.values()) {
|
||||
info.startTime = now;
|
||||
}
|
||||
ctx.body = { content: `已重置所有存活工作空间的开始时间` };
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'clear-keep-workspace-alive',
|
||||
description: '对存活的工作空间,超过5小时的进行清理',
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
tags: [],
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const res = clearKeepAlive();
|
||||
ctx.body = {
|
||||
content: `已清理所有存活工作空间中超过5小时的任务` + (res.length ? `,清理项:${res.map(i => i.wsUrl).join(', ')}` : ''),
|
||||
list: res
|
||||
};
|
||||
}).addTo(app);
|
||||
|
||||
const clearKeepAlive = () => {
|
||||
const now = Date.now();
|
||||
let clearedArr: { id: string; wsUrl: string }[] = [];
|
||||
for (const [id, info] of keepAliveMap.entries()) {
|
||||
if (now - info.startTime > FIVE_HOURS) {
|
||||
console.log(`工作空间 ${(info as any).KeepAlive?.wsUrl} 超过5小时,自动停止`);
|
||||
info.KeepAlive?.disconnect?.();
|
||||
keepAliveMap.delete(id);
|
||||
clearedArr.push({ id, wsUrl: (info as any).KeepAlive?.wsUrl });
|
||||
}
|
||||
}
|
||||
return clearedArr;
|
||||
}
|
||||
|
||||
// 每5小时自动清理超时的keepAlive任务
|
||||
const FIVE_HOURS = 5 * 60 * 60 * 1000;
|
||||
setInterval(() => {
|
||||
clearKeepAlive();
|
||||
}, FIVE_HOURS);
|
||||
Reference in New Issue
Block a user