Skip to content

Commit

Permalink
Make Worker.whenForeignPropertiesReceived return promise
Browse files Browse the repository at this point in the history
  • Loading branch information
mcheshkov committed Jun 20, 2018
1 parent b63f51f commit 9396456
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2017
}
}
23 changes: 10 additions & 13 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,14 @@ class Worker extends ClusterProcess {
this.emit('foreign properties received');
}

/**
* @param {Function} fn
*/
static whenForeignPropertiesReceived(fn) {
return /** @this {Worker} */function() {
whenForeignPropertiesReceived() {
return new Promise(resolve => {
if (this._foreignPropertiesReceived) {
setImmediate(fn.bind(this));
resolve();
} else {
this.once('foreign properties received', fn.bind(this));
this.once('foreign properties received', resolve);
}

return this;
};
});
}

/**
Expand Down Expand Up @@ -153,18 +148,20 @@ class Worker extends ClusterProcess {
* @public
*/
Worker.prototype.run = Worker.whenInitialized(
Worker.whenForeignPropertiesReceived(function() {
async function() {
await this.whenForeignPropertiesReceived();

const workerBase = this.config.resolve('app');

require(workerBase);
this.emit('loaded', workerBase);

if ( ! this.config.get('control.triggerReadyStateManually', false)) {
if (!this.config.get('control.triggerReadyStateManually', false)) {
setImmediate(this.ready.bind(this));
}

return this;
}));
});

/**
* Call Master method via RPC
Expand Down
1 change: 1 addition & 0 deletions test/func/test/restart_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('restart queue', () => {
const expected = [
'restarting',
'exit 1',
'run 1',
'dead 1',
'exit 1',
'exit 2',
Expand Down

0 comments on commit 9396456

Please sign in to comment.