embark-area-51/lib/process/processLauncher.js

178 lines
5.0 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;
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 this._handleEvent(msg);
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
this._checkSubscriptions(msg);
});
}
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.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);
let subscriptions;
let messageKey;
// Find if the message contains a key that we are subscribed to
messageKeys.some(_messageKey => {
return subscriptionsKeys.some(subscriptionKey => {
if (_messageKey === subscriptionKey) {
subscriptions = this.subscriptions[subscriptionKey];
messageKey = _messageKey;
return true;
}
return false;
});
});
2018-05-16 20:41:15 +00:00
2018-05-16 14:19:46 +00:00
if (subscriptions) {
let subscription;
// Find if we are subscribed to one of the values
subscriptions.some(sub => {
if (msg[messageKey] === sub.value) {
subscription = sub;
return true;
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
return false;
});
if (subscription) {
// We are subscribed to that message, call the callback
subscription.callback(msg);
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-16 20:41:15 +00:00
subscribeTo(key, value, callback) {
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-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;