evented_bluepill 0.0.46 → 0.0.47
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -54
- data/bin/bluepill +2 -3
- data/bin/sample_forking_server +55 -0
- data/evented_bluepill.gemspec +20 -79
- data/lib/bluepill.rb +7 -4
- data/lib/bluepill/application.rb +32 -75
- data/lib/bluepill/application/client.rb +3 -1
- data/lib/bluepill/application/server.rb +5 -3
- data/lib/bluepill/condition_watch.rb +25 -19
- data/lib/bluepill/controller.rb +14 -12
- data/lib/bluepill/dsl.rb +7 -151
- data/lib/bluepill/dsl/app_proxy.rb +29 -0
- data/lib/bluepill/dsl/process_factory.rb +87 -0
- data/lib/bluepill/dsl/process_proxy.rb +39 -0
- data/lib/bluepill/event.rb +25 -0
- data/lib/bluepill/group.rb +25 -18
- data/lib/bluepill/logger.rb +11 -9
- data/lib/bluepill/process.rb +139 -149
- data/lib/bluepill/process_conditions.rb +3 -1
- data/lib/bluepill/process_conditions/always_true.rb +4 -2
- data/lib/bluepill/process_conditions/cpu_usage.rb +4 -2
- data/lib/bluepill/process_conditions/http.rb +2 -0
- data/lib/bluepill/process_conditions/mem_usage.rb +6 -4
- data/lib/bluepill/process_conditions/process_condition.rb +6 -4
- data/lib/bluepill/process_statistics.rb +3 -1
- data/lib/bluepill/socket.rb +29 -9
- data/lib/bluepill/system.rb +50 -52
- data/lib/bluepill/trigger.rb +39 -37
- data/lib/bluepill/triggers/flapping.rb +16 -18
- data/lib/bluepill/util/rotational_array.rb +15 -13
- data/lib/bluepill/version.rb +3 -1
- data/lib/example.rb +2 -1
- data/lib/runit_example.rb +2 -0
- metadata +29 -23
- data/VERSION +0 -1
- data/bin/bpsv +0 -3
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'bluepill/process'
|
4
|
+
|
5
|
+
module Bluepill
|
6
|
+
class ProcessProxy
|
7
|
+
attr_reader :attributes, :watches, :name
|
8
|
+
def initialize(process_name, attributes, process_block)
|
9
|
+
@name = process_name
|
10
|
+
@attributes = attributes
|
11
|
+
@watches = {}
|
12
|
+
|
13
|
+
process_block.call(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
if args.size == 1 && name.to_s =~ /^(.*)=$/
|
18
|
+
@attributes[$1.to_sym] = args.first
|
19
|
+
elsif args.empty? && @attributes.key?(name.to_sym)
|
20
|
+
@attributes[name.to_sym]
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def checks(name, options = {})
|
27
|
+
@watches[name] = options
|
28
|
+
end
|
29
|
+
|
30
|
+
def monitor_children(&child_process_block)
|
31
|
+
@attributes[:monitor_children] = true
|
32
|
+
@attributes[:child_process_block] = child_process_block
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_process
|
36
|
+
Bluepill::Process.new(@name, @watches, @attributes)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'coolio'
|
4
|
+
|
5
|
+
module Bluepill
|
6
|
+
module Event
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def attach(watcher)
|
10
|
+
self.event_loop.attach watcher
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
self.event_loop.run
|
15
|
+
end
|
16
|
+
|
17
|
+
def stop
|
18
|
+
self.event_loop.stop
|
19
|
+
end
|
20
|
+
|
21
|
+
def event_loop
|
22
|
+
@event_loop ||= Coolio::Loop.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/bluepill/group.rb
CHANGED
@@ -1,25 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
module Bluepill
|
4
|
+
class GroupTimer < Coolio::TimerWatcher
|
5
|
+
def initialize(process, event)
|
6
|
+
@process = process
|
7
|
+
@event = event
|
8
|
+
|
9
|
+
super(0, false)
|
10
|
+
end
|
11
|
+
|
12
|
+
def on_timer
|
13
|
+
@process.handle_user_command(@event)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
2
17
|
class Group
|
3
18
|
attr_accessor :name, :processes, :logger
|
4
19
|
attr_accessor :process_logger
|
5
|
-
|
20
|
+
|
6
21
|
def initialize(name, options = {})
|
7
22
|
self.name = name
|
8
23
|
self.processes = []
|
9
24
|
self.logger = options[:logger]
|
10
25
|
end
|
11
|
-
|
26
|
+
|
12
27
|
def add_process(process)
|
13
28
|
process.logger = self.logger.prefix_with(process.name)
|
14
29
|
self.processes << process
|
15
30
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
self.processes.each do |process|
|
19
|
-
process.tick
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
31
|
+
|
23
32
|
def determine_initial_state
|
24
33
|
self.processes.each do |process|
|
25
34
|
process.determine_initial_state
|
@@ -30,30 +39,28 @@ module Bluepill
|
|
30
39
|
[:start, :unmonitor, :stop, :restart].each do |event|
|
31
40
|
class_eval <<-END
|
32
41
|
def #{event}(process_name = nil)
|
33
|
-
threads = []
|
34
42
|
affected = []
|
35
43
|
self.processes.each do |process|
|
36
44
|
next if process_name && process_name != process.name
|
37
45
|
affected << [self.name, process.name].join(":")
|
38
|
-
|
46
|
+
Bluepill::Event.attach(Bluepill::GroupTimer.new(process, "#{event}"))
|
39
47
|
end
|
40
|
-
threads.each { |t| t.join }
|
41
48
|
affected
|
42
|
-
end
|
49
|
+
end
|
43
50
|
END
|
44
51
|
end
|
45
|
-
|
52
|
+
|
46
53
|
def status(process_name = nil)
|
47
54
|
lines = []
|
48
55
|
if process_name.nil?
|
49
56
|
prefix = self.name ? " " : ""
|
50
57
|
lines << "#{self.name}:" if self.name
|
51
|
-
|
58
|
+
|
52
59
|
self.processes.each do |process|
|
53
60
|
lines << "%s%s(pid:%s): %s" % [prefix, process.name, process.actual_pid, process.state]
|
54
61
|
if process.monitor_children?
|
55
|
-
process.
|
56
|
-
lines << " %s%s: %s" % [prefix, child.name, child.state]
|
62
|
+
process.children_timer.each do |child|
|
63
|
+
lines << " %s%s: %s" % [prefix, child.process.name, child.process.state]
|
57
64
|
end
|
58
65
|
end
|
59
66
|
end
|
@@ -66,6 +73,6 @@ module Bluepill
|
|
66
73
|
end
|
67
74
|
lines << ""
|
68
75
|
end
|
69
|
-
|
76
|
+
|
70
77
|
end
|
71
78
|
end
|
data/lib/bluepill/logger.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Bluepill
|
2
4
|
class Logger
|
3
5
|
LOG_METHODS = [:emerg, :alert, :crit, :err, :warning, :notice, :info, :debug]
|
4
|
-
|
6
|
+
|
5
7
|
def initialize(options = {})
|
6
8
|
@options = options
|
7
9
|
@logger = options[:logger] || self.create_logger
|
@@ -9,7 +11,7 @@ module Bluepill
|
|
9
11
|
@stdout = options[:stdout]
|
10
12
|
@prefixes = {}
|
11
13
|
end
|
12
|
-
|
14
|
+
|
13
15
|
LOG_METHODS.each do |method|
|
14
16
|
eval <<-END
|
15
17
|
def #{method}(msg, prefix = [])
|
@@ -26,11 +28,11 @@ module Bluepill
|
|
26
28
|
end
|
27
29
|
END
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
def prefix_with(prefix)
|
31
33
|
@prefixes[prefix] ||= self.class.new(:logger => self, :prefix => prefix)
|
32
34
|
end
|
33
|
-
|
35
|
+
|
34
36
|
def reopen
|
35
37
|
if @logger.is_a?(self.class)
|
36
38
|
@logger.reopen
|
@@ -38,7 +40,7 @@ module Bluepill
|
|
38
40
|
@logger = create_logger
|
39
41
|
end
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
protected
|
43
45
|
def create_logger
|
44
46
|
if @options[:log_file]
|
@@ -50,13 +52,13 @@ module Bluepill
|
|
50
52
|
end
|
51
53
|
|
52
54
|
class LoggerAdapter < ::Logger
|
53
|
-
LOGGER_EQUIVALENTS =
|
55
|
+
LOGGER_EQUIVALENTS =
|
54
56
|
{:debug => :debug, :err => :error, :warning => :warn, :info => :info, :emerg => :fatal, :alert => :warn, :crit => :fatal, :notice => :info}
|
55
|
-
|
57
|
+
|
56
58
|
LOG_METHODS.each do |method|
|
57
59
|
next if method == LOGGER_EQUIVALENTS[method]
|
58
60
|
alias_method method, LOGGER_EQUIVALENTS[method]
|
59
61
|
end
|
60
|
-
end
|
62
|
+
end
|
61
63
|
end
|
62
64
|
end
|
data/lib/bluepill/process.rb
CHANGED
@@ -1,37 +1,61 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'state_machine'
|
4
|
+
require 'daemons'
|
3
5
|
|
4
6
|
module Bluepill
|
7
|
+
class ProcessTimer < Coolio::TimerWatcher
|
8
|
+
attr_accessor :process
|
9
|
+
|
10
|
+
def initialize(process)
|
11
|
+
self.process = process
|
12
|
+
super(1, true)
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_timer
|
16
|
+
return if self.process.skipping_ticks?
|
17
|
+
self.process.skip_ticks_until = nil
|
18
|
+
|
19
|
+
# clear the memoization per tick
|
20
|
+
self.process.process_running = nil
|
21
|
+
|
22
|
+
# run state machine transitions
|
23
|
+
self.process.tick
|
24
|
+
|
25
|
+
self.process.refresh_children! if self.process.up? and self.process.monitor_children?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
5
29
|
class Process
|
6
30
|
CONFIGURABLE_ATTRIBUTES = [
|
7
|
-
:start_command,
|
8
|
-
:stop_command,
|
9
|
-
:restart_command,
|
10
|
-
|
31
|
+
:start_command,
|
32
|
+
:stop_command,
|
33
|
+
:restart_command,
|
34
|
+
|
11
35
|
:stdout,
|
12
36
|
:stderr,
|
13
37
|
:stdin,
|
14
|
-
|
15
|
-
:daemonize,
|
16
|
-
:pid_file,
|
38
|
+
|
39
|
+
:daemonize,
|
40
|
+
:pid_file,
|
17
41
|
:working_dir,
|
18
42
|
:environment,
|
19
|
-
|
20
|
-
:start_grace_time,
|
21
|
-
:stop_grace_time,
|
43
|
+
|
44
|
+
:start_grace_time,
|
45
|
+
:stop_grace_time,
|
22
46
|
:restart_grace_time,
|
23
|
-
|
47
|
+
|
24
48
|
:uid,
|
25
49
|
:gid,
|
26
|
-
|
50
|
+
|
27
51
|
:monitor_children,
|
28
|
-
:
|
52
|
+
:child_process_factory
|
29
53
|
]
|
30
|
-
|
31
|
-
attr_accessor :name, :watches, :triggers, :logger, :skip_ticks_until
|
54
|
+
|
55
|
+
attr_accessor :name, :watches, :triggers, :logger, :skip_ticks_until, :process_running
|
32
56
|
attr_accessor *CONFIGURABLE_ATTRIBUTES
|
33
|
-
attr_reader :
|
34
|
-
|
57
|
+
attr_reader :children_timer, :statistics, :timer
|
58
|
+
|
35
59
|
state_machine :initial => :unmonitored do
|
36
60
|
# These are the idle states, i.e. only an event (either external or internal) will trigger a transition.
|
37
61
|
# The distinction between down and unmonitored is that down
|
@@ -47,15 +71,15 @@ module Bluepill
|
|
47
71
|
|
48
72
|
transition :up => :up, :if => :process_running?
|
49
73
|
transition :up => :down, :unless => :process_running?
|
50
|
-
|
74
|
+
|
51
75
|
# The process failed to die after entering the stopping state. Change the state to reflect
|
52
76
|
# reality.
|
53
|
-
transition :stopping => :up, :if => :process_running?
|
77
|
+
transition :stopping => :up, :if => :process_running?
|
54
78
|
transition :stopping => :down, :unless => :process_running?
|
55
|
-
|
79
|
+
|
56
80
|
transition :down => :up, :if => :process_running?
|
57
81
|
transition :down => :starting, :unless => :process_running?
|
58
|
-
|
82
|
+
|
59
83
|
transition :restarting => :up, :if => :process_running?
|
60
84
|
transition :restarting => :down, :unless => :process_running?
|
61
85
|
end
|
@@ -84,116 +108,79 @@ module Bluepill
|
|
84
108
|
|
85
109
|
after_transition any => any, :do => :record_transition
|
86
110
|
end
|
87
|
-
|
88
|
-
def initialize(process_name, options = {})
|
111
|
+
|
112
|
+
def initialize(process_name, checks, options = {})
|
89
113
|
@name = process_name
|
90
|
-
@event_mutex = Monitor.new
|
91
114
|
@transition_history = Util::RotationalArray.new(10)
|
92
115
|
@watches = []
|
93
116
|
@triggers = []
|
94
|
-
@
|
117
|
+
@children_timer = []
|
95
118
|
@statistics = ProcessStatistics.new
|
96
|
-
|
119
|
+
@actual_pid = options[:actual_pid]
|
120
|
+
self.logger = options[:logger]
|
121
|
+
|
122
|
+
checks.each do |name, opts|
|
123
|
+
if Bluepill::Trigger[name]
|
124
|
+
self.add_trigger(name, opts)
|
125
|
+
else
|
126
|
+
self.add_watch(name, opts)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
97
130
|
# These defaults are overriden below if it's configured to be something else.
|
98
131
|
@monitor_children = false
|
99
132
|
@start_grace_time = @stop_grace_time = @restart_grace_time = 3
|
100
133
|
@environment = {}
|
101
|
-
|
134
|
+
|
102
135
|
CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
|
103
136
|
self.send("#{attribute_name}=", options[attribute_name]) if options.has_key?(attribute_name)
|
104
137
|
end
|
105
|
-
|
138
|
+
|
106
139
|
# Let state_machine do its initialization stuff
|
107
140
|
super() # no arguments intentional
|
108
141
|
end
|
109
142
|
|
110
|
-
def tick
|
111
|
-
return if self.skipping_ticks?
|
112
|
-
self.skip_ticks_until = nil
|
113
|
-
|
114
|
-
# clear the memoization per tick
|
115
|
-
@process_running = nil
|
116
|
-
|
117
|
-
# run state machine transitions
|
118
|
-
super
|
119
|
-
|
120
|
-
if self.up?
|
121
|
-
self.run_watches
|
122
|
-
|
123
|
-
if self.monitor_children?
|
124
|
-
refresh_children!
|
125
|
-
children.each {|child| child.tick}
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
143
|
def logger=(logger)
|
131
144
|
@logger = logger
|
132
145
|
self.watches.each {|w| w.logger = logger }
|
133
146
|
self.triggers.each {|t| t.logger = logger }
|
134
147
|
end
|
135
|
-
|
148
|
+
|
136
149
|
# State machine methods
|
137
150
|
def dispatch!(event, reason = nil)
|
138
|
-
@
|
139
|
-
|
140
|
-
self.send("#{event}")
|
141
|
-
end
|
151
|
+
@statistics.record_event(event, reason)
|
152
|
+
self.send("#{event}")
|
142
153
|
end
|
143
|
-
|
154
|
+
|
144
155
|
def record_transition(transition)
|
145
156
|
unless transition.loopback?
|
146
|
-
@transitioned = true
|
147
|
-
|
148
157
|
# When a process changes state, we should clear the memory of all the watches
|
149
158
|
self.watches.each { |w| w.clear_history! }
|
150
|
-
|
159
|
+
|
151
160
|
# Also, when a process changes state, we should re-populate its child list
|
152
161
|
if self.monitor_children?
|
153
162
|
self.logger.warning "Clearing child list"
|
154
|
-
self.
|
163
|
+
self.children_timer.each {|timer| timer.detach }
|
164
|
+
self.children_timer.each {|timer| timer.process.watches.each {|w| w.detach }}
|
165
|
+
self.children_timer.clear
|
155
166
|
end
|
156
167
|
logger.info "Going from #{transition.from_name} => #{transition.to_name}"
|
157
168
|
end
|
158
169
|
end
|
159
|
-
|
170
|
+
|
160
171
|
def notify_triggers(transition)
|
161
172
|
self.triggers.each {|trigger| trigger.notify(transition)}
|
162
173
|
end
|
163
|
-
|
174
|
+
|
164
175
|
# Watch related methods
|
165
176
|
def add_watch(name, options = {})
|
166
|
-
self.watches << ConditionWatch.new(name, options.merge(:logger => self.logger))
|
177
|
+
self.watches << ConditionWatch.new(name, self, options.merge(:logger => self.logger))
|
167
178
|
end
|
168
|
-
|
179
|
+
|
169
180
|
def add_trigger(name, options = {})
|
170
181
|
self.triggers << Trigger[name].new(self, options.merge(:logger => self.logger))
|
171
182
|
end
|
172
183
|
|
173
|
-
def run_watches
|
174
|
-
now = Time.now.to_i
|
175
|
-
|
176
|
-
threads = self.watches.collect do |watch|
|
177
|
-
[watch, Thread.new { Thread.current[:events] = watch.run(self.actual_pid, now) }]
|
178
|
-
end
|
179
|
-
|
180
|
-
@transitioned = false
|
181
|
-
|
182
|
-
threads.inject([]) do |events, (watch, thread)|
|
183
|
-
thread.join
|
184
|
-
if thread[:events].size > 0
|
185
|
-
logger.info "#{watch.name} dispatched: #{thread[:events].join(',')}"
|
186
|
-
thread[:events].each do |event|
|
187
|
-
events << [event, watch.to_s]
|
188
|
-
end
|
189
|
-
end
|
190
|
-
events
|
191
|
-
end.each do |(event, reason)|
|
192
|
-
break if @transitioned
|
193
|
-
self.dispatch!(event, reason)
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
184
|
def determine_initial_state
|
198
185
|
if self.process_running?(true)
|
199
186
|
self.state = 'up'
|
@@ -201,8 +188,18 @@ module Bluepill
|
|
201
188
|
# TODO: or "unmonitored" if bluepill was started in no auto-start mode.
|
202
189
|
self.state = 'down'
|
203
190
|
end
|
191
|
+
|
192
|
+
# TODO move into right position
|
193
|
+
self.set_timer
|
194
|
+
end
|
195
|
+
|
196
|
+
def set_timer
|
197
|
+
@timer = Bluepill::ProcessTimer.new(self)
|
198
|
+
Bluepill::Event.attach(self.timer)
|
199
|
+
|
200
|
+
self.watches.each {|w| Bluepill::Event.attach(w) }
|
204
201
|
end
|
205
|
-
|
202
|
+
|
206
203
|
def handle_user_command(cmd)
|
207
204
|
case cmd
|
208
205
|
when "start"
|
@@ -223,67 +220,67 @@ module Bluepill
|
|
223
220
|
dispatch!(:unmonitor, "user initiated")
|
224
221
|
end
|
225
222
|
end
|
226
|
-
|
223
|
+
|
227
224
|
# System Process Methods
|
228
225
|
def process_running?(force = false)
|
229
226
|
@process_running = nil if force # clear existing state if forced
|
230
|
-
|
227
|
+
|
231
228
|
@process_running ||= signal_process(0)
|
232
229
|
# the process isn't running, so we should clear the PID
|
233
230
|
self.clear_pid unless @process_running
|
234
231
|
@process_running
|
235
232
|
end
|
236
|
-
|
233
|
+
|
237
234
|
def start_process
|
238
235
|
logger.warning "Executing start command: #{start_command}"
|
239
|
-
|
236
|
+
|
240
237
|
if self.daemonize?
|
241
238
|
System.daemonize(start_command, self.system_command_options)
|
242
|
-
|
239
|
+
|
243
240
|
else
|
244
241
|
# This is a self-daemonizing process
|
245
242
|
with_timeout(start_grace_time) do
|
246
243
|
result = System.execute_blocking(start_command, self.system_command_options)
|
247
|
-
|
244
|
+
|
248
245
|
unless result[:exit_code].zero?
|
249
246
|
logger.warning "Start command execution returned non-zero exit code:"
|
250
247
|
logger.warning result.inspect
|
251
|
-
end
|
248
|
+
end
|
252
249
|
end
|
253
250
|
end
|
254
|
-
|
251
|
+
|
255
252
|
self.skip_ticks_for(start_grace_time)
|
256
253
|
end
|
257
|
-
|
258
|
-
def stop_process
|
254
|
+
|
255
|
+
def stop_process
|
259
256
|
if stop_command
|
260
257
|
cmd = self.prepare_command(stop_command)
|
261
258
|
logger.warning "Executing stop command: #{cmd}"
|
262
|
-
|
259
|
+
|
263
260
|
with_timeout(stop_grace_time) do
|
264
261
|
result = System.execute_blocking(cmd, self.system_command_options)
|
265
|
-
|
262
|
+
|
266
263
|
unless result[:exit_code].zero?
|
267
264
|
logger.warning "Stop command execution returned non-zero exit code:"
|
268
265
|
logger.warning result.inspect
|
269
266
|
end
|
270
267
|
end
|
271
|
-
|
268
|
+
|
272
269
|
else
|
273
270
|
logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}"
|
274
271
|
signal_process("TERM")
|
275
272
|
end
|
276
273
|
self.unlink_pid # TODO: we only write the pid file if we daemonize, should we only unlink it if we daemonize?
|
277
|
-
|
274
|
+
|
278
275
|
self.skip_ticks_for(stop_grace_time)
|
279
276
|
end
|
280
|
-
|
277
|
+
|
281
278
|
def restart_process
|
282
279
|
if restart_command
|
283
280
|
cmd = self.prepare_command(restart_command)
|
284
|
-
|
281
|
+
|
285
282
|
logger.warning "Executing restart command: #{cmd}"
|
286
|
-
|
283
|
+
|
287
284
|
with_timeout(restart_grace_time) do
|
288
285
|
result = System.execute_blocking(cmd, self.system_command_options)
|
289
286
|
|
@@ -292,7 +289,7 @@ module Bluepill
|
|
292
289
|
logger.warning result.inspect
|
293
290
|
end
|
294
291
|
end
|
295
|
-
|
292
|
+
|
296
293
|
self.skip_ticks_for(restart_grace_time)
|
297
294
|
else
|
298
295
|
logger.warning "No restart_command specified. Must stop and start to restart"
|
@@ -300,22 +297,22 @@ module Bluepill
|
|
300
297
|
# the tick will bring it back.
|
301
298
|
end
|
302
299
|
end
|
303
|
-
|
300
|
+
|
304
301
|
def daemonize?
|
305
302
|
!!self.daemonize
|
306
303
|
end
|
307
|
-
|
304
|
+
|
308
305
|
def monitor_children?
|
309
306
|
!!self.monitor_children
|
310
307
|
end
|
311
|
-
|
308
|
+
|
312
309
|
def signal_process(code)
|
313
310
|
::Process.kill(code, actual_pid)
|
314
311
|
true
|
315
312
|
rescue
|
316
313
|
false
|
317
314
|
end
|
318
|
-
|
315
|
+
|
319
316
|
def actual_pid
|
320
317
|
@actual_pid ||= begin
|
321
318
|
if pid_file
|
@@ -329,76 +326,69 @@ module Bluepill
|
|
329
326
|
end
|
330
327
|
end
|
331
328
|
end
|
332
|
-
|
329
|
+
|
333
330
|
def actual_pid=(pid)
|
334
331
|
@actual_pid = pid
|
335
332
|
end
|
336
|
-
|
333
|
+
|
337
334
|
def clear_pid
|
338
335
|
@actual_pid = nil
|
339
336
|
end
|
340
|
-
|
337
|
+
|
341
338
|
def unlink_pid
|
342
339
|
File.unlink(pid_file) if pid_file && File.exists?(pid_file)
|
343
340
|
end
|
344
|
-
|
341
|
+
|
345
342
|
# Internal State Methods
|
346
343
|
def skip_ticks_for(seconds)
|
347
344
|
# TODO: should this be addative or longest wins?
|
348
345
|
# i.e. if two calls for skip_ticks_for come in for 5 and 10, should it skip for 10 or 15?
|
349
346
|
self.skip_ticks_until = (self.skip_ticks_until || Time.now.to_i) + seconds.to_i
|
350
347
|
end
|
351
|
-
|
348
|
+
|
352
349
|
def skipping_ticks?
|
353
350
|
self.skip_ticks_until && self.skip_ticks_until > Time.now.to_i
|
354
351
|
end
|
355
|
-
|
352
|
+
|
356
353
|
def refresh_children!
|
357
354
|
# First prune the list of dead children
|
358
|
-
|
359
|
-
|
355
|
+
dead_children = self.children_timer.select {|timer| !timer.process.process_running?(true) }
|
356
|
+
dead_children.each {|timer| timer.detach }
|
357
|
+
dead_children.each {|timer| timer.process.watches.each {|w| w.detach }}
|
358
|
+
@children_timer -= dead_children
|
359
|
+
|
360
360
|
# Add new found children to the list
|
361
|
-
new_children_pids = System.get_children(self.actual_pid) -
|
362
|
-
|
361
|
+
new_children_pids = System.get_children(self.actual_pid) - self.children_timer.map {|timer| timer.process.actual_pid}
|
362
|
+
|
363
363
|
unless new_children_pids.empty?
|
364
|
-
logger.info "Existing children: #{
|
364
|
+
logger.info "Existing children: #{self.children_timer.collect{|c| c.process.actual_pid}.join(",")}. Got new children: #{new_children_pids.inspect} for #{actual_pid}"
|
365
365
|
end
|
366
|
-
|
366
|
+
|
367
367
|
# Construct a new process wrapper for each new found children
|
368
368
|
new_children_pids.each do |child_pid|
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
child
|
373
|
-
|
374
|
-
|
369
|
+
name = "<child(pid:#{child_pid})>"
|
370
|
+
logger = self.logger.prefix_with(name)
|
371
|
+
|
372
|
+
child = self.child_process_factory.create_child_process(name, child_pid, logger)
|
373
|
+
|
375
374
|
child.initialize_state_machines
|
376
375
|
child.state = "up"
|
377
|
-
|
378
|
-
@children << child
|
379
|
-
end
|
380
|
-
end
|
381
376
|
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
clone.instance_variable_set("@event_mutex", Monitor.new)
|
388
|
-
clone.instance_variable_set("@triggers", triggers.collect{ |t| t.deep_copy })
|
389
|
-
@event_mutex = mutex
|
390
|
-
@triggers = triggers
|
391
|
-
clone
|
377
|
+
child.set_timer
|
378
|
+
child.watches.each {|w| w.process = child }
|
379
|
+
|
380
|
+
@children_timer << child.timer
|
381
|
+
end
|
392
382
|
end
|
393
383
|
|
394
384
|
def prepare_command(command)
|
395
385
|
command.to_s.gsub("{{PID}}", actual_pid.to_s)
|
396
386
|
end
|
397
|
-
|
387
|
+
|
398
388
|
def system_command_options
|
399
389
|
{
|
400
|
-
:uid => self.uid,
|
401
|
-
:gid => self.gid,
|
390
|
+
:uid => self.uid,
|
391
|
+
:gid => self.gid,
|
402
392
|
:working_dir => self.working_dir,
|
403
393
|
:environment => self.environment,
|
404
394
|
:pid_file => self.pid_file,
|
@@ -408,10 +398,10 @@ module Bluepill
|
|
408
398
|
:stderr => self.stderr
|
409
399
|
}
|
410
400
|
end
|
411
|
-
|
401
|
+
|
412
402
|
def with_timeout(secs, &blk)
|
413
403
|
Timeout.timeout(secs.to_f, &blk)
|
414
|
-
|
404
|
+
|
415
405
|
rescue Timeout::Error
|
416
406
|
logger.err "Execution is taking longer than expected. Unmonitoring."
|
417
407
|
logger.err "Did you forget to tell bluepill to daemonize this process?"
|
@@ -419,4 +409,4 @@ module Bluepill
|
|
419
409
|
end
|
420
410
|
end
|
421
411
|
end
|
422
|
-
|
412
|
+
|