2018-07-19 20:31:28 +00:00
|
|
|
|
|
|
|
class ProcessManager {
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.processes = {};
|
|
|
|
|
2018-08-01 15:14:02 +00:00
|
|
|
this._registerAsPlugin();
|
|
|
|
this._registerEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
_registerAsPlugin() {
|
|
|
|
const self =this;
|
|
|
|
self.plugin = this.plugins.createPlugin('processManager', {});
|
|
|
|
self.plugin.registerAPICall(
|
|
|
|
'get',
|
2018-08-01 16:53:37 +00:00
|
|
|
'/embark-api/processes',
|
2018-08-01 15:14:02 +00:00
|
|
|
(req, res) => {
|
2018-08-07 14:09:55 +00:00
|
|
|
const formatter = (acc, processName) => {
|
|
|
|
acc.push({state: self.processes[processName].state, name: processName});
|
|
|
|
return acc;
|
|
|
|
};
|
2018-10-03 13:22:42 +00:00
|
|
|
// add "embark" process to list of running processes
|
|
|
|
res.send(Object.keys(self.processes).reduce(formatter, []).concat({ state: "running", name: "embark" }));
|
2018-08-01 15:14:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_registerEvents() {
|
|
|
|
const self = this;
|
2018-07-19 20:31:28 +00:00
|
|
|
self.events.setCommandHandler('processes:register', (name, cb) => {
|
|
|
|
this.processes[name] = {
|
|
|
|
state: 'unstarted',
|
|
|
|
cb: cb
|
2018-07-26 16:37:33 +00:00
|
|
|
};
|
2018-07-19 20:31:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.events.setCommandHandler('processes:launch', (name, cb) => {
|
|
|
|
let process = self.processes[name];
|
2018-08-01 15:14:02 +00:00
|
|
|
if (process.state !== 'unstarted') {
|
2018-07-19 20:31:28 +00:00
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
process.state = 'starting';
|
2018-07-26 16:37:33 +00:00
|
|
|
process.cb.apply(process.cb, [
|
|
|
|
() => {
|
|
|
|
process.state = 'running';
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
]);
|
2018-07-19 20:31:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ProcessManager;
|