Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cluster fetcher example

  • Loading branch information...
commit 13c56a429b290375c4f64d139eec84724f45aaf2 1 parent 9bf119c
wicked authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 examples/cluster_fetcher.js
View
46 examples/cluster_fetcher.js
@@ -0,0 +1,46 @@
+var cluster = require('cluster');
+var http = require('http');
+var url = require('url');
+var Gearman = require("node-gearman"),
+ gearman = new Gearman(); // defaults to localhost
+var numCPUs = require('os').cpus().length;
+
+if (cluster.isMaster) {
+ // Fork workers.
+ for (var i = 0; i < numCPUs; i++) {
+ console.log('fork');
+ cluster.fork();
+ }
+
+ // Handle deaths and respawn workers.
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ cluster.fork();
+ });
+} else {
+ gearman.registerWorker("fetch", function(payload, worker){
+ console.log('Payload: ' + payload);
+ if(!payload){
+ worker.error();
+ return;
+ }
+
+ var body = '';
+
+ var options = url.parse(payload.toString());
+
+ var req = http.get(options, function(res) {
+ console.log("Got response: " + res.statusCode);
+ res.on('data', function(chunk) {
+ console.log("Body: " + chunk);
+ body += chunk;
+ });
+ res.on('end', function() {
+ worker.end(body);
+ });
+ }).on('error', function(e) {
+ console.log("Got error: " + e.message);
+ worker.error();
+ });
+ });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.