Skip to content

Commit

Permalink
[refactor test dist] Refactor /lib/foreverd/ into /lib/forever/service/
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Oct 9, 2011
1 parent 36e0b9b commit 89be252
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/count-timer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* count-timer.js: Counts forever on a timer
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/error-on-timer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* error-on-timer.js: Sample script that errors on a timer
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* spawn-and-error.js: Sample script that spawns a simple child process and errors
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
7 changes: 7 additions & 0 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ forever.cli = require('./forever/cli');
//
require('pkginfo')(module, 'version');

//
// Expose the global forever service
//
forever.__defineGetter__('service', function () {
return require('./forever/service');
});

//
// ### function getSockets (sockPath, callback)
// #### @sockPath {string} Path in which to look for UNIX domain sockets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ var Adapter = module.exports = function Adapter(service) {
// This should install assets to appropriate places for initialization,
// configuration, and storage
//
// The script will be used on startup to load ForeverService
// The script will be used on startup to load Service
//
// ForeverService should listen on something that the management events
// Service should listen on something that the management events
// can respond to in full duplex
//
// The installed adapter should send the following events in dnode protocol
// to the ForeverService and invoke methods as appropriate
// to the Service and invoke methods as appropriate
//
Adapter.prototype.install = function install() {
throw new Error('not implemented');
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var fs = require('fs'),
spawn = require('child_process').spawn,
daemon = require('daemon'),
dnode = require('dnode'),
forever = require('../../../forever'),
Adapter = require('../../adapter');
forever = require('../../../../forever'),
Adapter = require('../adapter');

//
// Classic init.d script adapter
Expand Down
5 changes: 3 additions & 2 deletions lib/foreverd/cli.js → lib/forever/service/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var optimist = require('optimist'),
forever = require('../forever'),
ForeverService = require('./service'),
Service = require('./service'),
argv;

var mappings = {
Expand Down Expand Up @@ -31,7 +31,7 @@ function processArgs(cmd) {
var router = module.exports = function router(app) {
app.use(function (cmd, tty, next) {
cmd.flags._.shift();
cmd.service = new ForeverService({
cmd.service = new Service({
adapter: cmd.flags.adapter
});

Expand Down Expand Up @@ -194,6 +194,7 @@ var router = module.exports = function router(app) {
app.cli('/pause', function (cmd, tty) {
cmd.service.pause();
});

app.cli('/resume', function (cmd, tty) {
cmd.service.resume();
});
Expand Down
15 changes: 15 additions & 0 deletions lib/forever/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

var fs = require('fs'),
path = require('path');

var service = exports;

service.Service = require('./service');
service.adapters = {};

fs.readdirSync(path.join(__dirname, 'adapters')).forEach(function (file) {
file = file.replace(/\.js/, '');
service.adapters.__defineGetter__(file, function () {
return require(__dirname + '/adapters/' + file);
})
});
45 changes: 20 additions & 25 deletions lib/foreverd/service.js → lib/forever/service/service.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
var fs = require('fs'),
path = require('path'),
util = require('util'),
events = require('events'),
dnode = require('dnode'),
EventEmitter2 = require('eventemitter2').EventEmitter2,
portfinder = require('portfinder'),
forever = require('../forever'),
SystemVAdapter = require('./adapter/systemv');

forever = require('../../forever'),
SystemVAdapter = require('./adapters/systemv');

// options
// directories {log, pid, conf, run, local}
var ForeverService = module.exports = function ForeverService(options) {
var Service = module.exports = function Service(options) {
EventEmitter2.call(this);
options = options || {};

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

this.servers = [];
if (typeof options.adapter === 'string') {
options.adapter = ForeverService.adapter[options.adapter];
options.adapter = Service.adapter[options.adapter];
}

AdapterType = options.adapter || SystemVAdapter;
this.adapter = new AdapterType(this);
console.log(this.adapter);
};

util.inherits(ForeverService, EventEmitter2);

fs.readdirSync(path.join(__dirname, 'adapter')).forEach(function loadAdapter(adapterModule) {
ForeverService[adapterModule] = require(path.join(__dirname, 'adapter', adapterModule));
});
util.inherits(Service, events.EventEmitter);

ForeverService.prototype.startServer = function startServer(callback) {
Service.prototype.startServer = function startServer(callback) {
var socket = path.join(forever.config.get('sockPath'), 'forever.sock'),
monitors = [],
self = this,
Expand Down Expand Up @@ -80,7 +75,7 @@ ForeverService.prototype.startServer = function startServer(callback) {
return this;
};

ForeverService.prototype.listen = function listen(server) {
Service.prototype.listen = function listen(server) {
var dnodeServer = dnode(this);

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

ForeverService.prototype.load = function load() {
Service.prototype.load = function load() {
var self = this;
this.adapter.load(function onLoaded(applications) {
console.error(arguments);
Expand All @@ -119,7 +114,7 @@ ForeverService.prototype.load = function load() {
// DOES NOT START THE APPLICATION
// call's the service manager's add method
//
ForeverService.prototype.add = function add(file, options, callback) {
Service.prototype.add = function add(file, options, callback) {
console.log(arguments);
if (this.paused) {
return callback && callback(new Error('foreverd is paused'));
Expand All @@ -133,7 +128,7 @@ ForeverService.prototype.add = function add(file, options, callback) {
// remove the application from the service manager
// call's the service manager's remove method
//
ForeverService.prototype.remove = function remove(file, options, callback) {
Service.prototype.remove = function remove(file, options, callback) {
if (this.paused) {
return callback(new Error('foreverd is paused'));
}
Expand Down Expand Up @@ -177,7 +172,7 @@ ForeverService.prototype.remove = function remove(file, options, callback) {
// installs all the required to run foreverd
// call's the service manager's install(options)
//
ForeverService.prototype.install = function install(callback) {
Service.prototype.install = function install(callback) {
this.adapter.install(callback);
return this;
};
Expand All @@ -187,7 +182,7 @@ ForeverService.prototype.install = function install(callback) {
// uninstalls all the required to run foreverd
// call's the service manager's uninstall(options)
//
ForeverService.prototype.uninstall = function uninstall(callback) {
Service.prototype.uninstall = function uninstall(callback) {
this.adapter.uninstall(callback);
return this;
};
Expand All @@ -196,7 +191,7 @@ ForeverService.prototype.uninstall = function uninstall(callback) {
// Function start()
// calls the appropriate OS functionality to start this service
//
ForeverService.prototype.start = function start(callback) {
Service.prototype.start = function start(callback) {
this.adapter.start(callback);
return this;
};
Expand All @@ -205,7 +200,7 @@ ForeverService.prototype.start = function start(callback) {
// Function run()
// creates monitors for all the services
//
ForeverService.prototype.run = function run(callback) {
Service.prototype.run = function run(callback) {
var self = this;
this.adapter.run(function adapterStarted() {
console.error(self.applications);
Expand All @@ -224,7 +219,7 @@ ForeverService.prototype.run = function run(callback) {
//
// Function stop(monitors)
//
ForeverService.prototype.stop = function stop(callback) {
Service.prototype.stop = function stop(callback) {
var self = this;
this.adapter.start(function adapterStopped() {
self.applications.forEach(function stopApplication(application) {
Expand All @@ -240,7 +235,7 @@ ForeverService.prototype.stop = function stop(callback) {
//
// Function restart()
//
ForeverService.prototype.restart = function restart(callback) {
Service.prototype.restart = function restart(callback) {
var self = this;
this.adapter.start(function adapterRestarted() {
self.applications.forEach(function restartApplication(application) {
Expand All @@ -257,7 +252,7 @@ ForeverService.prototype.restart = function restart(callback) {
// Function pause()
// disables adding / removing applications
//
ForeverService.prototype.pause = function pause(callback) {
Service.prototype.pause = function pause(callback) {
this.paused = true;
if (callback) {
callback();
Expand All @@ -270,7 +265,7 @@ ForeverService.prototype.pause = function pause(callback) {
// Function resume()
// reenables adding / removing applications
//
ForeverService.prototype.resume = function resume(callback) {
Service.prototype.resume = function resume(callback) {
this.paused = false;
if (callback) {
callback();
Expand All @@ -279,7 +274,7 @@ ForeverService.prototype.resume = function resume(callback) {
return this;
};

ForeverService.prototype.list = function list(callback) {
Service.prototype.list = function list(callback) {
this.adapter.list(callback);
return this;
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"dnode": "0.8.x",
"eyes": "0.1.x",
"daemon": "0.3.x",
"eventemitter2": "0.4.x",
"mkdirp": "0.x.x",
"nconf": "0.x.x",
"optimist": "0.2.x",
Expand Down
2 changes: 1 addition & 1 deletion test/env-spawn-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* env-spawn-test.js: Tests for supporting environment variables in the forever module
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* test-hook.js: Test hook fixture for raising an event on forever.Monitor `exit`
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion test/forever-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* forever-test.js: Tests for forever module
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* helpers.js: Test helpers for the forever module
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion test/hook-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* hook-test.js: Tests for forever-based hooks
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
2 changes: 1 addition & 1 deletion test/multiple-processes-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* forever-test.js: Tests for forever module
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down
25 changes: 25 additions & 0 deletions test/service-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* forever-test.js: Tests for forever module
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/

var assert = require('assert'),
path = require('path'),
vows = require('vows'),
forever = require('../lib/forever');

vows.describe('forever/service').addBatch({
"When using forever": {
"the service module": {
"should have the correct exports": function () {
assert.isObject(forever.service);
assert.isFunction(forever.service.Service);
assert.isObject(forever.service.adapters);
assert.isFunction(forever.service.adapters.initd)
}
}
}
}).export(module);
4 changes: 2 additions & 2 deletions test/spin-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* forever-test.js: Tests for forever module
* spin-test.js: Tests for spin restarts in forever.
*
* (C) 2010 and Charlie Robbins
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
Expand Down

0 comments on commit 89be252

Please sign in to comment.