Skip to content

Commit

Permalink
solve stray unhandled promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
nomilous committed Nov 1, 2016
1 parent 5dbc36c commit e0d2995
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 40 deletions.
15 changes: 6 additions & 9 deletions lib/Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class Cluster extends EventEmitter {
return new Promise((resolve, reject) => {
if (!this._masterName) return reject(new Error('TODO: WAIT FOR MASTER on ' + this._vertex.name)); // TODO...
if (!this.members[this._masterName]) return reject(new Error('TODO: WAIT FOR MASTER on ' + this._vertex.name)); // TODO...

let master = this.members[this._masterName];
if (master._sys.closed) return reject(new Error('TODO: MASTER CLOSED'));
resolve(this.members[this._masterName]);
});
}
Expand Down Expand Up @@ -160,10 +163,7 @@ class Cluster extends EventEmitter {
if (member.self) {
error = new VertexError('Lost socket to self');
this.log.fatal('lost socket to self', error);
this._vertex.$stop().then(() => {
// this._vertex.emit('error', error);
});
this._vertex.emit('error', error);
this._vertex.$stop(error).then(() => {});
return;
}

Expand All @@ -174,8 +174,7 @@ class Cluster extends EventEmitter {
}

this.log.fatal('delete member %s denied', member.name);
this._vertex.$stop().then(() => {});
this._vertex.emit('error', error);
this._vertex.$stop(error).then(() => {});
};

let consensus = this.config.depart.consensus;
Expand All @@ -184,16 +183,14 @@ class Cluster extends EventEmitter {
let retries = 0;
let handleError = error => {
// perhaps also lost master, retry with new one
console.log('stop', retries);
if (retries++ < 1) {
this.log.warn('delete member %s error', member.name, error);
this._memberStore.del(member.name, {consensus: consensus, expire: expire})
.then(handleResult)
.catch(handleError);
} else {
this.log.fatal('delete member %s error', member.name, error);
this._vertex.$stop().then(() => {});
this._vertex.emit('error', error);
this._vertex.$stop(error).then(() => {});
}
};

Expand Down
54 changes: 27 additions & 27 deletions lib/KeyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,33 +450,33 @@ class KeyStore {
return this._doSetSeed(store, key, value)
}

return new Promise((resolve, reject) => {
this._cluster.getMaster()
.then(master => {
let update = {
store: store.name,
act: action,
key: key,
};
if (typeof value != 'undefined') {
update.value = value;
}
if (typeof opts != 'undefined') {
update.opts = opts;
}
return master._sys.send([{
FOR: constants.CLUSTER_ROUTE_CODE,
DO: constants.CLUSTER_HANDLER_STORE_1
}, update]);
})
.then(({store1}) => {
if (store1 instanceof Error) {
return reject(store1);
}
resolve(store1)
})
.catch(reject);
});
return this._cluster.getMaster()
.then(master => {
let update = {
store: store.name,
act: action,
key: key,
};
if (typeof value != 'undefined') {
update.value = value;
}
if (typeof opts != 'undefined') {
update.opts = opts;
}
return master._sys.send([{
FOR: constants.CLUSTER_ROUTE_CODE,
DO: constants.CLUSTER_HANDLER_STORE_1
}, update]);
})
.then(({store1}) => {
if (store1 instanceof Error) {
// return reject(store1);
throw store1;
}
// resolve(store1)
return store1;
});

}


Expand Down
10 changes: 9 additions & 1 deletion lib/Vertex.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,18 @@ class Vertex extends EventEmitter {
}


$stop() {
$stop(error) {
if (this.stopping) return Promise.resolve();
if (this.stopped) return Promise.resolve();
this.stopping = true;
this._error = error;

try {
this.emit('error', error);
} catch (e) {
// don't want to stop stopping if their is no error listener
}

return new Promise(resolve => {
if (this._services.length == 0) {
this.log.debug('stopped');
Expand Down
8 changes: 6 additions & 2 deletions test/func-Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const expect = require('expect.js');
const Vertex = require('../');
const hooks = require('./lib/hooks');

// process.on('unhandledRejection', (reason, promise) => {
// console.log('REASON', reason);
// });

describe(filename, () => {

let logLevel = (nfo) => {
Expand Down Expand Up @@ -320,15 +324,15 @@ describe(filename, () => {

});

it.only('stops the vertex on lost socket where cluster disagrees', done => {
it('stops the vertex on lost socket where cluster disagrees', done => {

let {servers} = cluster;
let server = servers.pop();

server.on('stopped', () => {
setTimeout(() => {
expect(
Object.keys(servers[0].cluster._members)
Object.keys(servers[0].cluster._members).sort()
).to.eql([
'node-0',
'node-1',
Expand Down
10 changes: 9 additions & 1 deletion test/unit-KeyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ describe(filename, () => {
log: new VertexLogger({name: 'cluster', root: 'test-node', level: 'off'}),
config: {},
members: {},
getMaster: () => {},
getMaster: () => {
return {
then: () => {
return {
then: () => {}
}
}
}
},
_noConnect: true,
on: (event, handler) => {
if (event == 'member/add') addMemberHandler = handler;
Expand Down

0 comments on commit e0d2995

Please sign in to comment.