-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
process-manager.js
124 lines (105 loc) · 2.96 KB
/
process-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const every = require('lodash/every');
const requiredMethods = [
'start',
'stop',
'isRunning'
];
const enableMethods = [
'isEnabled',
'enable',
'disable'
];
class ProcessManager {
/**
* Constructs the process manager. Process Managers get access to the UI and System instances
*
* @param {UI} ui UI instance
* @param {System} system System
* @param {Instance} instance Ghost instance
*/
constructor(ui, system, instance) {
this.ui = ui;
this.system = system;
this.instance = instance;
}
/**
* Method called to start the Ghost process
*
* @param {String} cwd Current working directory of Ghost instance
* @param {String} environment Environment to start Ghost in
* @return Promise<void>|null
*/
start() {
// Base implementation - noop
return Promise.resolve();
}
stop() {
// Base implementation - noop
return Promise.resolve();
}
restart(cwd, env) {
return this.stop(cwd, env).then(() => this.start(cwd, env));
}
success() {
// Base implementation - noop
}
error(error) {
// Base implementation - re-throw the error in case the
// extension has no error method defined
throw error;
}
isRunning() {
// Base Implementation
}
/**
* General implementation of figuring out if the Ghost blog has started successfully.
*
* @returns {Promise<any>}
*/
ensureStarted(options) {
const portPolling = require('./utils/port-polling');
options = Object.assign({
stopOnError: true,
port: this.instance.config.get('server.port')
}, options || {});
return portPolling(options)
.catch((err) => {
if (options.stopOnError) {
return this.stop()
.then(() => {
throw err;
})
.catch(() => {
throw err;
});
}
throw err;
});
}
/**
* This function checks if this process manager can be used on this system
*
* @return {Boolean} whether or not the process manager can be used
*/
static willRun() {
// Base implementation - return true
return true;
}
}
function isValid(SubClass) {
if (!(SubClass.prototype instanceof ProcessManager)) {
return false;
}
const missing = requiredMethods.filter((method) => !SubClass.prototype.hasOwnProperty(method));
if (!missing.length) {
return true;
}
return missing;
}
function supportsEnableBehavior(processInstance) {
return every(enableMethods, (method) => processInstance[method]);
}
module.exports = ProcessManager
module.exports.isValid = isValid;
module.exports.supportsEnableBehavior = supportsEnableBehavior;