An event scheduler.

Happenstance is a scheduler. You put values in it. They come back out when the time is right. It doesn't set timers, so you'll need to check with it at regular intervals.

var Scheduler = require('happenstance')
var scheduler = new Scheduler
scheduler.schedule('a', 1, Date.now() + 30000)
scheduler.schedule('b', 1, Date.now() + 30000)
scheduler.schedule('c', 3, Date.now() + 120000)
setTimeout(function () {
    console.log('after one minute')
    setTimeout(function () {
        console.log('after two and a half minutes')
    }, 90000)
}, 60000)

The above program will print the following.

$ node schedule.js
after one minute
after two and a half minutes

Createa new Scheduler object.

Options are:

  • key an key that you can use to remove the event using unschedule.
  • value the value returned when the event is due.
  • when time time after which the event is due.

Unschedule the event with the given key.

Return any events currently due.

Clear all scheduled events.

This is all seeming very dubious now. It seems like it would be better to have a clock rather than a range for a delay.

scheduler.schedule(key, value, 1, 4)

The above would pull a random number from the random number generator that would be used for all delays in the next series of ticks.

The only immediate downstream is Paxos, and it doesn't really need to stagger its events, since events going to eminate out from the core of the algorithm, there is no point in providing back-off logic.

Okay, so it is not here, I've removed delay ranges, because that's back-off stuff and it doesn't belong here, it should be just external so you can record it for replaying.