Leader election for nodejs applications
Elect a leader among several nodes using consul.
Connect to consul and get notified about leadership status.
var leaderElection = ;var service = ;var config =key: "locks/my-little-service"consul :host: "my.consul.hostname";
The let-it-crash way
If you want your application to crash in case of leader election errors, just omit the "error" listener. This is the simplest and most robust way to deal with errors, but it requires that you have a process manager (such as forever or pm2) in place to ensure that the application is restarted again.
key: "locks/my-little-service" // Only required optiondebug: consolelogconsul:host: "127.0.0.1"port: 8050token: "asdpf2344d083udhd8723"ttl: 15lockDelay: 10readWait: 180
consul.host: Consul hostname or ip (string)
consul.port: Consul port (number)
consul.ttl: Tuning, session ttl seconds (number, default 15)
consul.lockDelay: Tuning, session lock-delay seconds (number, default 10)
consul.readWait: Tuning, read wait seconds (number, default 180)
To read more about the tuning parameters, see https://www.consul.io/docs/internals/sessions.html
debug: function accepting a variable number of args. Supply if you want to print debugging info.