round-robin

1.0.0 • Public • Published

Round Robin Dependency Status Build Status Coverage Status

Create consumers that expire after a certain time period or task count that are scheduled to be used in a round robin fashion.

How to

 
var RoundRobin = require("round-robin").RoundRobin
 
var scheduler = new RoundRobin({
 
  // Create a new thing to do tasks
  spinUp: function (cb) {
    var thing = {doTask: function() {}, destroy: function() {}};
    cb(null, thing); // Remember to call the callback when ready
  },
  
  // Tear down the thing, it won't be used again!
  spinDown: function(thing, cb) {
    thing.destroy();
    cb(); // Remember to call the callback when done!
  },
  
  maxUp: 10, // Allow at most 10 things to be spun up at once
  maxUsage: 100, // Allow the thing to be used 100 times before it is spun down
  maxAge: 1000 * 60 * 60, // Allow the thing to live for an hour before it is spun down
  lazySpinDown: true // Spin down lazily (avoids the polling interval processing)
});
 
// Use the scheduler to get a thing and perform a task every second
var intervalId = setInterval(function () {
  scheduler.get(function (er, thing) {
    thing.doTask();
    scheduler.ret(thing); // Return the thing so the scheduler can hand it out again
  })
}, 1000);
 
// Finish doing tasks after 5 minutes
setTimeout(function () {
  clearInterval(intervalId);
  
  scheduler.destroy(function (er) {
    console.log("Finished doing tasks");
  })
}, 1000 * 60 * 5);
 

Readme

Keywords

none

Package Sidebar

Install

npm i round-robin

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • alanshaw