child_process = require 'child_process' handleError = (error)-> console.error "Error", error module.exports = (client)-> hashKey = "test-sentinel-" + Math.round( Math.random() * 1000000 ) console.log "Using test hash", hashKey # put an incremental n:n entry in a hash, # to see if we've missed anything during the failover. runCount = 0 doIO = -> runCount++ do (runCount)-> console.log "---- knock #{runCount} ----" n = runCount.toString() client.hset hashKey, n, n, (error)-> if error then handleError error client.hget hashKey, n, (error, val)-> if error then handleError error console.log "#{runCount} Set?", (val is n).toString() # count how many entries we're missing client.hgetall hashKey, (error, hash)-> missing = [] if error then handleError error else unless typeof hash is 'object' then handleError new Error "Missing hash #{hashKey}" else for i in [1..runCount] if not hash[ i.toString() ]? then missing.push i console.log "#{runCount} check integrity: " + (if missing.length is 0 then "all good" else "missing " + missing.join(',')) # continously hit the current master setInterval doIO, 1000 # kill the master after 5 seconds setTimeout -> # find the PID of the master on 5379 child_process.exec 'ps -ef | grep "redis-server" | grep "port 5379" | grep -v "grep" | awk \'{print $2}\'' , (error, stdout, stderr)-> # console.log "finding pid", console.log(arguments) pid = stdout.trim() if error or (stderr.trim() isnt '') or (not pid.match /^[0-9]*$/) console.error "Missing master pid, can't force failover", [ error, stderr, pid ] process.exit 1 else # kill the master console.log "**** Killing master on #{pid} ****" child_process.exec "kill #{pid}", (error, stdout, stderr)-> if error or (stderr.trim() isnt '') console.error "Failed to kill master pid", [ error, stderr ] else console.log "... should failover now!" , 5000