request(moduleId: string, msg: any, cb: Callback) { if (this.state !== ST_REGISTERED) { logger.error('agent can not request now, state:' + this.state); return; } let reqId = this.reqId++; this.callbacks[reqId] = cb; this.doSend('monitor', protocol.composeRequest(reqId, moduleId, msg)); // this.socket.emit('monitor', protocol.composeRequest(reqId, moduleId, msg)); }
let removeAllFromStore = function (self: ChannelService, key: string) { if (!!self.store) { self.store.removeAll(key, function (err) { if (!!err) { logger.error('remove key: %s all members from store, with err: %j', key, err); } }); } }
close() { if (!!this.closeMethod && this.closeMethod === 'end') { this._socket.end(); } else { try { this._socket.destroy(); } catch (e) { logger.error('socket close with destroy error: %j', e.stack); } } }
notifyById(id: string, msg: any) { (this.service.agent as MasterAgent).request(id, Constants.KEYWORDS.MONITOR_WATCHER, msg, function (signal: any) { if (signal !== Constants.SIGNAL.OK) { logger.error('master watchdog fail to notify to monitor, id: %s, msg: %j', id, msg); } else { logger.debug('master watchdog notify to monitor success, id: %s, msg: %j', id, msg); } }); }
sendRaw(msg: any) { this.socket.send(msg, 0, msg.length, this.port, this.host, function (err, bytes) { if (!!err) { logger.error('send msg to remote with err: %j', err.stack); return; } }); }
let errorHandler = function (tracer: Tracer, station: MailStation, err: Error, serverId: string, msg: object, opts: object, flag: boolean, cb: Function) { if (!!station.handleError) { station.handleError(err, serverId, msg, opts); } else { logger.error('[pinus-rpc] rpc filter error with serverId: %s, err: %j', serverId, err.stack); station.emit('error', constants.RPC_ERROR.FILTER_ERROR, tracer, serverId, msg, opts); } }
addTimeout(reconnectFlag ?: boolean) { let self = this; if (this.timeoutFlag) { return; } this.timeoutFlag = true; this.timeoutId = setTimeout(function () { self.timeoutFlag = false; logger.error('mqtt client connect %s:%d timeout %d s', self.host, self.port, self.timeout / 1000); self.socket.emit('timeout', reconnectFlag); }, self.timeout); }
let composePush = function (route: string, msgBody: any) { let msg = generate.publish(msgBody); if (!msg) { logger.error('invalid mqtt publish message: %j', msgBody); } return msg; }
/** * Run the script on the specified server */ let run = function(scriptModule: ScriptsModule, agent: MasterAgent, msg: any, cb: MasterCallback) { agent.request(msg.serverId, ScriptsModule.moduleId, msg, function(err, res) { if (err) { logger.error('fail to run script for ' + err.stack); return; } cb(null, res); }); }
let loadAllFromStore = function (self: ChannelService, key: string, cb: (err: Error, list: string[]) => void) { if (!!self.store) { self.store.load(key, function (err, list) { if (!!err) { logger.error('load key: %s from store, with err: %j', key, err); utils.invokeCallback(cb, err); } else { utils.invokeCallback(cb, null, list); } }); } }
let addToStore = function (self: ChannelService, key: string, value: string) { if (!!self.store) { self.store.add(key, value, function (err) { if (!!err) { logger.error('add key: %s value: %s to store, with err: %j', key, value, err); } }); } }
let removeFromStore = function (self: ChannelService, key: string, value: string) { if (!!self.store) { self.store.remove(key, value, function (err) { if (!!err) { logger.error('remove key: %s value: %s from store, with err: %j', key, value, err); } }); } }
close() { if (!!this.closeMethod && this.closeMethod === 'end') { this._socket.end(); } else { try { this._socket.destroy(); } catch (e) { logger.error('socket close with destroy error: %j', e.stack); } } }
notifyById(id: string, msg: any) { (this.service.agent as MasterAgent).request(id, Constants.KEYWORDS.MONITOR_WATCHER, msg, function (signal: any) { if (signal !== Constants.SIGNAL.OK) { logger.error('master watchdog fail to notify to monitor, id: %s, msg: %j', id, msg); } else { logger.debug('master watchdog notify to monitor success, id: %s, msg: %j', id, msg); } }); }
request(moduleId: string, msg: any, cb: Callback) { if (this.state !== ST_REGISTERED) { logger.error('agent can not request now, state:' + this.state); return; } let reqId = this.reqId++; this.callbacks[reqId] = cb; this.doSend('monitor', protocol.composeRequest(reqId, moduleId, msg)); // this.socket.emit('monitor', protocol.composeRequest(reqId, moduleId, msg)); }