cluster-service
Advanced tools
Comparing version 0.4.8 to 0.4.9
@@ -32,3 +32,14 @@ var | ||
log: console.log, | ||
error: console.error, | ||
debug: console.debug, | ||
json: false, // output as JSON | ||
colors: { | ||
cservice: "grey", | ||
success: "green", | ||
error: "red", | ||
data: "cyan", | ||
warn: "yellow", | ||
info: "magenta", | ||
debug: "grey" | ||
}, | ||
error: console.error | ||
@@ -69,4 +80,6 @@ } | ||
} else { // otherwise assume it is a command to execute | ||
options.run = options._[0]; | ||
options.cliEnabled = false; | ||
options.run = options._[0]; | ||
if (options.json === true) { | ||
options.cliEnabled = false; | ||
} | ||
} | ||
@@ -101,2 +114,4 @@ } | ||
colors.setTheme(options.colors); | ||
if (options.run) { | ||
@@ -180,12 +195,35 @@ require("./lib/run").start(options, function(err, result) { | ||
exports.on("workerStart", function(evt, pid, reason) { | ||
exports.log(("worker " + pid.cyan + " start, reason: " + (reason || locals.reason)).green); | ||
exports.log(("worker " + pid.data + " start, reason: " + (reason || locals.reason)).success); | ||
}, false); | ||
exports.on("workerExit", function(evt, pid, reason) { | ||
exports.log(("worker " + pid.cyan + " exited, reason: " + (reason || locals.reason)).yellow); | ||
exports.log(("worker " + pid.data + " exited, reason: " + (reason || locals.reason)).warn); | ||
}, false); | ||
} | ||
exports.log = function() { | ||
if (locals.options.cliEnabled === true && locals.options.log) { | ||
locals.options.log.apply(this, arguments); | ||
exports.debug = function () { | ||
if (locals.options.cliEnabled === true && locals.options.debug) { | ||
var args = Array.prototype.slice.call(arguments); | ||
for (var i = 0; i < args.length; i++) { | ||
if (typeof args[i] === "string") { | ||
args[i] = args[i].debug; | ||
} | ||
} | ||
if (args.length > 0 && typeof args[0] === "string" && args[0][0] === "{") { | ||
locals.options.debug("cservice:".cservice); | ||
} else { | ||
args = ["cservice: ".cservice].concat(args); | ||
} | ||
locals.options.debug.apply(this, args); | ||
} | ||
}; | ||
exports.log = function () { | ||
if (locals.options.cliEnabled === true && locals.options.log) { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (args.length > 0 && typeof args[0] === "string" && args[0][0] === "{") { | ||
locals.options.log("cservice:".cservice); | ||
} else { | ||
args = ["cservice: ".cservice].concat(args); | ||
} | ||
locals.options.log.apply(this, args); | ||
} | ||
@@ -196,9 +234,14 @@ }; | ||
if (locals.options.cliEnabled === true && locals.options.error) { | ||
var args = arguments; | ||
for (var i = 0; i < args.length; i++) { | ||
var args = Array.prototype.slice.call(arguments); | ||
for (var i = 0; i < args.length; i++) { | ||
if (typeof args[i] === "string") { | ||
args[i] = args[i].red; | ||
args[i] = args[i].error; | ||
} | ||
} | ||
locals.options.error.apply(this, args); | ||
if (args.length > 0 && typeof args[0] === "string" && args[0][0] === "{") { | ||
locals.options.error("cservice:".cservice); | ||
} else { | ||
args = ["cservice: ".cservice].concat(args); | ||
} | ||
locals.options.error.apply(this, args); | ||
} | ||
@@ -205,0 +248,0 @@ }; |
var | ||
cservice = require("../cluster-service"), | ||
readline = require("readline"), | ||
util = require("util"), | ||
rl = readline.createInterface({ input: process.stdin, output: process.stdout }), | ||
locals = null, | ||
@@ -16,7 +14,11 @@ options = null | ||
cservice.log("cluster-service CLI is now available. Enter 'help [enter]' for instructions.".magenta); | ||
cservice.log("CLI is now available. Enter 'help [enter]' for instructions.".info); | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on("data", onCommand); | ||
// wait momentarily before attaching CLI. allows workers a little time to output as needed | ||
setTimeout(function() { | ||
rl.question("cservice> ".grey, onCommand); | ||
process.stdout.write("cservice> ".cservice); | ||
}, 1000); | ||
@@ -33,2 +35,3 @@ }; | ||
function onCommand(question) { | ||
question = question.replace(/[\r\n]/g, ""); | ||
var args = require("./util").getArgsFromQuestion(question, " "); | ||
@@ -63,3 +66,3 @@ args = [args[0], onCallback].concat(args.slice(1)); | ||
rl.question("cservice> ".grey, onCommand); | ||
process.stdout.write("cservice> ".cservice); | ||
} |
var | ||
readline = require("readline"), | ||
util = require("util"), | ||
rl = readline.createInterface({ input: process.stdin, output: process.stdout }), | ||
request = require("request"), | ||
@@ -16,6 +14,9 @@ querystring = require("querystring"), | ||
cservice.log("Service already running. Attached CLI to master service. Enter 'help [enter]' for instructions.".magenta); | ||
cservice.log("Service already running. Attached CLI to master service. Enter 'help [enter]' for help.".info); | ||
if (!options || options.silentMode !== true) { | ||
rl.question("cservice> ".grey, onCommand); | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on("data", onCommand); | ||
process.stdout.write("cservice> ".cservice); | ||
} | ||
@@ -27,3 +28,4 @@ }; | ||
function onCommand(question, cb) { | ||
var split = question.split(" "); | ||
question = question.replace(/[\r\n]/g, ""); | ||
var split = question.split(" "); | ||
if (split[0] === "exit") { | ||
@@ -41,29 +43,25 @@ cservice.log("Exiting CLI ONLY.".yellow); | ||
; | ||
cservice.log("Running remote command: ".yellow + url.cyan); | ||
cservice.log("Running remote command: ".warn + url.data); | ||
request.post(url, function (err, res, body) { | ||
if (res.statusCode !== 200 && !err && body) { | ||
err = body; | ||
err = body; | ||
} | ||
if (err) { | ||
onCallback(err, null, cb); | ||
} else { | ||
onCallback(null, body, cb); | ||
} | ||
onCallback(err, body, res, cb); | ||
}); | ||
} | ||
function onCallback(err, result, cb) { | ||
function onCallback(err, result, res, cb) { | ||
if (err) { | ||
cservice.error("Error: ", err); | ||
} else if (result) { | ||
cservice.error("Error: ", err); | ||
result = { statusCode: res.statusCode, error: err }; | ||
} else if (result) { | ||
if (typeof result === "string" && (result.indexOf("{") === 0 || result.indexOf("[") === 0)) { | ||
result = JSON.parse(result); // deserialize | ||
} | ||
cservice.log(util.inspect(result, { depth: null, colors: true })); | ||
} | ||
cservice.log(util.inspect(result, { depth: null, colors: true })); | ||
//cservice.log("");//newline | ||
if (!options || options.silentMode !== true) { | ||
rl.question("cservice> ".grey, onCommand); | ||
//cservice.log("");//newline | ||
process.stdout.write("cservice> ".cservice); | ||
} | ||
@@ -70,0 +68,0 @@ |
@@ -37,3 +37,3 @@ var | ||
try { | ||
cservice.log("cservice.API: ".yellow + req.url.cyan); | ||
cservice.log("API: " + req.url.data); | ||
@@ -64,3 +64,3 @@ if (req.url.indexOf("/cli?") !== 0) { | ||
} catch (ex) { | ||
cservice.error("cservice: Woops, an ERROR!", util.inspect(ex, {depth:null}), util.inspect(ex.stack || new Error().stack, {depth:null})); | ||
cservice.error("Woops, an ERROR!".error, util.inspect(ex, { depth: null }), util.inspect(ex.stack || new Error().stack, { depth: null })); | ||
} | ||
@@ -74,3 +74,3 @@ } | ||
if (!locals.events[args[0]]) { | ||
cservice.error("Command " + args[0].cyan + " not found"); | ||
cservice.error("Command " + args[0].cyan + " not found".error); | ||
res.writeHead(404); | ||
@@ -127,3 +127,3 @@ res.end("Not found. Try /help"); | ||
res.end(JSON.stringify({ error: err })); | ||
cservice.error("cservice: Woops, an ERROR!", err, util.inspect(ex.stack || new Error().stack, {depth:null})); | ||
cservice.error("Woops, an ERROR!".error, err, util.inspect(ex.stack || new Error().stack, { depth: null })); | ||
} | ||
@@ -136,4 +136,4 @@ } else { | ||
} catch (ex) { | ||
cservice.error("cservice: Woops, an ERROR!", util.inspect(ex, {depth:null}), util.inspect(ex.stack || new Error().stack, {depth:null})); | ||
cservice.error("Woops, an ERROR!".error, util.inspect(ex, { depth: null }), util.inspect(ex.stack || new Error().stack, { depth: null })); | ||
} | ||
} |
@@ -90,3 +90,3 @@ var | ||
if (typeof options.accessKey !== "string") { // in-proc mode only | ||
cservice.log("cluster-service is in LOCAL ONLY MODE. Run with 'accessKey' option to enable communication channel.".magenta); | ||
cservice.log("LOCAL ONLY MODE. Run with 'accessKey' option to enable communication channel.".info); | ||
cb(); | ||
@@ -98,3 +98,3 @@ return; | ||
if (!err) { | ||
cservice.log(("cluster-service is listening at " + (options.host + ":" + options.port).cyan).magenta); | ||
cservice.log(("Listening at " + (options.host + ":" + options.port).data).info); | ||
} | ||
@@ -101,0 +101,0 @@ |
@@ -24,7 +24,10 @@ var | ||
; | ||
cservice.log("Running remote command: " + url); | ||
cservice.log("Running remote command: ".warn + url.data); | ||
request.post(url, function (err, res, body) { | ||
if (err) { | ||
if (res.statusCode !== 200 && !err && body) { | ||
err = body; | ||
} | ||
if (err) { | ||
cservice.error("Error: ", err); | ||
body = { error: err }; | ||
body = { statusCode: res.statusCode, error: err }; | ||
} else if (body) { | ||
@@ -31,0 +34,0 @@ if (typeof body === "string" && (body.indexOf("{") === 0 || body.indexOf("[") === 0)) { |
{ | ||
"name": "cluster-service", | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Aaron Silvas", |
67632
1705