Пример #1
0
 pool.on('fail', function() {
   // When a remote action fails, any tasks remaining in the pool will be
   // cleared.  Some actions may be currently executing, so final reporting
   // will wait until they have completed and the pool is idle.
   pool.clearQueue();
   failed = true;
 });
Пример #2
0
var functionpool = require('functionpool');

var pool = new functionpool.Pool(function(delay, done) {
  if (delay < 0) {
    done(new Error('delay must be greater than zero'));
    return;
  }
  
  console.log('Working for ' + delay + ' milliseconds...');
  setTimeout(function() { done(null, delay) }, delay);
});

function callback(err, result) {
  if (err) { 
    console.log('ERROR: ' + err.name + ' ' + err.message);
    return;
  }
  console.log('Worked for ' + result + ' milliseconds.');
}


pool.add(1000, callback);
pool.add(-1, callback);
Пример #3
0
 // queues work for `host` and collects results.
 function work(host) {
   results[host] = null;
   pool.task(host, function(err) {
     results[host] = err || true;
   });
 }
Пример #4
0
 task(this._name, function() {
   var taskArgs = arguments;
   var results = {};
   var failed = false;
   
   // Allocate a function pool to perform the action on the remote hosts.
   // The pool serves as a mechanism to limit the number of simultaneous
   // connections that will be established.
   var pool = new functionpool.Pool({ size: 5 }, function(host, done) {
     function finished(err) {
       process.nextTick(function() { done(err); });
     };
     
     // Create a new context for this host, within which the action will be
     // invoked.
     var exec = new Executor(host);
     var args = Array.prototype.slice.call(taskArgs);
     args.push(finished);  // callback is the last argument
     self._action.apply(exec, args);
   });
   pool.on('fail', function() {
     // When a remote action fails, any tasks remaining in the pool will be
     // cleared.  Some actions may be currently executing, so final reporting
     // will wait until they have completed and the pool is idle.
     pool.clearQueue();
     failed = true;
   });
   pool.on('idle', function() {
     report();
     
     if (failed) { return fail(new Error('Remote task failed: ' + self._name)); }
     return complete();
   });
   
   
   // queues work for `host` and collects results.
   function work(host) {
     results[host] = null;
     pool.task(host, function(err) {
       results[host] = err || true;
     });
   }
   
   // reports results.
   function report() {
     var done = self._hosts.filter(function(host) {
       var result = results[host];
       return (result === true);
     });
     var failed = self._hosts.filter(function(host) {
       var result = results[host];
       return (result !== true && result !== null);
     });
     var incomplete = self._hosts.filter(function(host) {
       var result = results[host];
       return (result === null);
     });
     
     console.info('Task %s: %d/%d complete. (%d done, %d failed)', self._name,
                                                                   (done.length + failed.length),
                                                                   self._hosts.length,
                                                                   done.length,
                                                                   failed.length);
   }
   
   console.info('Invoking %s task on %d hosts.', self._name, self._hosts.length);
   for (var i = 0, len = self._hosts.length; i < len; i++) {
     work(self._hosts[i]);
   }
 }, {async: true});
Пример #5
0
var functionpool = require('functionpool');

var startedAt = Date.now();

var pool = new functionpool.Pool({ size: 3 }, function(delay, done) {
  console.log('Working for ' + delay + ' milliseconds...');
  setTimeout(done, delay);
});

pool.on('idle', function() {
  console.log('Finished (' + (Date.now() - startedAt) + ' ms)');
});

pool.task(1000);
pool.task(2000);
pool.task(3000);
pool.task(1000);