Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
/*
2
* service.js: Object responsible for managing the foreverd daemon.
3
*
4
* (C) 2010 Nodejitsu Inc.
5
* MIT LICENCE
6
*
7
*/
8
9
var fs = require('fs'),
10
path = require('path'),
11
util = require('util'),
12
events = require('events'),
13
forever = require('../../forever'),
14
SystemVAdapter = require('./adapters/systemv');
15
16
// options
17
// directories {log, pid, conf, run, local}
18
var Service = module.exports = function Service(options) {
19
events.EventEmitter.call(this);
20
options = options || {};
21
22
var self = this,
23
AdapterType;
24
25
this.applications = [
26
//{
27
//file:
28
//options:
29
//monitor:
30
//}
31
];
32
33
this.servers = [];
34
if (typeof options.adapter === 'string') {
35
options.adapter = Service.adapter[options.adapter];
36
}
37
38
AdapterType = options.adapter || SystemVAdapter;
39
this.adapter = new AdapterType(this);
40
};
41
42
util.inherits(Service, events.EventEmitter);
43
44
Service.prototype.startServer = function startServer(callback) {
45
var socket = path.join(forever.config.get('sockPath'), 'forever.sock'),
46
monitors = [],
47
self = this,
48
server;
49
50
server = dnode(this);
51
server.on('error', function onServerError() {
52
//
53
// TODO: This is really bad.
54
//
55
});
56
57
server.on('ready', function onServerReady(err) {
58
self.listen(server);
59
if (callback) {
60
if (err) {
61
callback(err);
62
}
63
else {
64
callback(null, server, socket);
65
}
66
}
67
});
68
69
server.listen(socket);
70
71
return this;
72
};
73
74
Service.prototype.listen = function listen(server) {
75
var dnodeServer = dnode(this);
76
77
this.servers.push(dnodeServer);
78
dnodeServer.listen(server);
79
80
return this;
81
};
82
83
Service.prototype.load = function load() {
84
var self = this;
85
this.adapter.load(function onLoaded(applications) {
86
applications.forEach(function startApplication(application, index) {
87
var monitor = application.monitor = new forever.Monitor(application.file, application.options);
88
89
monitor.start();
90
self.applications.push(application);
91
92
if (index === applications.length - 1) {
93
self.listen(path.join(forever.config.get('root'), 'foreverd.sock'));
94
}
95
96
self.emit('foreverd::loaded');
97
});
98
});
99
return this;
100
};
101
102
//
103
// Function add(file, options)
104
// add the application to the service manager
105
// DOES NOT START THE APPLICATION
106
// call's the service manager's add method
107
//
108
Service.prototype.add = function add(file, options, callback) {
109
if (this.paused) {
110
return callback && callback(new Error('foreverd is paused'));
111
}
112
113
this.adapter.add(file, options, callback);
114
};
115
116
//
117
// Function remove(file, options)
118
// remove the application from the service manager
119
// call's the service manager's remove method
120
//
121
Service.prototype.remove = function remove(file, options, callback) {
122
if (this.paused) {
123
return callback(new Error('foreverd is paused'));
124
}
125
126
var applicationsToRemove = this.applications,
127
self = this,
128
optionStr,
129
fileStr;
130
131
if (file) {
132
fileStr = JSON.stringify(file);
133
applicationsToRemove = applicationsToRemove.filter(function compareFile(application) {
134
return fileStr !== JSON.stringify(application.file);
135
});
136
}
137
138
if (options) {
139
optionStr = JSON.stringify(options);
140
applicationsToRemove = applicationsToRemove.filter(function compareOptions(application) {
141
return optionStr !== JSON.stringify(application.options);
142
});
143
}
144
145
applicationsToRemove.forEach(function removeApplication(application) {
146
if (application.monitor) {
147
application.monitor.stop();
148
}
149
150
self.applications.splice(self.applications.indexOf(application), 1);
151
});
152
153
if (callback) {
154
callback();
155
}
156
157
return this;
158
};
159
160
//
161
// Function install()
162
// installs all the required to run foreverd
163
// call's the service manager's install(options)
164
//
165
Service.prototype.install = function install(callback) {
166
this.adapter.install(callback);
167
return this;
168
};
169
170
//
171
// Function uninstall(options)
172
// uninstalls all the required to run foreverd
173
// call's the service manager's uninstall(options)
174
//
175
Service.prototype.uninstall = function uninstall(callback) {
176
this.adapter.uninstall(callback);
177
return this;
178
};
179
180
//
181
// Function start()
182
// calls the appropriate OS functionality to start this service
183
//
184
Service.prototype.start = function start(callback) {
185
this.adapter.start(callback);
186
return this;
187
};
188
189
//
190
// Function run()
191
// creates monitors for all the services
192
//
193
Service.prototype.run = function run(callback) {
194
var self = this;
195
this.adapter.run(function adapterStarted() {
196
self.applications.forEach(function startApplication(application) {
197
application.monitor = new forever.Monitor(application.file, application.options);
198
application.monitor.start();
199
});
200
201
return callback && callback();
202
});
203
204
return this;
205
};
206
207
//
208
// Function stop(monitors)
209
//
210
Service.prototype.stop = function stop(callback) {
211
var self = this;
212
this.adapter.start(function adapterStopped() {
213
self.applications.forEach(function stopApplication(application) {
214
application.monitor.stop();
215
});
216
217
return callback && callback();
218
});
219
220
return this;
221
};
222
223
//
224
// Function restart()
225
//
226
Service.prototype.restart = function restart(callback) {
227
var self = this;
228
this.adapter.start(function adapterRestarted() {
229
self.applications.forEach(function restartApplication(application) {
230
application.monitor.restart();
231
});
232
233
return callback && callback();
234
});
235
236
return this;
237
};
238
239
//
240
// Function pause()
241
// disables adding / removing applications
242
//
243
Service.prototype.pause = function pause(callback) {
244
this.paused = true;
245
if (callback) {
246
callback();
247
}
248
249
return this;
250
};
251
252
//
253
// Function resume()
254
// reenables adding / removing applications
255
//
256
Service.prototype.resume = function resume(callback) {
257
this.paused = false;
258
if (callback) {
259
callback();
260
}
261
262
return this;
263
};
264
265
Service.prototype.list = function list(callback) {
266
this.adapter.list(callback);
267
return this;
268
};
269