Skip to content

Instantly share code, notes, and snippets.

@benaubin
Forked from traviskroberts/mysql.rb
Last active March 6, 2016 04:40
Show Gist options
  • Save benaubin/a9fbf5b1f019c83efc76 to your computer and use it in GitHub Desktop.
Save benaubin/a9fbf5b1f019c83efc76 to your computer and use it in GitHub Desktop.
a nice way of auto restarting mysql
# run in non-daemonized mode (so you can monitor it) with `god -c /path/to/mysql.god -D`
# run normally with `god -c /path/to/mysql.god`
God.watch do |w|
# you can name this whatever you want
w.name = "mySQL"
# polling interval
w.interval = 30.seconds
# command to start service
w.start = "service mysql start"
# command to stop service
w.stop = "service mysql stop"
# command to restart service
w.restart = "service mysql restart"
w.start_grace = 20.seconds
w.restart_grace = 20.seconds
# location of pid file
w.pid_file = "/var/run/mysqld/mysqld.pid"
# tell god to delete the pid file when mysqld crashes
w.behavior(:clean_pid_file)
# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
on.condition(:process_running) do |c|
c.running = true
end
end
# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
end
# failsafe
on.condition(:tries) do |c|
c.times = 8
c.within = 2.minutes
c.transition = :start
end
end
# start if process is not running
w.transition(:up, :start) do |on|
on.condition(:process_exits) do |c|
end
end
# lifecycle
w.lifecycle do |on|
# If the service keeps triggering a restart over and over, it is considered to be "flapping".
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 1.minute
c.transition = :unmonitored
# If the service is flapping, wait 10 minutes, then try to start/restart again.
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment