diff --git a/lib/core/processes/processManager.js b/lib/core/processes/processManager.js index 2fa52af0e..9bc9a8d05 100644 --- a/lib/core/processes/processManager.js +++ b/lib/core/processes/processManager.js @@ -1,4 +1,11 @@ +const ProcessState = { + Unstarted: 'unstarted', + Starting: 'starting', + Running: 'running', + Stopping: 'stopping', +} + class ProcessManager { constructor(options) { this.logger = options.logger; @@ -29,22 +36,48 @@ class ProcessManager { _registerEvents() { const self = this; self.events.setCommandHandler('processes:register', (name, cb) => { + + let launchFn, stopFn; + + if (typeof cb === 'object') { + launchFn = cb.launchFn; + stopFn = cb.stopFn; + } + this.processes[name] = { - state: 'unstarted', - cb: cb + name: name, + state: ProcessState.Unstarted, + cb: launchFn || cb, + stopFn: stopFn || function noop () {} }; }); self.events.setCommandHandler('processes:launch', (name, cb) => { + cb = cb || function noop() {}; let process = self.processes[name]; - if (process.state !== 'unstarted') { + if (process.state !== ProcessState.Unstarted) { return cb(); } - process.state = 'starting'; + process.state = ProcessState.Starting; process.cb.apply(process.cb, [ - () => { - process.state = 'running'; - cb(); + (...args) => { + process.state = ProcessState.Running; + cb.apply(cb, args); + } + ]); + }); + + self.events.setCommandHandler('processes:stop', (name, cb) => { + let process = self.processes[name]; + cb = cb || function noop() {}; + if (process.state !== ProcessState.Running) { + return cb(); + } + process.state = ProcessState.Stopping; + process.stopFn.apply(process.stopFn, [ + (...args) => { + process.state = ProcessState.Unstarted; + cb.apply(cb, args); } ]); });