Skip to content

Commit

Permalink
chore: ability to send custom events from workers
Browse files Browse the repository at this point in the history
  • Loading branch information
j0tunn committed Jul 24, 2019
1 parent d1867f3 commit 926b907
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/utils/workers-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {EventEmitter} = require('events');
const workerFarm = require('worker-farm');
const Promise = require('bluebird');
const _ = require('lodash');
const Events = require('../constants/runner-events');
const RuntimeConfig = require('../config/runtime-config');
const WorkerProcess = require('./worker-process');
Expand All @@ -17,6 +18,7 @@ module.exports = class WorkersRegistry extends EventEmitter {
this._config = config;
this._ended = false;
this._workerFarm = null;
this._eventBridge = new EventEmitter();
}

init() {
Expand All @@ -37,7 +39,8 @@ module.exports = class WorkersRegistry extends EventEmitter {
}

register(workerFilepath, exportedMethods) {
const workers = {};
const workers = Object.create(this._eventBridge);

for (const methodName of exportedMethods) {
workers[methodName] = (...args) => {
if (this._ended) {
Expand All @@ -46,6 +49,7 @@ module.exports = class WorkersRegistry extends EventEmitter {
return Promise.promisify(this._workerFarm)(workerFilepath, methodName, args);
};
}

return workers;
}

Expand Down Expand Up @@ -87,8 +91,8 @@ module.exports = class WorkersRegistry extends EventEmitter {
}

_initChild(child) {
child.on('message', ({event} = {}) => {
switch (event) {
child.on('message', (data = {}) => {
switch (data.event) {
case 'worker.init':
child.send({
event: 'master.init',
Expand All @@ -102,6 +106,11 @@ module.exports = class WorkersRegistry extends EventEmitter {
config: this._config.serialize()
});
break;
default:
if (data.event) {
this._eventBridge.emit(data.event, _.omit(data, 'event'));
}
break;
}
});

Expand Down
26 changes: 26 additions & 0 deletions test/lib/utils/workers-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ describe('WorkersRegistry', () => {
config: {foo: 'bar'}
});
});

it('should emit other events through workers object', () => {
const workersRegistry = mkWorkersRegistry_();
const workers = workersRegistry.register(null, []);

const onEvent = sandbox.stub().named('onEvent');
workers.on('foo', onEvent);

const child = initChild_();
child.emit('message', {event: 'foo', bar: 'baz'});

assert.calledOnceWith(onEvent, {bar: 'baz'});
});

it('should not emit unknown events (without event field) through workers object', () => {
const workersRegistry = mkWorkersRegistry_();
const workers = workersRegistry.register(null, []);

const onEvent = sandbox.stub().named('onEvent');
workers.on('foo', onEvent);

const child = initChild_();
child.emit('message', {foo: 'bar'});

assert.notCalled(onEvent);
});
});

describe('execute worker\'s method', () => {
Expand Down

0 comments on commit 926b907

Please sign in to comment.