|
| 1 | +'use strict'; |
| 2 | +const each = require('lodash/each'); |
| 3 | +const toArray = require('lodash/toArray'); |
| 4 | +const includes = require('lodash/includes'); |
| 5 | +const isFunction = require('lodash/isFunction'); |
| 6 | +const Promise = require('bluebird'); |
| 7 | + |
| 8 | +const BaseService = require('./base'); |
| 9 | +const BaseProcess = require('./process/base'); |
| 10 | +const localService = require('./process/local'); |
| 11 | + |
| 12 | +function hasRequiredFns(pm) { |
| 13 | + let unimplementedMethods = []; |
| 14 | + |
| 15 | + each(BaseProcess.requiredMethods, function (method) { |
| 16 | + if (!pm[method] || !pm.hasOwnProperty(method) || !isFunction(pm[method])) { |
| 17 | + unimplementedMethods.push(method); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + return unimplementedMethods; |
| 22 | +} |
| 23 | + |
| 24 | +class ServiceManager { |
| 25 | + constructor(config, ui) { |
| 26 | + this.config = config; |
| 27 | + this.ui = ui; |
| 28 | + |
| 29 | + this.hooks = {}; |
| 30 | + this.services = {}; |
| 31 | + this.process = null; |
| 32 | + } |
| 33 | + |
| 34 | + registerHook(hook, fn, serviceName) { |
| 35 | + if (!includes(ServiceManager.allowedHooks, hook)) { |
| 36 | + throw new Error(`Hook ${hook} does not exist.`); |
| 37 | + } |
| 38 | + |
| 39 | + if (!this.services[serviceName]) { |
| 40 | + throw new Error(`Service ${serviceName} does not exist`); |
| 41 | + } |
| 42 | + |
| 43 | + if (!this.hooks[hook]) { |
| 44 | + this.hooks[hook] = {}; |
| 45 | + } |
| 46 | + |
| 47 | + this.hooks[hook][serviceName] = fn; |
| 48 | + } |
| 49 | + |
| 50 | + callHook(hook) { |
| 51 | + if (!includes(ServiceManager.allowedHooks, hook)) { |
| 52 | + throw new Error(`Hook ${hook} does not exist.`); |
| 53 | + } |
| 54 | + |
| 55 | + let args = toArray(arguments).slice(1); |
| 56 | + let hooks = this.hooks[hook] || {}; |
| 57 | + |
| 58 | + return Promise.each(Object.keys(hooks), (serviceName) => { |
| 59 | + let fn = hooks[serviceName]; |
| 60 | + return fn.apply(this.services[serviceName], args); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + _loadProcess(name, Process) { |
| 65 | + if (this.process || name !== this.config.get('process')) { |
| 66 | + // Process manager has already been loaded, or the name does not |
| 67 | + // match the configured process manager |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!(Process.prototype instanceof BaseProcess)) { |
| 72 | + throw new Error(`Configured process manager ${name} does not extend BaseProcess!`); |
| 73 | + } |
| 74 | + |
| 75 | + let missingFns = hasRequiredFns(Process.prototype); |
| 76 | + |
| 77 | + if (missingFns.length) { |
| 78 | + throw new Error(`Configured manager ${name} is missing the following required methods: ${missingFns.join(', ')}`); |
| 79 | + } |
| 80 | + |
| 81 | + if (!Process.willRun()) { |
| 82 | + this.ui.log(`The '${name}' process manager will not run on this system, defaulting to 'local'`, 'yellow'); |
| 83 | + this.config.set('process', 'local'); |
| 84 | + return this._loadProcess('local', localService.class); |
| 85 | + } |
| 86 | + |
| 87 | + this.process = this._loadService(name, Process); |
| 88 | + } |
| 89 | + |
| 90 | + _loadService(name, ServiceClass) { |
| 91 | + if (this.services[name]) { |
| 92 | + throw new Error('Service already exists!'); |
| 93 | + } |
| 94 | + |
| 95 | + let service = new ServiceClass(this); |
| 96 | + // Inject name into the service instance so it's accessible by the methods |
| 97 | + service.name = name; |
| 98 | + service.init(); |
| 99 | + this.services[name] = service; |
| 100 | + |
| 101 | + return service; |
| 102 | + } |
| 103 | + |
| 104 | + static load(config, ui) { |
| 105 | + let sm = new ServiceManager(config, ui); |
| 106 | + |
| 107 | + each(ServiceManager.knownServices, (service) => { |
| 108 | + let ServiceClass = service.class; |
| 109 | + |
| 110 | + if (!(ServiceClass.prototype instanceof BaseService)) { |
| 111 | + throw new Error('Service does not inherit from BaseService'); |
| 112 | + } |
| 113 | + |
| 114 | + let name = service.name; |
| 115 | + |
| 116 | + if (service.type === 'process') { |
| 117 | + sm._loadProcess(name, ServiceClass); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + sm._loadService(name, ServiceClass); |
| 122 | + }); |
| 123 | + |
| 124 | + return sm; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +ServiceManager.allowedHooks = ['setup', 'start', 'stop', 'run']; |
| 129 | +ServiceManager.knownServices = [ |
| 130 | + // TODO: we only know about the nginx & built in process manager services |
| 131 | + // for now, in the future make this load globally installed services |
| 132 | + require('./nginx'), |
| 133 | + require('./process/systemd'), |
| 134 | + localService |
| 135 | +]; |
| 136 | + |
| 137 | +module.exports = ServiceManager; |
0 commit comments