/
index.js
197 lines (168 loc) · 4.99 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* index.js: Top-level include for the systemv foreverd service adapter
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
var fs = require('fs'),
util = require('util'),
path = require('path'),
spawn = require('child_process').spawn,
daemon = require('daemon'),
dnode = require('dnode'),
forever = require('../../../../forever'),
Adapter = require('../adapter');
//
// Classic init.d script adapter
// Sometimes called inittab, but its origin is called systemv
//
var SystemVAdapter = module.exports = function SystemVAdapter(service) {
Adapter.call(this, service);
this.daemonized = false;
};
util.inherits(SystemVAdapter, Adapter);
SystemVAdapter.prototype.install = function install(callback) {
//
// Copy the init.d script to the right location
// TODO Distribution fixes?
//
forever.config.set('root', path.join('/var', 'local', 'foreverd'));
var initdPath = path.join('/etc', 'init.d', 'foreverd'),
script,
target;
try {
fs.mkdirSync(forever.config.get('root'), '0777');
fs.mkdirSync(path.join(forever.config.get('root'), 'services'), '0777');
}
catch (e) {
if (e.code !== 'EEXIST') {
return callback && callback(e);
}
}
try {
script = fs.createReadStream(path.join(__dirname, 'foreverd'));
target = fs.createWriteStream(initdPath, { flags: 'w', mode: '0777' });
script.pipe(target);
script.on('end', function () {
var directories = fs.readdirSync('/etc');
directories.forEach(function (directory) {
var match = directory.match(/^rc(\d+)\.d$/),
killOrStart;
if (match) {
killOrStart = { 0: true, 1: true, 6: true }[match[1]] ? 'K' : 'S';
try {
fs.symlinkSync(initdPath, path.join('/etc', directory, killOrStart + '20foreverd'));
}
catch (e) {
if (e.code !== 'EEXIST') {
return callback && callback(e);
}
}
}
});
return callback && callback();
});
}
catch (e) {
if (e.code !== 'EEXIST') {
return callback && callback(e);
}
}
};
//
//
//
SystemVAdapter.prototype.load = function load(callback) {
forever.config.set('root', path.join('/var', 'local', 'foreverd'));
var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')),
services = [];
if (serviceFiles.length !== 0) {
serviceFiles.forEach(function loadServiceFiles(serviceFile, index) {
var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile),
service = JSON.parse(fs.readFileSync(serviceFilePath)),
file = service.file,
options = service.options;
options.minUptime = 200;
services.push({
file: service.file,
options: service.options
});
});
}
callback(services);
};
SystemVAdapter.prototype.start = function start(callback) {
spawn('/etc/init.d/foreverd', ['start']);
return callback && callback();
};
SystemVAdapter.prototype.run = function run(callback) {
if (this.daemonized) {
return callback();
}
var self = this,
pidFilePath = path.join('/var', 'run', 'foreverd.pid'),
logFilePath = path.join('/var', 'log', 'foreverd');
process.on('exit', function removePIDFile() {
try {
fs.unlinkSync(pidFilePath);
}
catch (err) {
// we are exiting anyway. this may have some noexist error already
}
});
fs.open(logFilePath, 'w+', function serviceLogOpened(err, logFile) {
if (err) {
throw err;
}
self.service.startServer(function () {
try {
daemon.start(logFile);
daemon.lock(pidFilePath);
self.daemonized = true;
return callback && callback();
}
catch (err) {
console.error(err);
return callback && callback(err);
}
});
});
};
SystemVAdapter.prototype.add = function add(file, options, callback) {
forever.config.set('root', path.join('/var', 'local', 'foreverd'));
//
// Add descriptor to our service list
// this is just a json file in $root/services/*.json
//
var filePath, service = {
file: file,
options: options || {}
};
options.appendLog = true;
filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');
fs.writeFile(filePath, JSON.stringify(service), function (err) {
return callback && callback(err);
});
};
SystemVAdapter.prototype.list = function list(callback) {
forever.config.set('root', path.join('/var', 'local', 'foreverd'));
var sockPath = path.join(forever.config.get('root'), 'foreverd.sock'),
client;
client = dnode.connect(sockPath, function onConnect(remote, client) {
if (callback) {
callback(false, remote.applications);
}
client.end();
});
client.on('error', function onError(err) {
if (err.code === 'ECONNREFUSED') {
try {
fs.unlinkSync(fullPath);
}
catch (ex) { }
return callback && callback(false, []);
}
return callback && callback(err);
});
};