From 36e0b9bb0efda9f13dba2f67242c19731a45169f Mon Sep 17 00:00:00 2001 From: indexzero Date: Thu, 6 Oct 2011 00:38:41 -0400 Subject: [PATCH] [minor] Updated foreverd for JSHint --- lib/foreverd/adapter.js | 70 +++--- lib/foreverd/adapter/systemv/index.js | 177 ++++++++------ lib/foreverd/cli.js | 321 ++++++++++++++------------ lib/foreverd/service.js | 308 +++++++++++++----------- 4 files changed, 482 insertions(+), 394 deletions(-) diff --git a/lib/foreverd/adapter.js b/lib/foreverd/adapter.js index a3f219ff..758dc7a9 100644 --- a/lib/foreverd/adapter.js +++ b/lib/foreverd/adapter.js @@ -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 @@ -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'); -} \ No newline at end of file +Adapter.prototype.resume = function resume(monitors) { + throw new Error('not implemented'); +}; \ No newline at end of file diff --git a/lib/foreverd/adapter/systemv/index.js b/lib/foreverd/adapter/systemv/index.js index 484d63e3..32ec453a 100644 --- a/lib/foreverd/adapter/systemv/index.js +++ b/lib/foreverd/adapter/systemv/index.js @@ -1,21 +1,22 @@ -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) { // @@ -23,31 +24,36 @@ SystemVAdapter.prototype.install = function install(callback) { // 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') { @@ -56,6 +62,7 @@ SystemVAdapter.prototype.install = function install(callback) { } } }); + return callback && callback(); }); } @@ -64,69 +71,76 @@ 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')); @@ -134,31 +148,42 @@ SystemVAdapter.prototype.add = function add(file, options, callback) { // 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); - }) -} \ No newline at end of file + + return callback && callback(err); + }); +}; \ No newline at end of file diff --git a/lib/foreverd/cli.js b/lib/foreverd/cli.js index c8e87858..33f5fd6f 100644 --- a/lib/foreverd/cli.js +++ b/lib/foreverd/cli.js @@ -1,7 +1,7 @@ -var optimist = require('optimist'); -var forever = require('../forever'); -var ForeverService = require('./service'); -module.exports = router; +var optimist = require('optimist'), + forever = require('../forever'), + ForeverService = require('./service'), + argv; var mappings = { 'c': 'command', @@ -25,159 +25,176 @@ var mappings = { }; function processArgs(cmd) { - + } -function router(app) { - app.use(function(cmd,tty,next) { - cmd.flags._.shift(); - cmd.service = new ForeverService({ - adapter: cmd.flags.adapter - }); - cmd.argv = cmd.flags._; - var file = cmd.argv[0], options = {}; - cmd.file = file; - cmd.options = options; - if (file) { - // - // Setup pass-thru options for child-process - // - options.options = cmd.argv.splice(cmd.argv.indexOf(file)).splice(1); - } - else if (cmd.flags.c || cmd.flags.command) { - options.options = cmd.argv.splice(cmd.argv.indexOf(cmd.flags.c || cmd.flags.command) + 1); - } - // - // Now that we've removed the target script options - // reparse the options and configure the forever settings - // - argv = optimist(cmd.argv).boolean(['v', 'verbose', 'a', 'append', 's', 'silent']).argv; - Object.keys(argv).forEach(function (key) { - if (mappings[key] && argv[key]) { - options[mappings[key]] = argv[key]; - } - }); - - if (typeof options['max'] === 'undefined') { - // - // If max isn't specified set it to run forever - // - options.forever = true; - } - - if (typeof options['minUptime'] !== 'undefined') { - options['minUptime'] = parseFloat(options['minUptime']); - } - if (typeof options['spinSleepTime'] !== 'undefined') { - options['spinSleepTime'] = parseFloat(options['spinSleepTime']); - } - - if (!options.sourceDir) { - // - // Set the sourceDir of the options for graceful - // restarting outside of the main directory - // - options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/'; - } - - var uid = options.uid || forever.randomString(24); - options.uid = uid; - options.pidFile = options.pidFile || uid + '.pid'; - options.logFile = argv.l || uid + '.log'; - - // - // Check for existing global config and set each - // key appropriately if it exists. - // - ['append', 'silent', 'verbose'].forEach(function (key) { - var target = mappings[key], - value = forever.config.get(key); - - if (value) { - options[target] = options[target] || value === 'true'; - } - }); - - // - // Pass the source dir to spawn - // - options.spawnWith = { - cwd: options.sourceDir - }; - - // - // Configure winston for forever based on the CLI options - // - if (options.verbose) { - forever.log.transports.console.level = 'silly'; - } - - // - // Setup configurations for forever - // - var config = { - root: cmd.flags.p - }; - - // - // Only call `forever.load()` if the root path is different than - // the default root exposed by forever. - // - if ((config.root && config.root !== forever.root)) { - forever.log.silly('Loading forever with config: ', config); - forever.load(config); - forever.log.silly('Loaded forever successfully.'); - } - - next(); - }) - app.cli('/install', function(cmd,tty) { - cmd.service.install(function onInstall(err) { - if (err) { - tty.error(err); - } - else { - tty.info('foreverd installed'); - } - }); - }); - //TODO - app.cli('/run', function(cmd,tty) { - cmd.service.load(function() { - cmd.service.run(); - }) - }); - app.cli('/uninstall', function(cmd,tty) { - cmd.service.uninstall(); - }); - app.cli('/add/*', function(cmd,tty) { - cmd.service.add(cmd.file, cmd.options); +var router = module.exports = function router(app) { + app.use(function (cmd, tty, next) { + cmd.flags._.shift(); + cmd.service = new ForeverService({ + adapter: cmd.flags.adapter }); - //TODO - app.cli('/remove', function(cmd,tty) { - cmd.service.remove(cmd.file, cmd.options); - }); - app.cli('/start', function(cmd,tty) { - cmd.service.start(); - }); - //TODO - app.cli('/stop', function(cmd,tty) { - cmd.service.stop(); + + cmd.argv = cmd.flags._; + var file = cmd.argv[0], + options = {}, + config, + uid; + + cmd.file = file; + cmd.options = options; + + if (file) { + // + // Setup pass-thru options for child-process + // + options.options = cmd.argv.splice(cmd.argv.indexOf(file)).splice(1); + } + else if (cmd.flags.c || cmd.flags.command) { + options.options = cmd.argv.splice(cmd.argv.indexOf(cmd.flags.c || cmd.flags.command) + 1); + } + + // + // Now that we've removed the target script options + // reparse the options and configure the forever settings + // + argv = optimist(cmd.argv).boolean(['v', 'verbose', 'a', 'append', 's', 'silent']).argv; + Object.keys(argv).forEach(function (key) { + if (mappings[key] && argv[key]) { + options[mappings[key]] = argv[key]; + } }); - app.cli('/restart', function(cmd,tty) { - cmd.service.restart(); + + if (typeof options['max'] === 'undefined') { + // + // If max isn't specified set it to run forever + // + options.forever = true; + } + + if (typeof options['minUptime'] !== 'undefined') { + options['minUptime'] = parseFloat(options['minUptime']); + } + if (typeof options['spinSleepTime'] !== 'undefined') { + options['spinSleepTime'] = parseFloat(options['spinSleepTime']); + } + + if (!options.sourceDir) { + // + // Set the sourceDir of the options for graceful + // restarting outside of the main directory + // + options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/'; + } + + uid = options.uid || forever.randomString(24); + options.uid = uid; + options.pidFile = options.pidFile || uid + '.pid'; + options.logFile = argv.l || uid + '.log'; + + // + // Check for existing global config and set each + // key appropriately if it exists. + // + ['append', 'silent', 'verbose'].forEach(function (key) { + var target = mappings[key], + value = forever.config.get(key); + + if (value) { + options[target] = options[target] || value === 'true'; + } }); - app.cli('/list', function(cmd,tty) { - cmd.service.list(function(err, applications) { - applications.forEach(function printApplication(application) { - console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile) - }); - }); + + // + // Pass the source dir to spawn + // + options.spawnWith = { + cwd: options.sourceDir + }; + + // + // Configure winston for forever based on the CLI options + // + if (options.verbose) { + forever.log.transports.console.level = 'silly'; + } + + // + // Setup configurations for forever + // + config = { + root: cmd.flags.p + }; + + // + // Only call `forever.load()` if the root path is different than + // the default root exposed by forever. + // + if ((config.root && config.root !== forever.root)) { + forever.log.silly('Loading forever with config: ', config); + forever.load(config); + forever.log.silly('Loaded forever successfully.'); + } + + next(); + }); + + app.cli('/install', function (cmd, tty) { + cmd.service.install(function onInstall(err) { + if (err) { + tty.error(err); + } + else { + tty.info('foreverd installed'); + } }); - app.cli('/pause', function(cmd,tty) { - cmd.service.pause(); + }); + + //TODO + app.cli('/run', function (cmd, tty) { + cmd.service.load(function () { + cmd.service.run(); }); - app.cli('/resume', function(cmd,tty) { - cmd.service.resume(); + }); + + app.cli('/uninstall', function (cmd, tty) { + cmd.service.uninstall(); + }); + + app.cli('/add/*', function (cmd, tty) { + cmd.service.add(cmd.file, cmd.options); + }); + + //TODO + app.cli('/remove', function (cmd, tty) { + cmd.service.remove(cmd.file, cmd.options); + }); + + app.cli('/start', function (cmd, tty) { + cmd.service.start(); + }); + + //TODO + app.cli('/stop', function (cmd, tty) { + cmd.service.stop(); + }); + + app.cli('/restart', function (cmd, tty) { + cmd.service.restart(); + }); + + app.cli('/list', function (cmd, tty) { + cmd.service.list(function (err, applications) { + applications.forEach(function printApplication(application) { + console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile); + }); }); -} + }); + + app.cli('/pause', function (cmd, tty) { + cmd.service.pause(); + }); + app.cli('/resume', function (cmd, tty) { + cmd.service.resume(); + }); +}; \ No newline at end of file diff --git a/lib/foreverd/service.js b/lib/foreverd/service.js index 5700cf29..49f3f29e 100644 --- a/lib/foreverd/service.js +++ b/lib/foreverd/service.js @@ -1,50 +1,57 @@ -var util = require('util'); -var EventEmitter2 = require('eventemitter2').EventEmitter2; -var SystemVAdapter = require('./adapter/systemv') -var forever = require('../forever'); -var path = require('path'); -var fs = require('fs'); -var dnode = require('dnode'); -var portfinder = require('portfinder') +var fs = require('fs'), + path = require('path'), + util = require('util'), + dnode = require('dnode'), + EventEmitter2 = require('eventemitter2').EventEmitter2, + portfinder = require('portfinder'), + forever = require('../forever'), + SystemVAdapter = require('./adapter/systemv'); -module.exports = ForeverService; // options // directories {log, pid, conf, run, local} -function ForeverService(options) { - EventEmitter2.call(this); - options = options || {}; - var self = this; - this.applications = [ - //{ - //file: - //options: - //monitor: - //} - ]; - this.servers = []; - if(typeof options.adapter == 'string') { - options.adapter = ForeverService.adapter[options.adapter]; - } - var AdapterType = options.adapter || SystemVAdapter; - this.adapter = new AdapterType(this); - console.log(this.adapter) -} +var ForeverService = module.exports = function ForeverService(options) { + EventEmitter2.call(this); + options = options || {}; + + var self = this, + AdapterType; + + this.applications = [ + //{ + //file: + //options: + //monitor: + //} + ]; + + this.servers = []; + if (typeof options.adapter === 'string') { + options.adapter = ForeverService.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)); + ForeverService[adapterModule] = require(path.join(__dirname, 'adapter', adapterModule)); }); ForeverService.prototype.startServer = function startServer(callback) { - var socket = path.join(forever.config.get('sockPath'), 'forever.sock'), + var socket = path.join(forever.config.get('sockPath'), 'forever.sock'), monitors = [], + self = this, server; - console.log('SS') + + console.log('SS'); server = dnode(this); - var self = this; + portfinder.getSocket({ path: socket }, function onSocketFound(err, socket) { - console.error(arguments) + console.error(arguments); if (err) { return callback(err); } @@ -54,7 +61,7 @@ ForeverService.prototype.startServer = function startServer(callback) { // TODO: This is really bad. // }); - + server.on('ready', function onServerReady(err) { self.listen(server); if (callback) { @@ -66,36 +73,45 @@ ForeverService.prototype.startServer = function startServer(callback) { } } }); - + server.listen(socket); }); + return this; }; ForeverService.prototype.listen = function listen(server) { - var dnodeServer = dnode(this); - this.servers.push(dnodeServer); - dnodeServer.listen(server); - setTimeout(function(){console.error(dnodeServer)},10000) - return this; -} + var dnodeServer = dnode(this); + + this.servers.push(dnodeServer); + dnodeServer.listen(server); + + setTimeout(function () { + console.error(dnodeServer); + }, 10000); + + return this; +}; ForeverService.prototype.load = function load() { - var self = this; - this.adapter.load(function onLoaded(applications) { - console.error(arguments) - applications.forEach(function startApplication(application, index) { - var monitor = application.monitor = new forever.Monitor(application.file, application.options); - monitor.start(); - self.applications.push(application); - if(index === applications.length - 1) { - self.listen(path.join(forever.config.get('root'),'foreverd.sock')); - } - self.emit('foreverd::loaded') - }); + var self = this; + this.adapter.load(function onLoaded(applications) { + console.error(arguments); + applications.forEach(function startApplication(application, index) { + var monitor = application.monitor = new forever.Monitor(application.file, application.options); + + monitor.start(); + self.applications.push(application); + + if (index === applications.length - 1) { + self.listen(path.join(forever.config.get('root'), 'foreverd.sock')); + } + + self.emit('foreverd::loaded'); }); - return this; -} + }); + return this; +}; // // Function add(file, options) @@ -104,12 +120,13 @@ ForeverService.prototype.load = function load() { // call's the service manager's add method // ForeverService.prototype.add = function add(file, options, callback) { - console.log(arguments) - if (this.paused) { - return callabck && callback(new Error('foreverd is paused')); - } - this.adapter.add(file, options, callback); -} + console.log(arguments); + if (this.paused) { + return callback && callback(new Error('foreverd is paused')); + } + + this.adapter.add(file, options, callback); +}; // // Function remove(file, options) @@ -117,131 +134,152 @@ ForeverService.prototype.add = function add(file, options, callback) { // call's the service manager's remove method // ForeverService.prototype.remove = function remove(file, options, callback) { - if (this.paused) { - return callback(new Error('foreverd is paused')); - } - var applicationsToRemove = this.applications; - if (file) { - var fileStr = JSON.stringify(file); - applicationsToRemove = applicationsToRemove.filter(function compareFile(application) { - return fileStr !== JSON.stringify(application.file); - }); - } - if (options) { - var optionStr = JSON.stringify(options); - applicationsToRemove = applicationsToRemove.filter(function compareOptions(application) { - return optionStr !== JSON.stringify(application.options); - }); - } - var self = this; - applicationsToRemove.forEach(function removeApplication(application) { - if (application.monitor) { - application.monitor.stop(); - } - self.applications.splice(self.applications.indexOf(application), 1); + if (this.paused) { + return callback(new Error('foreverd is paused')); + } + + var applicationsToRemove = this.applications, + self = this, + optionStr, + fileStr; + + if (file) { + fileStr = JSON.stringify(file); + applicationsToRemove = applicationsToRemove.filter(function compareFile(application) { + return fileStr !== JSON.stringify(application.file); + }); + } + + if (options) { + optionStr = JSON.stringify(options); + applicationsToRemove = applicationsToRemove.filter(function compareOptions(application) { + return optionStr !== JSON.stringify(application.options); }); - callback && callback(); - return this; -} + } + + applicationsToRemove.forEach(function removeApplication(application) { + if (application.monitor) { + application.monitor.stop(); + } + + self.applications.splice(self.applications.indexOf(application), 1); + }); + + if (callback) { + callback(); + } + + return this; +}; // // Function install() // installs all the required to run foreverd // call's the service manager's install(options) // - ForeverService.prototype.install = function install(callback) { - this.adapter.install(callback); - return this; -} + this.adapter.install(callback); + return this; +}; // // Function uninstall(options) // uninstalls all the required to run foreverd // call's the service manager's uninstall(options) // - ForeverService.prototype.uninstall = function uninstall(callback) { - this.adapter.uninstall(callback); - return this; -} + this.adapter.uninstall(callback); + return this; +}; // // Function start() // calls the appropriate OS functionality to start this service // ForeverService.prototype.start = function start(callback) { - this.adapter.start(callback); - return this; -} + this.adapter.start(callback); + return this; +}; // // Function run() // creates monitors for all the services // ForeverService.prototype.run = function run(callback) { - var self = this; - this.adapter.run(function adapterStarted() { - console.error(self.applications) - self.applications.forEach(function startApplication(application) { - console.error(application) - application.monitor = new forever.Monitor(application.file, application.options); - application.monitor.start(); - }); - callback && callback(); + var self = this; + this.adapter.run(function adapterStarted() { + console.error(self.applications); + self.applications.forEach(function startApplication(application) { + console.error(application); + application.monitor = new forever.Monitor(application.file, application.options); + application.monitor.start(); }); - return this; -} + + return callback && callback(); + }); + + return this; +}; // // Function stop(monitors) // ForeverService.prototype.stop = function stop(callback) { - var self = this; - this.adapter.start(function adapterStopped() { - self.applications.forEach(function stopApplication(application) { - application.monitor.stop(); - }); - callback && callback(); + var self = this; + this.adapter.start(function adapterStopped() { + self.applications.forEach(function stopApplication(application) { + application.monitor.stop(); }); - return this; -} + + return callback && callback(); + }); + + return this; +}; // // Function restart() // ForeverService.prototype.restart = function restart(callback) { - var self = this; - this.adapter.start(function adapterRestarted() { - self.applications.forEach(function restartApplication(application) { - application.monitor.restart(); - }); - callback && callback(); + var self = this; + this.adapter.start(function adapterRestarted() { + self.applications.forEach(function restartApplication(application) { + application.monitor.restart(); }); - return this; -} + + return callback && callback(); + }); + + return this; +}; // // Function pause() // disables adding / removing applications // ForeverService.prototype.pause = function pause(callback) { - this.paused = true; - callback && callback(); - return this; -} + this.paused = true; + if (callback) { + callback(); + } + + return this; +}; // // Function resume() // reenables adding / removing applications // ForeverService.prototype.resume = function resume(callback) { - this.paused = false; - callback && callback(); - return this; -} + this.paused = false; + if (callback) { + callback(); + } + + return this; +}; ForeverService.prototype.list = function list(callback) { - this.adapter.list(callback); - return this; -} \ No newline at end of file + this.adapter.list(callback); + return this; +}; \ No newline at end of file