* Using external config
[nancy.git] / nancy.god
blobdbd5d690e3df96759db462ce51ffdfbfde01d68f
1 NANCY_ROOT = '/var/www/blakemizerany.com'
3 [*8000..8001].each do |port|
5   God.watch do |w|
6     w.name = "local-#{port}"
7     w.interval = 5.seconds # default
8     w.start = "ruby #{NANCY_ROOT}/nancy.rb -p #{port} -e production"
9   
10     # clean pid files before start if necessary
11     w.behavior(:clean_pid_file)
12   
13     # determine the state on startup
14     w.transition(:init, { true => :up, false => :start }) do |on|
15       on.condition(:process_running) do |c|
16         c.running = true
17       end
18     end
19   
20     # determine when process has finished starting
21     w.transition([:start, :restart], :up) do |on|
22       on.condition(:process_running) do |c|
23         c.running = true
24       end
25     
26       # failsafe
27       on.condition(:tries) do |c|
28         c.times = 5
29         c.transition = :start
30       end
31     end
33     # start if process is not running
34     w.transition(:up, :start) do |on|
35       on.condition(:process_exits)
36     end
37   
38     # restart if memory or cpu is too high
39     w.transition(:up, :restart) do |on|
40       on.condition(:memory_usage) do |c|
41         c.interval = 20
42         c.above = 50.megabytes
43         c.times = [3, 5]
44       end
45     
46       on.condition(:cpu_usage) do |c|
47         c.interval = 10
48         c.above = 10.percent
49         c.times = [3, 5]
50       end
51     end
52   
53     # lifecycle
54     w.lifecycle do |on|
55       on.condition(:flapping) do |c|
56         c.to_state = [:start, :restart]
57         c.times = 5
58         c.within = 5.minute
59         c.transition = :unmonitored
60         c.retry_in = 10.minutes
61         c.retry_times = 5
62         c.retry_within = 2.hours
63       end
64     end
65   end
67 end