Skip to content

Commit

Permalink
feat(graceful): support serverGracefulIgnoreCode (#5027)
Browse files Browse the repository at this point in the history
Co-authored-by: hugh.hyj <hugh.hyj@alibaba-inc.com>
  • Loading branch information
hyj1991 and hugh.hyj committed Sep 23, 2022
1 parent 8b8dd3b commit a0761d6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class Application extends EggApplication {
onServer(server) {
// expose app.server
this.server = server;
// set ignore code
const serverGracefulIgnoreCode = this.config.serverGracefulIgnoreCode || [];

/* istanbul ignore next */
graceful({
Expand All @@ -177,6 +179,7 @@ class Application extends EggApplication {
}
this.coreLogger.error(err);
},
ignoreCode: serverGracefulIgnoreCode,
});

server.on('clientError', (err, socket) => this.onClientError(err, socket));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"egg-view": "^2.1.3",
"egg-watcher": "^3.1.1",
"extend2": "^1.0.1",
"graceful": "^1.0.2",
"graceful": "^1.1.0",
"humanize-ms": "^1.2.1",
"is-type-of": "^1.2.1",
"koa-bodyparser": "^4.3.0",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/app-die-ignore-code/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
app.get('/uncaughtException', function*() {
setTimeout(() => {
const error = new Error('MockError');
error.code = 'EMOCKERROR';
throw error;
}, 100);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.keys = 'foo';

exports.serverGracefulIgnoreCode = ['EMOCKERROR'];
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-die-ignore-code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "app-die-ignore-code"
}
42 changes: 42 additions & 0 deletions test/lib/cluster/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,48 @@ describe('test/lib/cluster/master.test.js', () => {
});
});

describe('app worker should not die with matched serverGracefulIgnoreCode', () => {
let app;
before(() => {
mm.env('default');
app = utils.cluster('apps/app-die-ignore-code');
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('should not restart when matched uncaughtException happened', async () => {
try {
await app.httpRequest()
.get('/uncaughtException');
} catch (_) {
// do nothing
}

// wait for app worker restart
await sleep(5000);

// error pipe to console
app.notExpect('stdout', /app_worker#1:\d+ disconnect/);
});

it('should still log uncaughtException when matched uncaughtException happened', async () => {
try {
await app.httpRequest()
.get('/uncaughtException');
} catch (_) {
// do nothing
}

// wait for app worker restart
await sleep(5000);

app.expect('stderr', /\[graceful:worker:\d+:uncaughtException] throw error 1 times/);
app.expect('stderr', /matches ignore list/);
app.notExpect('stdout', /app_worker#1:\d+ disconnect/);
});
});

describe('Master start fail', () => {
let master;

Expand Down

0 comments on commit a0761d6

Please sign in to comment.