tianji/src/server/model/serverStatus.ts

78 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-11-07 15:13:44 +00:00
import dayjs from 'dayjs';
import { ServerStatusInfo } from '../../types';
import { createSubscribeInitializer, subscribeEventBus } from '../ws/shared';
2023-11-07 15:13:44 +00:00
import _ from 'lodash';
import { isServerOnline } from '../../shared';
const serverMap: Record<
string, // workspaceId
Record<
string, // nodeName or hostname
ServerStatusInfo
>
> = {};
createSubscribeInitializer('onServerStatusUpdate', (workspaceId) => {
if (!serverMap[workspaceId]) {
serverMap[workspaceId] = {};
}
return serverMap[workspaceId];
});
export function recordServerStatus(info: ServerStatusInfo) {
const { workspaceId, name, hostname, timeout, payload } = info;
if (!workspaceId || !name || !hostname) {
console.warn(
'[ServerStatus] lost some necessary params, request will be ignore',
info
);
return;
}
if (!serverMap[workspaceId]) {
serverMap[workspaceId] = {};
}
serverMap[workspaceId][name || hostname] = {
workspaceId,
name,
hostname,
timeout,
updatedAt: Date.now(),
payload,
};
subscribeEventBus.emit(
'onServerStatusUpdate',
workspaceId,
serverMap[workspaceId]
);
}
2023-11-07 15:13:44 +00:00
export function clearOfflineServerStatus(workspaceId: string) {
if (!serverMap[workspaceId]) {
return;
}
const offlineNode: string[] = [];
Object.entries(serverMap[workspaceId]).forEach(([key, info]) => {
if (!isServerOnline(info)) {
offlineNode.push(key);
}
});
for (const node of offlineNode) {
delete serverMap[workspaceId][node];
}
subscribeEventBus.emit(
'onServerStatusUpdate',
workspaceId,
serverMap[workspaceId]
);
return serverMap[workspaceId];
}