Skip to content

Commit

Permalink
fix: close gracefully (eggjs#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoshuai0102 authored and popomore committed Feb 22, 2017
1 parent 2a21e34 commit ed97533
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
10 changes: 9 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ const http = require('http');
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;
const util = require('./core/util');
const cluster = require('cluster-client');

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
const LOCALS = Symbol('Application#locals');
const LOCALS_LIST = Symbol('Application#localsList');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const CLUSTER_CLIENTS = Symbol.for('egg#clusterClients');

/**
* Singleton instance in App Worker, extend {@link EggApplication}
Expand All @@ -27,7 +29,13 @@ class Application extends EggApplication {
constructor(options = {}) {
options.type = 'application';
super(options);
this.loader.load();
try {
this.loader.load();
} catch (e) {
// close gracefully
this[CLUSTER_CLIENTS].forEach(cluster.close);
throw e;
}

// dump config after loaded, ensure all the dynamic modifications will be recorded
this.dumpConfig();
Expand Down
1 change: 1 addition & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class EggApplication extends EggCore {
const create = client.create;
client.create = (...args) => {
const realClient = create.apply(client, args);
this[CLUSTER_CLIENTS].push(realClient);

this.beforeClose(function* () {
yield cluster.close(realClient);
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/apps/cluster-client-error/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
const err = Error();
err.name = 'MockError';
throw err;
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/cluster-client-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "cluster-client-error"
}
33 changes: 33 additions & 0 deletions test/lib/cluster/cluster-client-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const sleep = require('ko-sleep');
const utils = require('../../utils');

describe('test/lib/cluster/cluster-client-error.test.js', () => {
let app;
before(function* () {
app = utils.app('apps/cluster-client-error');

let err;
try {
yield app.ready();
} catch (e) {
err = e;
}
assert(err);
});

it('should close even if app throw error', () => {
return app.close();
});

it('should follower not throw error', function* () {
yield sleep(1000);
const cnt = fs.readFileSync(path.join(__dirname, '../../fixtures/apps/cluster-client-error/logs/cluster-client-error/common-error.log'), 'utf8');
assert(!cnt.includes('ECONNRESET'));
});

});
12 changes: 6 additions & 6 deletions test/lib/cluster/cluster-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe('test/lib/cluster/cluster-client.test.js', () => {
app = utils.app('apps/cluster_mod_app', { coverage: true });
yield app.ready();
});
after(function* () {
yield app.close();
const agentInnerClient = app.agent.registryClient[innerClient];
assert(agentInnerClient._realClient.closed === true);
mm.restore();
});

it('should publish & subscribe ok', () => {
return request(app.callback())
Expand All @@ -33,10 +39,4 @@ describe('test/lib/cluster/cluster-client.test.js', () => {
});
});

after(function* () {
yield app.close();
const agentInnerClient = app.agent.registryClient[innerClient];
assert(agentInnerClient._realClient.closed === true);
mm.restore();
});
});

0 comments on commit ed97533

Please sign in to comment.