Skip to content

Commit 89be252

Browse files
committed
[refactor test dist] Refactor /lib/foreverd/ into /lib/forever/service/
1 parent 36e0b9b commit 89be252

20 files changed

+86
-44
lines changed

examples/count-timer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* count-timer.js: Counts forever on a timer
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

examples/error-on-timer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* error-on-timer.js: Sample script that errors on a timer
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

examples/spawn-and-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* spawn-and-error.js: Sample script that spawns a simple child process and errors
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

lib/forever.js

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ forever.cli = require('./forever/cli');
4949
//
5050
require('pkginfo')(module, 'version');
5151

52+
//
53+
// Expose the global forever service
54+
//
55+
forever.__defineGetter__('service', function () {
56+
return require('./forever/service');
57+
});
58+
5259
//
5360
// ### function getSockets (sockPath, callback)
5461
// #### @sockPath {string} Path in which to look for UNIX domain sockets

lib/foreverd/adapter.js renamed to lib/forever/service/adapters/adapter.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ var Adapter = module.exports = function Adapter(service) {
77
// This should install assets to appropriate places for initialization,
88
// configuration, and storage
99
//
10-
// The script will be used on startup to load ForeverService
10+
// The script will be used on startup to load Service
1111
//
12-
// ForeverService should listen on something that the management events
12+
// Service should listen on something that the management events
1313
// can respond to in full duplex
1414
//
1515
// The installed adapter should send the following events in dnode protocol
16-
// to the ForeverService and invoke methods as appropriate
16+
// to the Service and invoke methods as appropriate
1717
//
1818
Adapter.prototype.install = function install() {
1919
throw new Error('not implemented');

lib/foreverd/adapter/systemv/index.js renamed to lib/forever/service/adapters/systemv/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ var fs = require('fs'),
44
spawn = require('child_process').spawn,
55
daemon = require('daemon'),
66
dnode = require('dnode'),
7-
forever = require('../../../forever'),
8-
Adapter = require('../../adapter');
7+
forever = require('../../../../forever'),
8+
Adapter = require('../adapter');
99

1010
//
1111
// Classic init.d script adapter

lib/foreverd/cli.js renamed to lib/forever/service/cli.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var optimist = require('optimist'),
22
forever = require('../forever'),
3-
ForeverService = require('./service'),
3+
Service = require('./service'),
44
argv;
55

66
var mappings = {
@@ -31,7 +31,7 @@ function processArgs(cmd) {
3131
var router = module.exports = function router(app) {
3232
app.use(function (cmd, tty, next) {
3333
cmd.flags._.shift();
34-
cmd.service = new ForeverService({
34+
cmd.service = new Service({
3535
adapter: cmd.flags.adapter
3636
});
3737

@@ -194,6 +194,7 @@ var router = module.exports = function router(app) {
194194
app.cli('/pause', function (cmd, tty) {
195195
cmd.service.pause();
196196
});
197+
197198
app.cli('/resume', function (cmd, tty) {
198199
cmd.service.resume();
199200
});

lib/forever/service/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
var fs = require('fs'),
3+
path = require('path');
4+
5+
var service = exports;
6+
7+
service.Service = require('./service');
8+
service.adapters = {};
9+
10+
fs.readdirSync(path.join(__dirname, 'adapters')).forEach(function (file) {
11+
file = file.replace(/\.js/, '');
12+
service.adapters.__defineGetter__(file, function () {
13+
return require(__dirname + '/adapters/' + file);
14+
})
15+
});

lib/foreverd/service.js renamed to lib/forever/service/service.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
var fs = require('fs'),
22
path = require('path'),
33
util = require('util'),
4+
events = require('events'),
45
dnode = require('dnode'),
5-
EventEmitter2 = require('eventemitter2').EventEmitter2,
66
portfinder = require('portfinder'),
7-
forever = require('../forever'),
8-
SystemVAdapter = require('./adapter/systemv');
9-
7+
forever = require('../../forever'),
8+
SystemVAdapter = require('./adapters/systemv');
109

1110
// options
1211
// directories {log, pid, conf, run, local}
13-
var ForeverService = module.exports = function ForeverService(options) {
12+
var Service = module.exports = function Service(options) {
1413
EventEmitter2.call(this);
1514
options = options || {};
1615

@@ -27,21 +26,17 @@ var ForeverService = module.exports = function ForeverService(options) {
2726

2827
this.servers = [];
2928
if (typeof options.adapter === 'string') {
30-
options.adapter = ForeverService.adapter[options.adapter];
29+
options.adapter = Service.adapter[options.adapter];
3130
}
3231

3332
AdapterType = options.adapter || SystemVAdapter;
3433
this.adapter = new AdapterType(this);
3534
console.log(this.adapter);
3635
};
3736

38-
util.inherits(ForeverService, EventEmitter2);
39-
40-
fs.readdirSync(path.join(__dirname, 'adapter')).forEach(function loadAdapter(adapterModule) {
41-
ForeverService[adapterModule] = require(path.join(__dirname, 'adapter', adapterModule));
42-
});
37+
util.inherits(Service, events.EventEmitter);
4338

44-
ForeverService.prototype.startServer = function startServer(callback) {
39+
Service.prototype.startServer = function startServer(callback) {
4540
var socket = path.join(forever.config.get('sockPath'), 'forever.sock'),
4641
monitors = [],
4742
self = this,
@@ -80,7 +75,7 @@ ForeverService.prototype.startServer = function startServer(callback) {
8075
return this;
8176
};
8277

83-
ForeverService.prototype.listen = function listen(server) {
78+
Service.prototype.listen = function listen(server) {
8479
var dnodeServer = dnode(this);
8580

8681
this.servers.push(dnodeServer);
@@ -93,7 +88,7 @@ ForeverService.prototype.listen = function listen(server) {
9388
return this;
9489
};
9590

96-
ForeverService.prototype.load = function load() {
91+
Service.prototype.load = function load() {
9792
var self = this;
9893
this.adapter.load(function onLoaded(applications) {
9994
console.error(arguments);
@@ -119,7 +114,7 @@ ForeverService.prototype.load = function load() {
119114
// DOES NOT START THE APPLICATION
120115
// call's the service manager's add method
121116
//
122-
ForeverService.prototype.add = function add(file, options, callback) {
117+
Service.prototype.add = function add(file, options, callback) {
123118
console.log(arguments);
124119
if (this.paused) {
125120
return callback && callback(new Error('foreverd is paused'));
@@ -133,7 +128,7 @@ ForeverService.prototype.add = function add(file, options, callback) {
133128
// remove the application from the service manager
134129
// call's the service manager's remove method
135130
//
136-
ForeverService.prototype.remove = function remove(file, options, callback) {
131+
Service.prototype.remove = function remove(file, options, callback) {
137132
if (this.paused) {
138133
return callback(new Error('foreverd is paused'));
139134
}
@@ -177,7 +172,7 @@ ForeverService.prototype.remove = function remove(file, options, callback) {
177172
// installs all the required to run foreverd
178173
// call's the service manager's install(options)
179174
//
180-
ForeverService.prototype.install = function install(callback) {
175+
Service.prototype.install = function install(callback) {
181176
this.adapter.install(callback);
182177
return this;
183178
};
@@ -187,7 +182,7 @@ ForeverService.prototype.install = function install(callback) {
187182
// uninstalls all the required to run foreverd
188183
// call's the service manager's uninstall(options)
189184
//
190-
ForeverService.prototype.uninstall = function uninstall(callback) {
185+
Service.prototype.uninstall = function uninstall(callback) {
191186
this.adapter.uninstall(callback);
192187
return this;
193188
};
@@ -196,7 +191,7 @@ ForeverService.prototype.uninstall = function uninstall(callback) {
196191
// Function start()
197192
// calls the appropriate OS functionality to start this service
198193
//
199-
ForeverService.prototype.start = function start(callback) {
194+
Service.prototype.start = function start(callback) {
200195
this.adapter.start(callback);
201196
return this;
202197
};
@@ -205,7 +200,7 @@ ForeverService.prototype.start = function start(callback) {
205200
// Function run()
206201
// creates monitors for all the services
207202
//
208-
ForeverService.prototype.run = function run(callback) {
203+
Service.prototype.run = function run(callback) {
209204
var self = this;
210205
this.adapter.run(function adapterStarted() {
211206
console.error(self.applications);
@@ -224,7 +219,7 @@ ForeverService.prototype.run = function run(callback) {
224219
//
225220
// Function stop(monitors)
226221
//
227-
ForeverService.prototype.stop = function stop(callback) {
222+
Service.prototype.stop = function stop(callback) {
228223
var self = this;
229224
this.adapter.start(function adapterStopped() {
230225
self.applications.forEach(function stopApplication(application) {
@@ -240,7 +235,7 @@ ForeverService.prototype.stop = function stop(callback) {
240235
//
241236
// Function restart()
242237
//
243-
ForeverService.prototype.restart = function restart(callback) {
238+
Service.prototype.restart = function restart(callback) {
244239
var self = this;
245240
this.adapter.start(function adapterRestarted() {
246241
self.applications.forEach(function restartApplication(application) {
@@ -257,7 +252,7 @@ ForeverService.prototype.restart = function restart(callback) {
257252
// Function pause()
258253
// disables adding / removing applications
259254
//
260-
ForeverService.prototype.pause = function pause(callback) {
255+
Service.prototype.pause = function pause(callback) {
261256
this.paused = true;
262257
if (callback) {
263258
callback();
@@ -270,7 +265,7 @@ ForeverService.prototype.pause = function pause(callback) {
270265
// Function resume()
271266
// reenables adding / removing applications
272267
//
273-
ForeverService.prototype.resume = function resume(callback) {
268+
Service.prototype.resume = function resume(callback) {
274269
this.paused = false;
275270
if (callback) {
276271
callback();
@@ -279,7 +274,7 @@ ForeverService.prototype.resume = function resume(callback) {
279274
return this;
280275
};
281276

282-
ForeverService.prototype.list = function list(callback) {
277+
Service.prototype.list = function list(callback) {
283278
this.adapter.list(callback);
284279
return this;
285280
};

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"dnode": "0.8.x",
3333
"eyes": "0.1.x",
3434
"daemon": "0.3.x",
35-
"eventemitter2": "0.4.x",
3635
"mkdirp": "0.x.x",
3736
"nconf": "0.x.x",
3837
"optimist": "0.2.x",

test/env-spawn-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* env-spawn-test.js: Tests for supporting environment variables in the forever module
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/fixtures/test-hook.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* test-hook.js: Test hook fixture for raising an event on forever.Monitor `exit`
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/forever-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* forever-test.js: Tests for forever module
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* helpers.js: Test helpers for the forever module
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/hook-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* hook-test.js: Tests for forever-based hooks
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/multiple-processes-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* forever-test.js: Tests for forever module
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

test/service-test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* forever-test.js: Tests for forever module
3+
*
4+
* (C) 2010 Charlie Robbins
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var assert = require('assert'),
10+
path = require('path'),
11+
vows = require('vows'),
12+
forever = require('../lib/forever');
13+
14+
vows.describe('forever/service').addBatch({
15+
"When using forever": {
16+
"the service module": {
17+
"should have the correct exports": function () {
18+
assert.isObject(forever.service);
19+
assert.isFunction(forever.service.Service);
20+
assert.isObject(forever.service.adapters);
21+
assert.isFunction(forever.service.adapters.initd)
22+
}
23+
}
24+
}
25+
}).export(module);

test/spin-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* forever-test.js: Tests for forever module
2+
* spin-test.js: Tests for spin restarts in forever.
33
*
4-
* (C) 2010 and Charlie Robbins
4+
* (C) 2010 Charlie Robbins
55
* MIT LICENCE
66
*
77
*/

0 commit comments

Comments
 (0)