Skip to content

Commit

Permalink
fix: remove internal interval handler when close agent (#4654)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Apr 13, 2021
1 parent 6a6d68f commit ce23422
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class Agent extends EggApplication {
// dump config after loaded, ensure all the dynamic modifications will be recorded
const dumpStartTime = Date.now();
this.dumpConfig();
this.coreLogger.info('[egg:core] dump config after load, %s', ms(Date.now() - dumpStartTime));
this.coreLogger.info(
'[egg:core] dump config after load, %s',
ms(Date.now() - dumpStartTime)
);

// keep agent alive even it doesn't have any io tasks
setInterval(() => {}, 24 * 60 * 60 * 1000);
this.agentAliveHandler = setInterval(() => {}, 24 * 60 * 60 * 1000);

this._uncaughtExceptionHandler = this._uncaughtExceptionHandler.bind(this);
process.on('uncaughtException', this._uncaughtExceptionHandler);
Expand All @@ -55,16 +58,25 @@ class Agent extends EggApplication {
}

_wrapMessenger() {
for (const methodName of [ 'broadcast', 'sendTo', 'sendToApp', 'sendToAgent', 'sendRandom' ]) {
for (const methodName of [
'broadcast',
'sendTo',
'sendToApp',
'sendToAgent',
'sendRandom',
]) {
wrapMethod(methodName, this.messenger, this.coreLogger);
}

function wrapMethod(methodName, messenger, logger) {
const originMethod = messenger[methodName];
messenger[methodName] = function() {
const stack = new Error().stack.split('\n').slice(1).join('\n');
logger.warn('agent can\'t call %s before server started\n%s',
methodName, stack);
logger.warn(
"agent can't call %s before server started\n%s",
methodName,
stack
);
originMethod.apply(this, arguments);
};
messenger.prependOnceListener('egg-ready', () => {
Expand All @@ -75,9 +87,9 @@ class Agent extends EggApplication {

close() {
process.removeListener('uncaughtException', this._uncaughtExceptionHandler);
clearInterval(this.agentAliveHandler);
return super.close();
}

}

module.exports = Agent;

0 comments on commit ce23422

Please sign in to comment.