feat(加入热更和eval的管理模块):

This commit is contained in:
liangtongchuan
2023-05-05 15:42:17 +08:00
committed by luying
parent a362a397b5
commit 3a41136722
4 changed files with 176 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
import { Application, IModule, MonitorCallback, MasterCallback, MonitorAgent, MasterAgent } from "pinus";
export class UsrAdminModule implements IModule {
app: Application;
root: string;
static moduleId = 'UsrAdmin';
constructor(opts: { app: Application, path: string }) {
this.app = opts.app;
this.root = opts.path;
}
masterHandler(agent: MasterAgent, msg: any, cb: MasterCallback) {
console.log("enter masterHandler", msg);
}
monitorHandler(agent: MonitorAgent, msg: { action: string, script: string, serverId: string }, cb: MonitorCallback) {
console.log("enter monitorHandler", msg);
if (['master'].includes(this.app.getServerType())) {
console.log('scripts module can not be loaded on master');
return;
}
switch (msg.action) {
case 'hotupdate':
this.app.get('UpdateComponent').hotupdate(msg.script, (err, res) => {
if (err) {
console.log("hotupdate error", err);
cb(err);
return;
}
console.log("hotupdate success", res);
cb(null, res);
});
break;
case 'eval':
this.app.get('EvalComponent').eval(msg.script, (err, res) => {
if (err) {
console.log("eval error", err);
cb(err);
return;
}
console.log("eval success", res);
cb(null, res);
});
break;
default:
break;
}
}
clientHandler(agent: MasterAgent, msg: { action: string, script: string, serverId: string }, cb: MasterCallback) {
const { action, serverId } = msg;
switch (action) {
case 'hotupdate':
agent.notifyAll(UsrAdminModule.moduleId, msg);
cb(null, 'success notifyAll servers');
break;
case 'eval':
agent.request(serverId, UsrAdminModule.moduleId, msg, (err, res) => {
if (err) {
console.log("eval error", err);
cb(err, res);
return;
}
console.log("eval success", res);
cb(null, res);
});
break;
default:
break;
}
}
}