Created
February 20, 2011 20:56
-
-
Save swdyh/836299 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http') | |
// var times = parseInt(process.argv[2] || 5, 10) | |
var urls = [ | |
'/enq/1', | |
'/enq/999999', | |
'/enq/2', | |
'/enq/3', | |
'/enq/4' | |
] | |
urls.forEach(function(u) { | |
var url = 'http://localhost:3005' + u | |
console.log('GET:', url) | |
http.cat(url, function(err, val) { | |
if (err) { | |
console.log('E:', err) | |
} | |
console.log(val) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
var redis = require('redis') | |
var spawn = require('child_process').spawn | |
var app = express.createServer() | |
var redisc = redis.createClient() | |
var rediscp = redis.createClient() | |
var port = 3005 | |
var q_key = 'worker_q' | |
var rq_key = 'worker_rq' | |
var rq = {} | |
redisc.del(rq_key, function() { | |
run_workers(3) | |
run_server() | |
get_result() | |
}) | |
function get_result() { | |
// console.log('server: blpop', rq_key) | |
rediscp.blpop(rq_key, 0, function(err, val) { | |
if (val) { | |
var j = JSON.parse(val[1]) | |
var callback = rq[j['qid']] | |
if (callback) { | |
callback(j['val']) | |
delete rq[j['qid']] | |
} | |
} | |
setTimeout(get_result, 100) | |
}) | |
} | |
function run_workers(size) { | |
for (var i = 1; i <= size; i++) { | |
spawn_worker(i) | |
} | |
} | |
function spawn_worker(i) { | |
child = spawn('node', ['worker.js', q_key, rq_key]) | |
// console.log('spawn worker:', child.pid) | |
child.stdout.on('data', function (data) { | |
process.stdout.write('worker' + i + ': ' + data); | |
}) | |
child.stderr.on('data', function (data) { | |
process.stdout.write('worker' + i + ': ' + data) | |
}) | |
child.on('exit', function (code) { | |
process.stdout.write('worker' + i + ': exited with code ' + code); | |
}) | |
} | |
function run_server() { | |
var i = 0; | |
app.get('/', function(req, res) { | |
redisc.lrange(q_key, 0, -1, function(err, val) { | |
res.send(JSON.stringify(val)) | |
}) | |
}) | |
app.get('/enq/:id', function(req, res) { | |
console.log('server:', req.url) | |
var qid = 'q_' + (i++) | |
var val = { qid: qid, id: req.param('id') || 'none' } | |
rq[qid] = function(val) { | |
res.send(val || '') | |
} | |
redisc.lpush(q_key, JSON.stringify(val), function(err, val) { | |
if (err) { | |
console.log('E', err) | |
} | |
}) | |
}) | |
app.listen(port) | |
console.log('server: ' + port) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('redis') | |
var redisc = redis.createClient() | |
var q_key = process.argv[2] || 'worker_q' | |
var rq_key = process.argv[3] || 'worker_rq' | |
function foo(number) { | |
function calc(a, b){ | |
for(var c=0;c < b;c++) | |
a += 1 - 2*(c%2); | |
return a; | |
} | |
var n = 0, c; | |
for(c=0;c<number;c++){ | |
n = calc(n, c); | |
} | |
return n | |
} | |
function work() { | |
console.log('blpop') | |
redisc.blpop(q_key, 0, function(err, val) { | |
try { | |
var j = JSON.parse(val[1]) | |
var n = foo(j['id']) | |
var r = { qid: j['qid'], val: '' + n || 'none' } | |
redisc.lpush(rq_key, JSON.stringify(r)) | |
setTimeout(work, 100) | |
} | |
catch(e) { | |
console.lot(e) | |
setTimeout(work, 5000) | |
} | |
}) | |
} | |
work() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment