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; } } }