public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
commit  429ddd75ec6a04d5e520bc6198238bf6c2e68c14
tree    c8dca9434f2379948e2cea3c60ed7eb25a4b130b
parent  f5012ae9b02f2eed32a018a769f11e9439cc0134
thin / example / thin.god
100644 80 lines (64 sloc) 2.217 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# == God config file
# http://god.rubyforge.org/
# Authors: Gump and michael@glauche.de
#
# Config file for god that configures watches for each instance of a thin server for
# each thin configuration file found in /etc/thin.
# In order to get it working on Ubuntu, I had to make a change to god as noted at
# the following blog:
# http://blog.alexgirard.com/2007/10/25/ruby-one-line-to-save-god/
#
require 'yaml'
 
config_path = "/etc/thin"
 
Dir[config_path + "/*.yml"].each do |file|
  config = YAML.load_file(file)
  num_servers = config["servers"] ||= 1
 
  (0...num_servers).each do |i|
    # UNIX socket cluster use number 0 to 2 (for 3 servers)
    # and tcp cluster use port number 3000 to 3002.
    number = config['socket'] ? i : (config['port'] + i)
    
    God.watch do |w|
      w.group = "thin-" + File.basename(file, ".yml")
      w.name = w.group + "-#{number}"
      
      w.interval = 30.seconds
      
      w.uid = config["user"]
      w.gid = config["group"]
      
      w.start = "thin start -C #{file} -o #{number}"
      w.start_grace = 10.seconds
      
      w.stop = "thin stop -C #{file} -o #{number}"
      w.stop_grace = 10.seconds
      
      w.restart = "thin restart -C #{file} -o #{number}"
 
      pid_path = config["chdir"] + "/" + config["pid"]
      ext = File.extname(pid_path)
 
      w.pid_file = pid_path.gsub(/#{ext}$/, ".#{number}#{ext}")
      
      w.behavior(:clean_pid_file)
 
      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
 
      w.lifecycle do |on|
        on.condition(:flapping) do |c|
          c.to_state = [:start, :restart]
          c.times = 5
          c.within = 5.minutes
          c.transition = :unmonitored
          c.retry_in = 10.minutes
          c.retry_times = 5
          c.retry_within = 2.hours
        end
      end
    end
  end
end