embark/lib/process/processLauncher.js

210 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-05-16 20:41:15 +00:00
const child_process = require('child_process');
const constants = require('../constants');
2018-05-16 15:49:38 +00:00
2018-05-16 20:41:15 +00:00
class ProcessLauncher {
2018-05-16 15:49:38 +00:00
/**
* Constructor of ProcessLauncher. Forks the module and sets up the message handling
* @param {Object} options Options tp start the process
* * modulePath {String} Absolute path to the module to fork
* * logger {Object} Logger
* * normalizeInput {Function} Function to normalize logs
* * events {Function} Events Emitter instance
* @return {ProcessLauncher} The ProcessLauncher instance
*/
2018-05-16 20:41:15 +00:00
constructor(options) {
this.process = child_process.fork(options.modulePath);
this.logger = options.logger;
this.normalizeInput = options.normalizeInput;
this.events = options.events;
this.silent = options.silent;
2018-05-16 20:41:15 +00:00
this.subscriptions = {};
this._subscribeToMessages();
}
2018-05-16 15:49:38 +00:00
// Subscribes to messages from the child process and delegates to the right methods
2018-05-16 20:41:15 +00:00
_subscribeToMessages() {
const self = this;
this.process.on('message', (msg) => {
if (msg.result === constants.process.log) {
2018-05-16 14:19:46 +00:00
return self._handleLog(msg);
}
if (msg.event) {
return self._handleEvent(msg);
2018-05-16 20:41:15 +00:00
}
self._checkSubscriptions(msg);
2018-05-16 14:19:46 +00:00
});
this.process.on('exit', (code) => {
self.logger.info('Process exited with code ' + code);
});
2018-05-16 14:19:46 +00:00
}
2018-05-16 20:41:15 +00:00
2018-05-16 15:49:38 +00:00
// Translates logs from the child process to the logger
2018-05-16 14:19:46 +00:00
_handleLog(msg) {
if (this.silent && msg.type !== 'error') {
return;
}
2018-05-16 14:19:46 +00:00
if (this.logger[msg.type]) {
return this.logger[msg.type](this.normalizeInput(msg.message));
}
this.logger.debug(this.normalizeInput(msg.message));
}
2018-05-16 15:49:38 +00:00
// Handle event calls from the child process
2018-05-16 14:19:46 +00:00
_handleEvent(msg) {
const self = this;
if (!self.events[msg.event]) {
self.logger.warn('Unknown event method called: ' + msg.event);
return;
}
if (!msg.args || !Array.isArray(msg.args)) {
msg.args = [];
}
// Add callback in the args
msg.args.push((result) => {
self.process.send({
event: constants.process.events.response,
result,
eventId: msg.eventId
});
});
self.events[msg.event](msg.requestName, ...msg.args);
2018-05-16 14:19:46 +00:00
}
2018-05-16 20:41:15 +00:00
2018-05-16 15:49:38 +00:00
// Looks at the subscriptions to see if there is a callback to call
2018-05-16 14:19:46 +00:00
_checkSubscriptions(msg) {
const messageKeys = Object.keys(msg);
const subscriptionsKeys = Object.keys(this.subscriptions);
2018-05-18 18:11:29 +00:00
let subscriptionsForKey;
2018-05-16 14:19:46 +00:00
let messageKey;
// Find if the message contains a key that we are subscribed to
messageKeys.some(_messageKey => {
return subscriptionsKeys.some(subscriptionKey => {
if (_messageKey === subscriptionKey) {
2018-05-18 18:11:29 +00:00
subscriptionsForKey = this.subscriptions[subscriptionKey];
2018-05-16 14:19:46 +00:00
messageKey = _messageKey;
return true;
}
return false;
});
});
2018-05-16 20:41:15 +00:00
2018-05-18 18:11:29 +00:00
if (subscriptionsForKey) {
2018-05-16 14:19:46 +00:00
// Find if we are subscribed to one of the values
2018-05-18 18:11:29 +00:00
let subsIndex = [];
const subscriptionsForValue = subscriptionsForKey.filter((sub, index) => {
2018-05-16 14:19:46 +00:00
if (msg[messageKey] === sub.value) {
2018-05-18 18:11:29 +00:00
subsIndex.push(index);
2018-05-16 14:19:46 +00:00
return true;
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
return false;
});
2018-05-18 18:11:29 +00:00
if (subscriptionsForValue.length) {
2018-05-16 14:19:46 +00:00
// We are subscribed to that message, call the callback
2018-05-18 18:11:29 +00:00
subscriptionsForValue.forEach((subscription, index) => {
subscription.callback(msg);
if (subscription.once) {
// Called only once, we can remove it
subscription = null;
this.subscriptions[messageKey].splice(subsIndex[index], 1);
}
});
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
}
2018-05-16 20:41:15 +00:00
}
2018-05-16 15:49:38 +00:00
/**
* Subscribe to a message using a key-value pair
* @param {String} key Message key to subscribe to
* @param {String} value Value that the above key must have for the callback to be called
* @param {Function} callback callback(response)
* @return {void}
*/
2018-05-18 18:11:29 +00:00
on(key, value, callback) {
2018-05-16 20:41:15 +00:00
if (this.subscriptions[key]) {
2018-05-16 18:06:34 +00:00
this.subscriptions[key].push({value, callback});
2018-05-16 20:41:15 +00:00
return;
}
this.subscriptions[key] = [{value, callback}];
}
2018-05-18 18:11:29 +00:00
/**
* Same as .on, but only triggers once
* @param {String} key Message key to subscribe to
* @param {String} value Value that the above key must have for the callback to be called
* @param {Function} callback callback(response)
* @return {void}
*/
once(key, value, callback) {
const obj = {value, callback, once: true};
if (this.subscriptions[key]) {
this.subscriptions[key].push(obj);
return;
}
this.subscriptions[key] = [obj];
}
2018-05-16 15:49:38 +00:00
/**
* Unsubscribes from a previously subscribed key-value pair (or key if no value)
* @param {String} key Message key to unsubscribe
* @param {String} value [Optional] Value of the key to unsubscribe
* If there is no value, unsubscribes from all the values of that key
* @return {void}
*/
2018-05-16 20:41:15 +00:00
unsubscribeTo(key, value) {
if (!value) {
this.subscriptions[key] = [];
}
if (this.subscriptions[key]) {
this.subscriptions[key].filter((val, index) => {
if (val.value === value) {
2018-05-16 18:06:34 +00:00
this.subscriptions[key].splice(index, 1);
2018-05-16 20:41:15 +00:00
}
});
}
}
2018-05-16 15:49:38 +00:00
/**
* Unsubscribes from all subscriptions
* @return {void}
*/
2018-05-16 18:06:34 +00:00
unsubscribeToAll() {
2018-05-16 20:41:15 +00:00
this.subscriptions = {};
}
2018-05-16 15:49:38 +00:00
/**
* Sends a message to the child process. Same as ChildProcess.send()
* @params {Object} message Message to send
* For other parameters, see:
* https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
* @return {void}
*/
2018-05-16 20:41:15 +00:00
send() {
this.process.send(...arguments);
}
2018-05-16 15:49:38 +00:00
/**
* Disconnects the child process. It will exit on its own
* @return {void}
*/
2018-05-16 20:41:15 +00:00
disconnect() {
this.process.disconnect();
}
2018-05-16 15:49:38 +00:00
/**
* Kills the child process
* https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal
* @return {void}
*/
2018-05-16 20:41:15 +00:00
kill() {
this.process.kill(...arguments);
}
}
module.exports = ProcessLauncher;