-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupervisor.js
106 lines (86 loc) · 2.4 KB
/
supervisor.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
const fs = require('fs');
const {errors, ProcessManager} = require('ghost-cli');
const execa = require('execa');
const getUid = require('./get-uid');
const debug = require('debug')('ghost-cli-supervisor:process-manager');
const {CliError, ProcessError, SystemError} = errors;
function wrapError(error) {
if (error instanceof CliError) {
throw error;
}
throw new ProcessError(error);
}
class SupervisorProcessManager extends ProcessManager {
get programName() {
debug(this.instance.name);
return `${this.instance.name}`;
}
async updateSocket() {
const portfinder = require('portfinder');
const port = await portfinder.getPortPromise();
const socketAddress = {
host: 'localhost',
port
};
this.instance.config.set('bootstrap-socket', socketAddress);
this.instance.config.save();
return socketAddress;
}
async start() {
this._precheck();
try {
const socketAddress = await this.updateSocket();
await this.ui.sudo(`supervisorctl start ${this.programName}`);
await this.ensureStarted({socketAddress});
} catch (error) {
wrapError(error);
}
}
async stop() {
this._precheck();
try {
await this.ui.sudo(`supervisorctl stop ${this.programName}`);
} catch (error) {
wrapError(error);
}
}
async restart() {
this._precheck();
try {
const socketAddress = await this.updateSocket();
await this.ui.sudo(`supervisorctl restart ${this.programName}`);
await this.ensureStarted({socketAddress});
} catch (error) {
wrapError(error);
}
}
async isRunning() {
try {
const response = await this.ui.sudo(`supervisorctl status ${this.programName}`);
debug(response.stdout);
return Boolean(response.stdout.match(/RUNNING|STARTING/));
} catch (error) {
// @todo see if any other errors should return true
return !error.message.match(/start/);
}
}
static willRun() {
try {
execa.shellSync('which supervisorctl', {stdio: 'ignore'});
return true;
} catch {
return false;
}
}
_precheck() {
const uid = getUid(this.instance.dir);
if (!uid) {
throw new SystemError('Supervisor process manager has not been set up. Run `ghost setup linux-user supervisor` and try again.');
}
if (fs.existsSync(`/etc/supervisor/conf.d/${this.programName}.conf`)) {
return;
}
throw new SystemError('Supervisor process manager has not been set up. Run `ghost setup supervisor` and try again.');
}
}
module.exports = SupervisorProcessManager;