Skip to content

Instantly share code, notes, and snippets.

@teemow
Created June 24, 2011 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teemow/1044557 to your computer and use it in GitHub Desktop.
Save teemow/1044557 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var argv = require('/usr/local/lib/node_modules/optimist')
.usage('Usage: $0 [env]')
.boolean('f')
.argv
, exec = require('child_process').exec
if (argv._.length > 0) env = argv._.shift()
else env = 'production'
var cc_app = 'adcloud/' + env
function cmd(params) {
return 'cctrlapp ' + cc_app + ' ' + params
}
function get_worker_ids(cb) {
exec(cmd('worker'), function(err, result, stderr) {
if (err) throw err
if (stderr) return console.log(stderr)
var worker_ids = result.match(/(wrk.*)/g)
worker_ids.shift()
cb(undefined, worker_ids)
})
}
function get_worker_info(worker_ids, cb) {
var number_of_workers = worker_ids.length
, workers = []
worker_ids.forEach(function(worker) {
exec(cmd('worker ' + worker), function(err, result) {
if (err) {
number_of_workers--
//console.log(err.message.replace(/\n/, ''))
return
}
var data = {}
result = result.split(/\n/)
result.forEach(function(line) {
var match = line.match(/\s*(\w*)\s*:\s*(.*)/)
if (match) data[match[1]] = match[2]
})
workers.push(data)
if (workers.length === number_of_workers) cb(undefined, workers)
})
})
}
function display(workers) {
workers.sort(function(a, b) {
return new Date(a.created) - new Date(b.created)
})
console.log("\033[2J")
console.log('found ' + workers.length + ' workers')
workers.forEach(function(worker) {
console.log(worker.created + ' ' + worker.wrk_id + ' ' + worker.params)
})
}
function main() {
get_worker_ids(function(err, worker_ids) {
get_worker_info(worker_ids, function(err, workers) {
display(workers)
})
})
}
if (argv.f) setInterval(main, 10000)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment