Skip to content

Instantly share code, notes, and snippets.

@badboy
Last active August 29, 2015 14:08
Show Gist options
  • Save badboy/8d189b672b5af60397d6 to your computer and use it in GitHub Desktop.
Save badboy/8d189b672b5af60397d6 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'fileutils'
$pids = []
$first_child = true
def kill_all
$pids.each do |pid|
begin
puts "Killing #{pid}..."
Process.kill(:INT, pid)
rescue Exception, Errno::ECHILD
end
end
$pids = []
Process.waitall
end
# Handle dead children
trap("SIGCHLD") {
begin
next unless $first_child
$first_child = false
child_pid = Process.waitpid(-1, Process::WNOHANG)
puts "Atleast one Redis process died. Let's stop all and bail out. (pid=#{child_pid})"
exit
rescue Errno::ECHILD
end
}
# Catch Ctrl-C
trap("SIGINT") {
$first_child = false
exit
}
# On exit, make sure all Redis instances are killed
at_exit { kill_all }
sentinel_conf = "sentinel/sentinel.conf.tmpl"
unless File.exist?(sentinel_conf)
puts "Please make sure that the Sentinel config template is available in #{sentinel_conf}"
exit 2
end
dirs =["r1", "r2", "r3", "s1", "s2", "s3"]
dirs.each do |dir|
FileUtils.mkdir_p("sentinel/#{dir}")
if dir.start_with?("s")
FileUtils.cp(sentinel_conf, "sentinel/#{dir}/sentinel.conf")
end
end
# Masters
Dir.chdir("sentinel/r1") { $pids << spawn("redis-server --port 6380") }
#$pids << spawn("redis-server --port 6381")
# Slaves of Master 1
Dir.chdir("sentinel/r2") { $pids << spawn("redis-server --port 63800 --slaveof 127.0.0.1 6380") }
Dir.chdir("sentinel/r3") { $pids << spawn("redis-server --port 63801 --slaveof 127.0.0.1 6380") }
# Slaves of Master 2
#$pids << spawn("redis-server --port 63810 --slaveof 127.0.0.1 6381")
#$pids << spawn("redis-server --port 63811 --slaveof 127.0.0.1 6381")
# Sentinels
Dir.chdir("sentinel/s1") { $pids << spawn("redis-server sentinel.conf --sentinel --port 26379") }
Dir.chdir("sentinel/s1") { $pids << spawn("redis-server sentinel.conf --sentinel --port 26380") }
Dir.chdir("sentinel/s1") { $pids << spawn("redis-server sentinel.conf --sentinel --port 26381") }
sleep 5
$first_child = false
Process.waitall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment