Skip to content

Commit

Permalink
[minor] Updated foreverd for JSHint
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Oct 9, 2011
1 parent 3525130 commit 36e0b9b
Show file tree
Hide file tree
Showing 4 changed files with 482 additions and 394 deletions.
70 changes: 39 additions & 31 deletions lib/foreverd/adapter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = ForeverServiceAdapter;

function ForeverServiceAdapter(service) {
this.service = service;
}
var Adapter = module.exports = function Adapter(service) {
this.service = service;
};

//
// This should install assets to appropriate places for initialization,
// configuration, and storage
Expand All @@ -15,67 +15,75 @@ function ForeverServiceAdapter(service) {
// The installed adapter should send the following events in dnode protocol
// to the ForeverService and invoke methods as appropriate
//
ForeverServiceAdapter.prototype.install = function install() {
throw new Error('not implemented');
}
Adapter.prototype.install = function install() {
throw new Error('not implemented');
};

//
// This should do a rollback of install completely except for logs
//
ForeverServiceAdapter.prototype.uninstall = function uninstall() {
throw new Error('not implemented');
}
Adapter.prototype.uninstall = function uninstall() {
throw new Error('not implemented');
};

//
// This should call back with an array of [{file:...,options:...},] to pass to Monitors
// this will be invoked when foreverd is created (not started)
//
ForeverServiceAdapter.prototype.load = function load(callback) {
throw new Error('not implemented');
}
Adapter.prototype.load = function load(callback) {
throw new Error('not implemented');
};

//
// This should tell the OS to start the service
// this will not start any applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.start = function start(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.start = function start(monitors) {
throw new Error('not implemented');
};

//
// This should tell the OS to start the service
// this will not stop any applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.stop = function stop(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.stop = function stop(monitors) {
throw new Error('not implemented');
};

//
// This should tell the OS to reply with info about applications in the service
// this will not change any applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.status = function status(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.status = function status(monitors) {
throw new Error('not implemented');
};

//
// This should tell the OS to restart the service
// this will not restart any applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.restart = function restart(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.restart = function restart(monitors) {
throw new Error('not implemented');
};

//
// This should tell the OS to pause the service
// this will prevent any addition or removal of applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.pause = function pause(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.pause = function pause(monitors) {
throw new Error('not implemented');
};

//
// This should tell the OS to resume the service
// this will enable any addition or removal of applications
// make sure the adapter is installed and sending events to foreverd's listener
//
ForeverServiceAdapter.prototype.resume = function resume(monitors) {
throw new Error('not implemented');
}
Adapter.prototype.resume = function resume(monitors) {
throw new Error('not implemented');
};
177 changes: 101 additions & 76 deletions lib/foreverd/adapter/systemv/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
var util = require('util');
var forever = require('../../../forever');
var ForeverServiceAdapter = require('../../adapter');
var path = require('path');
var fs = require('fs');
var dnode = require('dnode');

module.exports = SystemVAdapter;
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
//
function SystemVAdapter(service) {
ForeverServiceAdapter.call(this, service);
this.daemonized = false;
}
util.inherits(SystemVAdapter, ForeverServiceAdapter);
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');
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);
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 {
var script = fs.createReadStream(path.join(__dirname, 'foreverd'));
var target = fs.createWriteStream(initdPath, {
flags: 'w',
mode: 0777
});
script = fs.createReadStream(path.join(__dirname, 'foreverd'));
target = fs.createWriteStream(initdPath, { flags: 'w', mode: '0777' });

script.pipe(target);
script.on('end', function() {
script.on('end', function () {
var directories = fs.readdirSync('/etc');
directories.forEach(function (directory) {
var match = directory.match(/^rc(\d+)\.d$/);
if(match) {
var kill_or_start = {0:true, 1:true, 6:true}[match[1]] ? 'K' : 'S';
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,kill_or_start+'20foreverd'));
fs.symlinkSync(initdPath, path.join('/etc', directory, killOrStart + '20foreverd'));
}
catch (e) {
if (e.code !== 'EEXIST') {
Expand All @@ -56,6 +62,7 @@ SystemVAdapter.prototype.install = function install(callback) {
}
}
});

return callback && callback();
});
}
Expand All @@ -64,101 +71,119 @@ SystemVAdapter.prototype.install = function install(callback) {
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'));
var services = [];
if (serviceFiles.length !== 0) {
serviceFiles.forEach(function loadServiceFiles(serviceFile, index) {
var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
var service = JSON.parse(fs.readFileSync(serviceFilePath));
var file = service.file;
var options = service.options;
options.minUptime = 200;
services.push({
file:service.file,
options:service.options
})
});
}
callback(services);
}
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) {
require('child_process').spawn('/etc/init.d/foreverd', ['start']);
callback && callback();
}
spawn('/etc/init.d/foreverd', ['start']);
return callback && callback();
};

SystemVAdapter.prototype.run = function run(callback) {
if(this.daemonized) {
if (this.daemonized) {
return callback();
}
var self = this;
var pidFilePath = path.join('/var','run', 'foreverd.pid');
var logFilePath = path.join('/var','log','foreverd');

var self = this,
pidFilePath = path.join('/var', 'run', 'foreverd.pid'),
logFilePath = path.join('/var', 'log', 'foreverd');

process.on('exit', function removePIDFile() {
try{
try {
fs.unlinkSync(pidFilePath);
}
catch(err) {
//we are exiting anyway. this may have some noexist error already
catch (err) {
// we are exiting anyway. this may have some noexist error already
}
})
});

fs.open(logFilePath, 'w+', function serviceLogOpened(err, logFile) {
if(err) {
if (err) {
throw err;
}
self.service.startServer(function() {

self.service.startServer(function () {
try {
daemon.start(logFile);
daemon.lock(pidFilePath);
daemon.lock(pidFilePath);
self.daemonized = true;
callback && callback();
return callback && callback();
}
catch (err) {
console.error(err)
return callback && callback(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 service = {
var filePath, service = {
file: file,
options: options || {}
};

options.appendLog = true;
var filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');
fs.writeFile(filePath, JSON.stringify(service), function(err) {
console.error('/',arguments)
callback && callback();
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');
var client = dnode.connect(sockPath, function onConnect(remote, client) {
callback && callback(false, remote.applications);

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) }
try {
fs.unlinkSync(fullPath);
}
catch (ex) { }
return callback && callback(false, []);
}
callback && callback(err);
})
}

return callback && callback(err);
});
};
Loading

0 comments on commit 36e0b9b

Please sign in to comment.