Skip to content

Instantly share code, notes, and snippets.

@alx
Created March 6, 2009 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alx/74756 to your computer and use it in GitHub Desktop.
Save alx/74756 to your computer and use it in GitHub Desktop.
# run with: god -c /path/to/merb_mpc.god
#
# This script controls the multi-process Merb 1.0 service
# To implement:
# start program = "/home/deploy/bin/monit_merb_mpc slice start_master /home/deploy/legodata -c2 -n4000"
# stop program = "/home/deploy/bin/monit_merb_mpc slice stop_master /home/deploy/legodata"
#
# start program = "/home/deploy/bin/monit_merb_mpc slice register_worker /home/deploy/legodata 4000"
# stop program = "/home/deploy/bin/monit_merb_mpc slice restart_worker /home/deploy/legodata 4000"
#
# start program = "/home/deploy/bin/monit_merb_mpc slice register_worker /home/deploy/legodata 4001"
# stop program = "/home/deploy/bin/monit_merb_mpc slice restart_worker /home/deploy/legodata 4001"
# /usr/local/bin/merb --name slice3_production -d -u deploy -G www-data -a mongrel \
# -L /home/deploy/legodata/slice3/current/log/production.log -e production -m /home/deploy/legodata/slice3/current \
# -c 2 -P '/home/deploy/legodata/slice3/shared/pids/production-merb.%s.pid' -p 4004
# restart_worker command="$merb -K $worker_id -e $environment -m $app_dir -P '$pid_dir/$real_pid_file'"
# ====
#
# Config
#
# ====
APP_NAME = "slice3"
APP_ENV = "production"
APP_ROOT = File.join("/home/deploy/legodata/", APP_NAME)
APP_USER = "deploy"
APP_GROUP = "www-data"
APP_ADAPTER = "mongrel"
APP_PIDS = File.join(APP_ROOT, "/shared/pids/")
CLUSTER_NODES = 2
CLUSTER_PORT = 4004
# ====
#
# Worker process
#
# ====
def merb_worker(port)
God.watch do |w|
w.name = "#{APP_NAME}-#{APP_ENV} worker - port #{port}"
w.interval = 30.seconds # default
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = File.join(APP_PIDS, "production-merb.#{port}.pid")
w.start = ""
w.stop = ""
w.restart = "merb -K #{port} -e #{APP_ENV} -m #{File.join(APP_ROOT, "/current")} \
-P '#{File.join(APP_PIDS, "production-merb.#{port}.pid")}'"
w.uid = APP_USER
w.gid = APP_GROUP
w.group = APP_NAME
w.behavior(:clean_pid_file)
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 150.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
# lifecycle
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
end
# ====
#
# Master process
#
# ====
God.watch do |w|
w.name = "#{APP_NAME}-#{APP_ENV} master"
w.interval = 30.seconds # default
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = File.join(APP_PIDS, "production-merb.main.pid")
w.uid = APP_USER
w.gid = APP_GROUP
w.group = APP_NAME
w.start = "merb --name #{APP_NAME}-#{APP_ENV} -d \
-u #{APP_USER} -G #{APP_GROUP} -a #{APP_ADAPTER} -e #{APP_ENV} \
-L #{File.join(APP_ROOT, "/current/log/#{APP_ENV}.log")} -m #{File.join(APP_ROOT, "/current")} \
-c #{CLUSTER_NODES} -P '#{File.join(APP_PIDS, "production-merb.%s.pid")}' -p #{CLUSTER_PORT}"
w.stop = "merb -K all -e #{APP_ENV} -m #{File.join(APP_ROOT, "/current")} \
-P '#{File.join(APP_PIDS, "production-merb.%s.pid")}'"
w.behavior(:clean_pid_file)
[*CLUSTER_PORT ... (CLUSTER_PORT + CLUSTER_NODES)].each do |worker_port|
merb_worker(worker_port)
end
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 150.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
# lifecycle
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
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