Created
February 28, 2010 18:13
-
-
Save pgr0ss/317707 to your computer and use it in GitHub Desktop.
This file contains 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 sys = require('sys'), | |
http = require('http'), | |
redis = require("./redisclient"); | |
var queuedRes = {} | |
var counter = 1; | |
http.createServer(function (req, res) { | |
pushOnQueue(req, res); | |
}).listen(8000); | |
function pushOnQueue(req, res) { | |
requestNumber = counter++; | |
message = JSON.stringify({ | |
"class": "RequestProcessor", | |
"args": [ {"node_id": requestNumber, "url": req.url} ] | |
}); | |
client.rpush('resque:queue:requests', message); | |
queuedRes[requestNumber] = res | |
} | |
var client = new redis.Client(); | |
client.connect(function() { | |
popFromQueue(); | |
}); | |
function popFromQueue() { | |
client.lpop('responses', handleResponse); | |
} | |
function handleResponse(err, result) { | |
if (result == null) { | |
setTimeout(function() { popFromQueue(); }, 100); | |
} else { | |
json = JSON.parse(result); | |
requestNumber = json.node_id; | |
body = unescape(json.body); | |
res = queuedRes[requestNumber]; | |
res.writeHeader(200, {'Content-Type': 'text/plain'}); | |
res.write(body); | |
res.close(); | |
delete queuedRes[requestNumber]; | |
popFromQueue(); | |
} | |
} | |
sys.puts('Server running at http://127.0.0.1:8000/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment