Class: GodProcess
- Inherits:
-
Object
show all
- Defined in:
- utils/god/god_process.rb
Constant Summary
collapse
- CONFIG_DEFAULTS =
{
:monitor_group => nil,
:uid => nil,
:gid => nil,
:start_notify => nil,
:restart_notify => nil,
:flapping_notify => nil,
:process_log_dir => '/var/log/god',
:start_grace_time => 20.seconds,
:restart_grace_time => nil, :default_interval => 5.minutes,
:start_interval => 5.minutes,
:mem_usage_interval => 20.minutes,
:cpu_usage_interval => 20.minutes,
}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ GodProcess
Returns a new instance of GodProcess.
21
22
23
|
# File 'utils/god/god_process.rb', line 21
def initialize config
self.config = CONFIG_DEFAULTS.compact.merge(config)
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
20
21
22
|
# File 'utils/god/god_process.rb', line 20
def config
@config
end
|
Class Method Details
.create(config = {}) ⇒ Object
34
35
36
37
38
|
# File 'utils/god/god_process.rb', line 34
def self.create config={}
proc = self.new config
proc.setup
proc
end
|
Instance Method Details
#config_watcher(watcher) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'utils/god/god_process.rb', line 71
def config_watcher watcher
watcher.name = self.handle
watcher.start = start_command
watcher.stop = stop_command if stop_command
watcher.restart = restart_command if restart_command
watcher.group = config[:monitor_group] if config[:monitor_group]
watcher.uid = config[:uid] if config[:uid]
watcher.gid = config[:gid] if config[:gid]
watcher.interval = config[:default_interval]
watcher.start_grace = config[:start_grace_time]
watcher.restart_grace = config[:restart_grace_time] || (config[:start_grace_time] + 2.seconds)
watcher.behavior(:clean_pid_file)
end
|
#handle ⇒ Object
40
41
42
|
# File 'utils/god/god_process.rb', line 40
def handle
(config[:handle] || "#{self.class.kind}_#{config[:port]}").to_s
end
|
#mkdirs! ⇒ Object
49
50
51
52
|
# File 'utils/god/god_process.rb', line 49
def mkdirs!
require 'fileutils'
FileUtils.mkdir_p File.dirname(process_log_file)
end
|
#process_log_file ⇒ Object
44
45
46
|
# File 'utils/god/god_process.rb', line 44
def process_log_file
File.join(config[:process_log_dir], handle+".log")
end
|
#restart_command ⇒ Object
command to restart if stop_command is nil, it lets god daemonize the process otherwise, by default it runs stop_command, pauses for 1 second, then runs start_command
66
67
68
69
|
# File 'utils/god/god_process.rb', line 66
def restart_command
return unless stop_command
[stop_command, "sleep 1", start_command].join(" && ")
end
|
#setup ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'utils/god/god_process.rb', line 25
def setup
LOG.info config.inspect
God.watch do |watcher|
config_watcher watcher
setup_start watcher
setup_restart watcher
setup_lifecycle watcher
end
end
|
#setup_lifecycle(watcher) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'utils/god/god_process.rb', line 115
def setup_lifecycle watcher
watcher.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 10
c.within = 15.minute
c.transition = :unmonitored
c.retry_in = 60.minutes
c.retry_times = 5
c.retry_within = 12.hours
c.notify = config[:flapping_notify] if config[:flapping_notify]
end
end
end
|
#setup_restart(watcher) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'utils/god/god_process.rb', line 97
def setup_restart watcher
watcher.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.interval = config[:mem_usage_interval] if config[:mem_usage_interval]
c.above = config[:max_mem_usage] || 150.megabytes
c.times = [3, 5] c.notify = config[:restart_notify] if config[:restart_notify]
end
restart.condition(:cpu_usage) do |c|
c.interval = config[:cpu_usage_interval] if config[:cpu_usage_interval]
c.above = config[:max_cpu_usage] || 50.percent
c.times = 5
c.notify = config[:restart_notify] if config[:restart_notify]
end
end
end
|
#setup_start(watcher) ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'utils/god/god_process.rb', line 86
def setup_start watcher
watcher.start_if do |start|
start.condition(:process_running) do |c|
c.interval = config[:start_interval]
c.running = false
c.notify = config[:start_notify] if config[:start_notify]
end
end
end
|
#start_command ⇒ Object
command to start the daemon
55
56
57
|
# File 'utils/god/god_process.rb', line 55
def start_command
config[:start_command]
end
|
#stop_command ⇒ Object
command to stop the daemon return nil to have god daemonize the process
60
61
62
|
# File 'utils/god/god_process.rb', line 60
def stop_command
config[:stop_command]
end
|