From 3a532a05e80a155317cf3c4be7bcca33ff676109 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 27 Jul 2018 17:33:50 -0400 Subject: [PATCH] move processes into core --- lib/core/processes/processLauncher.js | 3 - .../processes/processLauncher.js~develop_51} | 7 ++- lib/core/processes/processManager.js | 2 +- .../processes/processManager.js~develop_51} | 3 +- lib/core/processes/processWrapper.js | 4 -- .../processes/processWrapper.js~develop_51} | 18 +++--- lib/processes/eventsWrapper.js | 55 ------------------- 7 files changed, 16 insertions(+), 76 deletions(-) rename lib/{processes/processLauncher.js => core/processes/processLauncher.js~develop_51} (97%) rename lib/{processes/processManager.js => core/processes/processManager.js~develop_51} (88%) rename lib/{processes/processWrapper.js => core/processes/processWrapper.js~develop_51} (88%) delete mode 100644 lib/processes/eventsWrapper.js diff --git a/lib/core/processes/processLauncher.js b/lib/core/processes/processLauncher.js index 7ee80fc5..88ad8f4c 100644 --- a/lib/core/processes/processLauncher.js +++ b/lib/core/processes/processLauncher.js @@ -42,9 +42,6 @@ class ProcessLauncher { _subscribeToMessages() { const self = this; this.process.on('message', (msg) => { - if (msg.error) { - self.logger.error(msg.error); - } if (msg.result === constants.process.log) { return self._handleLog(msg); } diff --git a/lib/processes/processLauncher.js b/lib/core/processes/processLauncher.js~develop_51 similarity index 97% rename from lib/processes/processLauncher.js rename to lib/core/processes/processLauncher.js~develop_51 index 9c2c1b1d..1bbf9153 100644 --- a/lib/processes/processLauncher.js +++ b/lib/core/processes/processLauncher.js~develop_51 @@ -1,7 +1,7 @@ const child_process = require('child_process'); -const constants = require('../constants'); +const constants = require('../../constants'); const path = require('path'); -const utils = require('../utils/utils'); +const utils = require('../../utils/utils'); class ProcessLauncher { @@ -29,6 +29,9 @@ class ProcessLauncher { _subscribeToMessages() { const self = this; this.process.on('message', (msg) => { + if (msg.error) { + self.logger.error(msg.error); + } if (msg.result === constants.process.log) { return self._handleLog(msg); } diff --git a/lib/core/processes/processManager.js b/lib/core/processes/processManager.js index dbb4450d..2556acb5 100644 --- a/lib/core/processes/processManager.js +++ b/lib/core/processes/processManager.js @@ -16,7 +16,7 @@ class ProcessManager { self.events.setCommandHandler('processes:launch', (name, cb) => { let process = self.processes[name]; - if (process.state !== 'unstarted') { + if (process.state != 'unstarted') { return cb(); } process.state = 'starting'; diff --git a/lib/processes/processManager.js b/lib/core/processes/processManager.js~develop_51 similarity index 88% rename from lib/processes/processManager.js rename to lib/core/processes/processManager.js~develop_51 index 32a083ca..dbb4450d 100644 --- a/lib/processes/processManager.js +++ b/lib/core/processes/processManager.js~develop_51 @@ -16,8 +16,7 @@ class ProcessManager { self.events.setCommandHandler('processes:launch', (name, cb) => { let process = self.processes[name]; - if (process.state != 'unstarted') { - console.dir("=====> already started " + name); + if (process.state !== 'unstarted') { return cb(); } process.state = 'starting'; diff --git a/lib/core/processes/processWrapper.js b/lib/core/processes/processWrapper.js index 26137474..5948444e 100644 --- a/lib/core/processes/processWrapper.js +++ b/lib/core/processes/processWrapper.js @@ -1,7 +1,3 @@ -process.on('uncaughtException', function(e){ - process.send({error: e.stack}); -}); - const constants = require('../../constants'); const Events = require('./eventsWrapper'); diff --git a/lib/processes/processWrapper.js b/lib/core/processes/processWrapper.js~develop_51 similarity index 88% rename from lib/processes/processWrapper.js rename to lib/core/processes/processWrapper.js~develop_51 index 30108fe4..3b934a10 100644 --- a/lib/processes/processWrapper.js +++ b/lib/core/processes/processWrapper.js~develop_51 @@ -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'); -// Override process.chdir so that we have a partial-implementation PWD for Windows -const realChdir = process.chdir; -process.chdir = (...args) => { - if (!process.env.PWD) { - process.env.PWD = process.cwd(); - } - realChdir(...args); -}; +// Set PWD to CWD since Windows doesn't have a value for PWD +if (!process.env.PWD) { + process.env.PWD = process.cwd(); +} class ProcessWrapper { diff --git a/lib/processes/eventsWrapper.js b/lib/processes/eventsWrapper.js deleted file mode 100644 index de102158..00000000 --- a/lib/processes/eventsWrapper.js +++ /dev/null @@ -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;