Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

A more complex example of cluster url fetcher worker. #4

Merged
merged 1 commit into from

2 participants

@AlexeyKupershtokh

Please check if this is a useful one. Clustering is easily removable from this worker.

@andris9 andris9 merged commit 79ff114 into from
@andris9
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 30, 2012
  1. cluster fetcher example

    wicked authored
This page is out of date. Refresh to see the latest.
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();
+ });
+ });
+};
Something went wrong with that request. Please try again.