fix: 热重载容错

This commit is contained in:
手瓜一十雪 2024-05-17 21:44:52 +08:00
parent 252358ed66
commit 26e224f852

View File

@ -208,70 +208,75 @@ export class NapCatOnebot11 {
} }
} }
async SetConfig(NewOb11: OB11Config) { async SetConfig(NewOb11: OB11Config) {
function isEqual(obj1: any, obj2: any) { try {
if (obj1 === obj2) return true; function isEqual(obj1: any, obj2: any) {
if (obj1 == null || obj2 == null) return false; if (obj1 === obj2) return true;
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return obj1 === obj2; if (obj1 == null || obj2 == null) return false;
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return obj1 === obj2;
const keys1 = Object.keys(obj1); const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2); const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false; if (keys1.length !== keys2.length) return false;
for (let key of keys1) { for (let key of keys1) {
if (!isEqual(obj1[key], obj2[key])) return false; if (!isEqual(obj1[key], obj2[key])) return false;
}
return true;
} }
return true; // if (!NewOb11 || typeof NewOb11 !== 'object') {
} // throw new Error('Invalid configuration object');
// if (!NewOb11 || typeof NewOb11 !== 'object') { // }
// throw new Error('Invalid configuration object');
// }
const isHttpChanged = !isEqual(NewOb11.http.port, ob11Config.http.port) && NewOb11.http.enable; const isHttpChanged = !isEqual(NewOb11.http.port, ob11Config.http.port) && NewOb11.http.enable;
const isWsChanged = !isEqual(NewOb11.ws.port, ob11Config.ws.port); const isWsChanged = !isEqual(NewOb11.ws.port, ob11Config.ws.port);
const isEnableWsChanged = !isEqual(NewOb11.ws.enable, ob11Config.ws.enable); const isEnableWsChanged = !isEqual(NewOb11.ws.enable, ob11Config.ws.enable);
const isEnableWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, ob11Config.reverseWs.enable); const isEnableWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, ob11Config.reverseWs.enable);
const isWsReverseUrlsChanged = !isEqual(NewOb11.reverseWs.urls, ob11Config.reverseWs.urls); const isWsReverseUrlsChanged = !isEqual(NewOb11.reverseWs.urls, ob11Config.reverseWs.urls);
if (isHttpChanged) { if (isHttpChanged) {
ob11HTTPServer.restart(NewOb11.http.port, NewOb11.http.host); ob11HTTPServer.restart(NewOb11.http.port, NewOb11.http.host);
} }
if (!NewOb11.http.enable) { if (!NewOb11.http.enable) {
ob11HTTPServer.stop(); ob11HTTPServer.stop();
} else {
ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
}
if (isWsChanged) {
ob11WebsocketServer.restart(NewOb11.ws.port);
}
if (isEnableWsChanged) {
if (NewOb11.ws.enable) {
ob11WebsocketServer.start(NewOb11.ws.port, NewOb11.ws.host);
} else { } else {
ob11WebsocketServer.stop(); ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
} }
if (isWsChanged) {
ob11WebsocketServer.restart(NewOb11.ws.port);
}
if (isEnableWsChanged) {
if (NewOb11.ws.enable) {
ob11WebsocketServer.start(NewOb11.ws.port, NewOb11.ws.host);
} else {
ob11WebsocketServer.stop();
}
}
if (isEnableWsReverseChanged) {
if (NewOb11.reverseWs.enable) {
ob11ReverseWebsockets.start();
} else {
ob11ReverseWebsockets.stop();
}
}
if (NewOb11.reverseWs.enable && isWsReverseUrlsChanged) {
logDebug('反向ws地址有变化, 重启反向ws服务');
ob11ReverseWebsockets.restart();
}
if (NewOb11.http.enableHeart) {
httpHeart.start();
} else if (!NewOb11.http.enableHeart) {
httpHeart.stop();
}
ob11Config.save(NewOb11);
} catch (e) {
logError('热重载配置失败', e);
} }
if (isEnableWsReverseChanged) {
if (NewOb11.reverseWs.enable) {
ob11ReverseWebsockets.start();
} else {
ob11ReverseWebsockets.stop();
}
}
if (NewOb11.reverseWs.enable && isWsReverseUrlsChanged) {
logDebug('反向ws地址有变化, 重启反向ws服务');
ob11ReverseWebsockets.restart();
}
if (NewOb11.http.enableHeart) {
httpHeart.start();
} else if (!NewOb11.http.enableHeart) {
httpHeart.stop();
}
ob11Config.save(NewOb11);
} }
async postGroupNotifies(notifies: GroupNotify[]) { async postGroupNotifies(notifies: GroupNotify[]) {
for (const notify of notifies) { for (const notify of notifies) {