UNPKG

ozserver

Version:

API for OZ

75 lines (68 loc) 2.49 kB
var cluster, colors, http, numCPUs, server; colors = require('colors'); cluster = require('cluster'); http = require('http'); numCPUs = require('os').cpus(); server = require(global.home + '/script/controllers/server'); module.exports = function(program) { if (program == null) { throw 'program is not exists'; } if (server == null) { throw 'server is not exists'; } program.option('-c, --connection <string>', 'connection string to mongodb').option('-p, --port <port>', 'port for server', parseInt).option('-P, --profile <name>', 'profile for settings in /usr/lib/ozserver'); program.command('run').description('run server').action(function() { global.command = 'run'; if (cluster.isMaster) { server.configure({ port: program.port != null ? program.port : null, connection: program.connection != null ? program.connection : null, profile: program.profile != null ? program.profile : null }, function(results) { var args; args = ["run"]; console.log("server work at ".grey + ("http://localhost:" + (results[0].toString())).blue); if (program.port != null) { args.push('--port'); args.push(program.port); } if (program.connection != null) { args.push('--connection'); args.push(program.connection); } if (program.profile != null) { args.push('--profile'); args.push(program.profile); } cluster.setupMaster({ exec: "" + global.home + "/ozserver", args: args, silent: false }); cluster.fork(); return cluster.on('exit', function(worker, code, signal) { return console.log("worker " + worker.process.pid + " died"); }); }); } if (cluster.isWorker) { process.on('uncaughtException', function(err) { return console.error('Caught exception:'.red, JSON.stringify(err).blue); }); return server.run({ port: program.port != null ? program.port : null, connection: program.connection != null ? program.connection : null, profile: program.profile != null ? program.profile : null }, function(service, options) { return service.listen(options.port); }); } }); return program.on('--help', function() { console.log(' Examples:'); console.log(''); console.log(' $ ozserver run'); return console.log(''); }); };