move processes into core

This commit is contained in:
Iuri Matias 2018-07-27 17:33:50 -04:00 committed by Pascal Precht
parent b8cbee460b
commit 6d0f4b67ae
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
5 changed files with 14 additions and 73 deletions

View File

@ -42,9 +42,6 @@ class ProcessLauncher {
_subscribeToMessages() { _subscribeToMessages() {
const self = this; const self = this;
this.process.on('message', (msg) => { this.process.on('message', (msg) => {
if (msg.error) {
self.logger.error(msg.error);
}
if (msg.result === constants.process.log) { if (msg.result === constants.process.log) {
return self._handleLog(msg); return self._handleLog(msg);
} }

View File

@ -1,7 +1,7 @@
const child_process = require('child_process'); const child_process = require('child_process');
const constants = require('../constants'); const constants = require('../../constants');
const path = require('path'); const path = require('path');
const utils = require('../utils/utils'); const utils = require('../../utils/utils');
class ProcessLauncher { class ProcessLauncher {
@ -29,6 +29,9 @@ class ProcessLauncher {
_subscribeToMessages() { _subscribeToMessages() {
const self = this; const self = this;
this.process.on('message', (msg) => { this.process.on('message', (msg) => {
if (msg.error) {
self.logger.error(msg.error);
}
if (msg.result === constants.process.log) { if (msg.result === constants.process.log) {
return self._handleLog(msg); return self._handleLog(msg);
} }

View File

@ -1,7 +1,3 @@
process.on('uncaughtException', function(e){
process.send({error: e.stack});
});
const constants = require('../../constants'); const constants = require('../../constants');
const Events = require('./eventsWrapper'); const Events = require('./eventsWrapper');

View File

@ -1,14 +1,14 @@
const constants = require('../constants'); process.on('uncaughtException', function(e){
process.send({error: e.stack});
});
const constants = require('../../constants');
const Events = require('./eventsWrapper'); const Events = require('./eventsWrapper');
// Override process.chdir so that we have a partial-implementation PWD for Windows // Set PWD to CWD since Windows doesn't have a value for PWD
const realChdir = process.chdir;
process.chdir = (...args) => {
if (!process.env.PWD) { if (!process.env.PWD) {
process.env.PWD = process.cwd(); process.env.PWD = process.cwd();
} }
realChdir(...args);
};
class ProcessWrapper { class ProcessWrapper {

View File

@ -1,55 +0,0 @@
const uuid = require('uuid/v1');
const constants = require('../constants');
class Events {
/**
* Constructs an event wrapper for processes.
* Handles sending an event message to the parent process and waiting for its response
* No need to create an instance of eventsWrapper for your own process, just extend ProcessWrapper
* Then, you an use `this.events.[on|request]` with the usual parameters you would use
*/
constructor() {
this.subscribedEvents = {};
this.listenToParentProcess();
}
listenToParentProcess() {
process.on('message', (msg) => {
if (!msg.event || msg.event !== constants.process.events.response) {
return;
}
if (!this.subscribedEvents[msg.eventId]) {
return;
}
this.subscribedEvents[msg.eventId](msg.result);
});
}
sendEvent() {
const eventType = arguments[0];
const requestName = arguments[1];
let args = [].slice.call(arguments, 2);
const eventId = uuid();
this.subscribedEvents[eventId] = args[args.length - 1];
args = args.splice(0, args.length - 2);
process.send({
event: eventType,
requestName,
args,
eventId: eventId
});
}
on() {
this.sendEvent(constants.process.events.on, ...arguments);
}
request() {
this.sendEvent(constants.process.events.request, ...arguments);
}
}
module.exports = Events;