move processes into core
This commit is contained in:
parent
ac4b74588e
commit
3a532a05e8
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
|
@ -1,7 +1,3 @@
|
|||
process.on('uncaughtException', function(e){
|
||||
process.send({error: e.stack});
|
||||
});
|
||||
|
||||
const constants = require('../../constants');
|
||||
const Events = require('./eventsWrapper');
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
@ -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;
|
Loading…
Reference in New Issue