bluepill 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +50 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +105 -8
- data/bin/bluepill +25 -30
- data/bluepill.gemspec +21 -28
- data/lib/bluepill/application/lifecycle.rb +166 -0
- data/lib/bluepill/application/listener.rb +124 -0
- data/lib/bluepill/application/previous_daemon.rb +81 -0
- data/lib/bluepill/application.rb +57 -254
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +134 -56
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -94
- data/lib/bluepill/daemonize.rb +234 -0
- data/lib/bluepill/dsl/app_proxy.rb +30 -6
- data/lib/bluepill/dsl/process_factory/children.rb +71 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
- data/lib/bluepill/dsl/process_factory.rb +81 -92
- data/lib/bluepill/dsl/process_proxy.rb +109 -21
- data/lib/bluepill/group.rb +187 -78
- data/lib/bluepill/logger.rb +139 -48
- data/lib/bluepill/pid_file.rb +23 -0
- data/lib/bluepill/process/attributes.rb +379 -0
- data/lib/bluepill/process/children.rb +70 -0
- data/lib/bluepill/process/pid.rb +154 -0
- data/lib/bluepill/process/state_machine.rb +196 -0
- data/lib/bluepill/process/transition.rb +45 -0
- data/lib/bluepill/process/transition_callbacks.rb +101 -0
- data/lib/bluepill/process/user_commands.rb +71 -0
- data/lib/bluepill/process/watches.rb +141 -0
- data/lib/bluepill/process.rb +56 -653
- data/lib/bluepill/process_commands/execution.rb +89 -0
- data/lib/bluepill/process_commands/hooks.rb +186 -0
- data/lib/bluepill/process_commands/stop_signals.rb +114 -0
- data/lib/bluepill/process_commands.rb +104 -172
- data/lib/bluepill/process_conditions/always_true.rb +1 -1
- data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
- data/lib/bluepill/process_conditions/file_time.rb +4 -5
- data/lib/bluepill/process_conditions/http.rb +52 -24
- data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
- data/lib/bluepill/process_conditions/process_condition.rb +34 -3
- data/lib/bluepill/process_conditions/running_time.rb +1 -25
- data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
- data/lib/bluepill/process_conditions.rb +3 -2
- data/lib/bluepill/process_journal/cleanup.rb +200 -0
- data/lib/bluepill/process_journal/lock.rb +106 -0
- data/lib/bluepill/process_journal.rb +126 -233
- data/lib/bluepill/process_statistics.rb +2 -2
- data/lib/bluepill/socket.rb +60 -20
- data/lib/bluepill/system/command_setup.rb +98 -0
- data/lib/bluepill/system/commands.rb +219 -248
- data/lib/bluepill/system/daemon.rb +161 -0
- data/lib/bluepill/system/ps.rb +85 -0
- data/lib/bluepill/system/ps_table.rb +163 -0
- data/lib/bluepill/system.rb +45 -126
- data/lib/bluepill/trigger.rb +52 -26
- data/lib/bluepill/triggers/flapping.rb +7 -15
- data/lib/bluepill/units.rb +126 -0
- data/lib/bluepill/util/inflections.rb +34 -0
- data/lib/bluepill/util/levenshtein.rb +53 -0
- data/lib/bluepill/util/rotational_array.rb +1 -1
- data/lib/bluepill/version.rb +13 -12
- data/lib/bluepill.rb +33 -36
- data/sig/bluepill/application.rbs +257 -0
- data/sig/bluepill/checks.rbs +203 -0
- data/sig/bluepill/dsl.rbs +126 -0
- data/sig/bluepill/logger.rbs +104 -0
- data/sig/bluepill/process.rbs +542 -0
- data/sig/bluepill/system.rbs +316 -0
- data/sig/bluepill.rbs +47 -873
- metadata +39 -45
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
require "bluepill/process_commands/execution"
|
|
2
|
+
require "bluepill/process_commands/hooks"
|
|
3
|
+
require "bluepill/process_commands/stop_signals"
|
|
4
|
+
|
|
1
5
|
module Bluepill
|
|
2
6
|
# Starts, stops and restarts a Process with its configured commands
|
|
3
7
|
#
|
|
4
8
|
# @api private
|
|
5
9
|
module ProcessCommands
|
|
10
|
+
include Execution
|
|
11
|
+
include Hooks
|
|
12
|
+
include StopSignals
|
|
13
|
+
|
|
6
14
|
# Start the process with its start command
|
|
7
15
|
#
|
|
8
16
|
# Anything still running from an earlier start is killed first, and the
|
|
@@ -22,35 +30,50 @@ module Bluepill
|
|
|
22
30
|
# ProcessFactory makes sure every process has a start command
|
|
23
31
|
command = start_command #: String
|
|
24
32
|
logger.warning "Executing start command: #{command}"
|
|
33
|
+
run_start_command(command)
|
|
34
|
+
|
|
35
|
+
skip_ticks_for(start_grace_time)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Run the start command, as a daemon or for a self-daemonizing process
|
|
39
|
+
#
|
|
40
|
+
# A self-daemonizing process gets start_grace_time to finish, and then
|
|
41
|
+
# on_start_timeout is dispatched.
|
|
42
|
+
#
|
|
43
|
+
# @param [String] command the start command
|
|
44
|
+
#
|
|
45
|
+
# @return [void]
|
|
46
|
+
#
|
|
47
|
+
# @api private
|
|
48
|
+
def run_start_command(command)
|
|
25
49
|
if daemonize?
|
|
26
|
-
|
|
27
|
-
if daemon_id
|
|
28
|
-
ProcessJournal.append_pid_to_journal(name, daemon_id)
|
|
29
|
-
# Only refresh_children! fills children, and only when monitoring them
|
|
30
|
-
children.each do |child|
|
|
31
|
-
ProcessJournal.append_pid_to_journal(name, child.actual_pid)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
50
|
+
daemonize_process(command)
|
|
34
51
|
else
|
|
35
52
|
# This is a self-daemonizing process
|
|
36
53
|
with_timeout(start_grace_time, on_start_timeout) do
|
|
37
|
-
run_command(command,
|
|
54
|
+
run_command(command, "Start")
|
|
38
55
|
end
|
|
39
56
|
end
|
|
40
|
-
|
|
41
|
-
skip_ticks_for(start_grace_time)
|
|
42
57
|
end
|
|
43
58
|
|
|
44
|
-
# Run the
|
|
59
|
+
# Run the start command as a daemon, and add its pid to the journal
|
|
60
|
+
#
|
|
61
|
+
# The children's pids go in the journal too.
|
|
62
|
+
#
|
|
63
|
+
# @param [String] command the start command
|
|
45
64
|
#
|
|
46
65
|
# @return [void]
|
|
47
66
|
#
|
|
48
67
|
# @api private
|
|
49
|
-
def
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
68
|
+
def daemonize_process(command)
|
|
69
|
+
daemon_id = System.daemonize(command, system_command_options)
|
|
70
|
+
return unless daemon_id
|
|
71
|
+
|
|
72
|
+
ProcessJournal.append_pid_to_journal(name, daemon_id)
|
|
73
|
+
# Only refresh_children! fills children, and only when monitoring them
|
|
74
|
+
children.each do |child|
|
|
75
|
+
ProcessJournal.append_pid_to_journal(name, child.actual_pid)
|
|
76
|
+
end
|
|
54
77
|
end
|
|
55
78
|
|
|
56
79
|
# Stop the process with its stop command, stop signals, or TERM
|
|
@@ -63,202 +86,111 @@ module Bluepill
|
|
|
63
86
|
#
|
|
64
87
|
# @api private
|
|
65
88
|
def stop_process
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if stop_command
|
|
73
|
-
cmd = prepare_command(stop_command)
|
|
74
|
-
logger.warning "Executing stop command: #{cmd}"
|
|
75
|
-
|
|
76
|
-
with_timeout(stop_grace_time, 'stop') do
|
|
77
|
-
run_command(cmd, 'Stop')
|
|
78
|
-
end
|
|
79
|
-
cleanup_process
|
|
80
|
-
elsif stop_signals
|
|
81
|
-
# issue stop signals with configurable delay between each
|
|
82
|
-
logger.warning "Sending stop signals to #{actual_pid}"
|
|
83
|
-
@threads << Thread.new(stop_signals.clone) { |signals| send_stop_signals(signals) }
|
|
84
|
-
else
|
|
85
|
-
logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}"
|
|
86
|
-
signal_process('TERM')
|
|
87
|
-
cleanup_process
|
|
88
|
-
end
|
|
89
|
-
|
|
89
|
+
# The pid, for post_stop_command, before cleanup forgets it
|
|
90
|
+
@stopping_pid = actual_pid
|
|
91
|
+
pre_stop_process
|
|
92
|
+
journal_children if monitor_children
|
|
93
|
+
send_stop
|
|
90
94
|
skip_ticks_for(stop_grace_time)
|
|
91
95
|
end
|
|
92
96
|
|
|
93
|
-
#
|
|
97
|
+
# Add the pids of the process's children to its journal
|
|
94
98
|
#
|
|
95
|
-
# @return [
|
|
99
|
+
# @return [void]
|
|
96
100
|
#
|
|
97
101
|
# @api private
|
|
98
|
-
def
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
logger.warning "Executing restart command: #{cmd}"
|
|
103
|
-
|
|
104
|
-
with_timeout(restart_grace_time, 'restart') do
|
|
105
|
-
run_command(cmd, 'Restart')
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
skip_ticks_for(restart_grace_time)
|
|
109
|
-
else
|
|
110
|
-
logger.warning 'No restart_command specified. Must stop and start to restart'
|
|
111
|
-
stop_process
|
|
112
|
-
# Stop signals run in a thread. Wait for them, or start_process would
|
|
113
|
-
# kill the stopping process from the journal before its delays are up.
|
|
114
|
-
@threads.each(&:join)
|
|
115
|
-
start_process
|
|
102
|
+
def journal_children
|
|
103
|
+
System.get_children(actual_pid).each do |child_pid|
|
|
104
|
+
ProcessJournal.append_pid_to_journal(name, child_pid)
|
|
116
105
|
end
|
|
117
106
|
end
|
|
118
107
|
|
|
119
|
-
#
|
|
120
|
-
#
|
|
121
|
-
# Bluepill only writes the pid file for processes it daemonizes, but it
|
|
122
|
-
# removes it for every process, so a stopped process never leaves a stale
|
|
123
|
-
# pid behind for a later process to reuse. It forgets the pid it cached
|
|
124
|
-
# too, so a restart that stops and starts the process reads the new one.
|
|
108
|
+
# Run the stop command, send the stop signals, or send TERM
|
|
125
109
|
#
|
|
126
110
|
# @return [void]
|
|
127
111
|
#
|
|
128
112
|
# @api private
|
|
129
|
-
def
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
113
|
+
def send_stop
|
|
114
|
+
if stop_command
|
|
115
|
+
run_stop_command(stop_command)
|
|
116
|
+
elsif stop_signals
|
|
117
|
+
send_stop_signals_in_background(stop_signals)
|
|
118
|
+
else
|
|
119
|
+
logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}"
|
|
120
|
+
signal_process("TERM")
|
|
121
|
+
cleanup_process
|
|
122
|
+
end
|
|
133
123
|
end
|
|
134
124
|
|
|
135
|
-
#
|
|
136
|
-
#
|
|
137
|
-
# @param [String] command the stop or restart command
|
|
125
|
+
# Run the stop command, and clean up after the process
|
|
138
126
|
#
|
|
139
|
-
# @
|
|
127
|
+
# @param [String] command the stop command
|
|
140
128
|
#
|
|
141
|
-
# @
|
|
142
|
-
def prepare_command(command)
|
|
143
|
-
command.gsub('{{PID}}', actual_pid.to_s)
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
# Return the options the process's commands run with
|
|
147
|
-
#
|
|
148
|
-
# @return [Hash{Symbol => Object}] the uid, gid, working_dir, environment,
|
|
149
|
-
# pid_file, logger, stdin, stdout, stderr and supplementary_groups
|
|
129
|
+
# @return [void]
|
|
150
130
|
#
|
|
151
131
|
# @api private
|
|
152
|
-
def
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
gid: gid,
|
|
156
|
-
working_dir: working_dir,
|
|
157
|
-
environment: environment,
|
|
158
|
-
pid_file: pid_file,
|
|
159
|
-
logger: logger,
|
|
160
|
-
stdin: stdin,
|
|
161
|
-
stdout: stdout,
|
|
162
|
-
stderr: stderr,
|
|
163
|
-
supplementary_groups: supplementary_groups,
|
|
164
|
-
}
|
|
165
|
-
end
|
|
132
|
+
def run_stop_command(command)
|
|
133
|
+
cmd = prepare_command(command)
|
|
134
|
+
logger.warning "Executing stop command: #{cmd}"
|
|
166
135
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
#
|
|
172
|
-
# @yield the work to time
|
|
173
|
-
# @yieldparam [Numeric] secs the time limit
|
|
174
|
-
#
|
|
175
|
-
# @return [Object, Boolean] what the block returned, or whether the event's
|
|
176
|
-
# transition happened
|
|
177
|
-
#
|
|
178
|
-
# @api private
|
|
179
|
-
def with_timeout(secs, next_state, &blk)
|
|
180
|
-
Timeout.timeout(secs, &blk)
|
|
181
|
-
rescue Timeout::Error
|
|
182
|
-
logger.err 'Execution is taking longer than expected.'
|
|
183
|
-
logger.err 'Did you forget to tell bluepill to daemonize this process?'
|
|
184
|
-
dispatch!(next_state)
|
|
136
|
+
with_timeout(stop_grace_time, "stop") do
|
|
137
|
+
run_command(cmd, "Stop")
|
|
138
|
+
end
|
|
139
|
+
cleanup_process
|
|
185
140
|
end
|
|
186
141
|
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
# A command that fails, or that a signal kills, is logged as a warning.
|
|
190
|
-
#
|
|
191
|
-
# @param [String] cmd the command
|
|
192
|
-
# @param [String] description what the command is, for the log, such as
|
|
193
|
-
# Start
|
|
142
|
+
# Restart the process with its restart command, or by stopping and starting
|
|
194
143
|
#
|
|
195
|
-
# @return [
|
|
144
|
+
# @return [Integer] when ticks resume, in epoch seconds
|
|
196
145
|
#
|
|
197
146
|
# @api private
|
|
198
|
-
def
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
147
|
+
def restart_process
|
|
148
|
+
if restart_command
|
|
149
|
+
run_restart_command(restart_command)
|
|
150
|
+
else
|
|
151
|
+
logger.warning "No restart_command specified. Must stop and start to restart"
|
|
152
|
+
stop_process
|
|
153
|
+
# Stop signals run in a thread. Wait for them, or start_process would
|
|
154
|
+
# kill the stopping process from the journal before its delays are up.
|
|
155
|
+
@threads.each(&:join)
|
|
156
|
+
start_process
|
|
157
|
+
end
|
|
206
158
|
end
|
|
207
159
|
|
|
208
|
-
#
|
|
160
|
+
# Run the restart command, and skip ticks for restart_grace_time
|
|
209
161
|
#
|
|
210
|
-
#
|
|
211
|
-
# the process is still running. Then gives the last signal the rest of
|
|
212
|
-
# stop_grace_time to work before cleaning up.
|
|
162
|
+
# @param [String] command the restart command
|
|
213
163
|
#
|
|
214
|
-
# @
|
|
215
|
-
#
|
|
216
|
-
# @return [void]
|
|
164
|
+
# @return [Integer] when ticks resume, in epoch seconds
|
|
217
165
|
#
|
|
218
166
|
# @api private
|
|
219
|
-
def
|
|
220
|
-
|
|
221
|
-
signal = signals.shift
|
|
222
|
-
logger.info "Sending signal #{signal} to #{actual_pid}"
|
|
223
|
-
signal_process(signal) # send first signal
|
|
167
|
+
def run_restart_command(command)
|
|
168
|
+
cmd = prepare_command(command)
|
|
224
169
|
|
|
225
|
-
|
|
226
|
-
# we already checked to make sure signals had an odd number of items
|
|
227
|
-
delay = signals.shift #: Integer
|
|
228
|
-
signal = signals.shift
|
|
229
|
-
remaining -= delay
|
|
170
|
+
logger.warning "Executing restart command: #{cmd}"
|
|
230
171
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
# break unless signal_process(0) #break unless the process can be reached
|
|
234
|
-
unless signal_process(0)
|
|
235
|
-
logger.debug 'Process has terminated.'
|
|
236
|
-
return cleanup_process
|
|
237
|
-
end
|
|
238
|
-
logger.info "Sending signal #{signal} to #{actual_pid}"
|
|
239
|
-
signal_process(signal)
|
|
172
|
+
with_timeout(restart_grace_time, "restart") do
|
|
173
|
+
run_command(cmd, "Restart")
|
|
240
174
|
end
|
|
241
|
-
|
|
242
|
-
|
|
175
|
+
|
|
176
|
+
skip_ticks_for(restart_grace_time)
|
|
243
177
|
end
|
|
244
178
|
|
|
245
|
-
#
|
|
246
|
-
#
|
|
247
|
-
# It stops 2 seconds short of the grace time: ticks resume up to a second
|
|
248
|
-
# early (they count whole seconds) and can kill this thread, and the
|
|
249
|
-
# cleanup itself takes up to another second.
|
|
179
|
+
# Kill what's left of the process, and delete its pid file
|
|
250
180
|
#
|
|
251
|
-
#
|
|
181
|
+
# Bluepill only writes the pid file for processes it daemonizes, but it
|
|
182
|
+
# removes it for every process, so a stopped process never leaves a stale
|
|
183
|
+
# pid behind for a later process to reuse. It forgets the pid it cached
|
|
184
|
+
# too, so a restart that stops and starts the process reads the new one.
|
|
252
185
|
#
|
|
253
186
|
# @return [void]
|
|
254
187
|
#
|
|
255
188
|
# @api private
|
|
256
|
-
def
|
|
257
|
-
(
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
end
|
|
189
|
+
def cleanup_process
|
|
190
|
+
ProcessJournal.kill_all_from_journal(name) # finish cleanup
|
|
191
|
+
unlink_pid
|
|
192
|
+
clear_pid
|
|
193
|
+
post_stop_process
|
|
262
194
|
end
|
|
263
195
|
end
|
|
264
196
|
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
module ProcessConditions
|
|
3
|
+
# Checks that a value a process reads is below a limit
|
|
4
|
+
#
|
|
5
|
+
# The limit is the check's below: option. CpuUsage, MemUsage,
|
|
6
|
+
# RunningTime and FileTime build on it, and implement run to read the
|
|
7
|
+
# value.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
10
|
+
class BelowThreshold < ProcessCondition
|
|
11
|
+
# Create the check
|
|
12
|
+
#
|
|
13
|
+
# @param [Hash{Symbol => Object}] options the check's options, with
|
|
14
|
+
# :below, the limit
|
|
15
|
+
#
|
|
16
|
+
# @return [void]
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
19
|
+
def initialize(options = {})
|
|
20
|
+
super
|
|
21
|
+
@below = options[:below]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Return whether a value is below the limit
|
|
25
|
+
#
|
|
26
|
+
# @param [Numeric] value what run read
|
|
27
|
+
#
|
|
28
|
+
# @return [Boolean] true when it's below the limit
|
|
29
|
+
#
|
|
30
|
+
# @api private
|
|
31
|
+
def check?(value)
|
|
32
|
+
value < @below
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -10,20 +10,7 @@ module Bluepill
|
|
|
10
10
|
# process.checks :cpu_usage, every: 10.seconds, below: 5, times: 3
|
|
11
11
|
#
|
|
12
12
|
# @api private
|
|
13
|
-
class CpuUsage <
|
|
14
|
-
# Create the check
|
|
15
|
-
#
|
|
16
|
-
# @param [Hash{Symbol => Object}] options the check's options, with
|
|
17
|
-
# :below, the highest percentage that's fine
|
|
18
|
-
#
|
|
19
|
-
# @return [void]
|
|
20
|
-
#
|
|
21
|
-
# @api private
|
|
22
|
-
def initialize(options = {})
|
|
23
|
-
super
|
|
24
|
-
@below = options[:below]
|
|
25
|
-
end
|
|
26
|
-
|
|
13
|
+
class CpuUsage < BelowThreshold
|
|
27
14
|
# Read the process's CPU usage from the tick's ps table
|
|
28
15
|
#
|
|
29
16
|
# @param [Integer, nil] pid the process's pid
|
|
@@ -36,17 +23,6 @@ module Bluepill
|
|
|
36
23
|
# third col in the ps axu output
|
|
37
24
|
System.cpu_usage(pid, include_children).to_f
|
|
38
25
|
end
|
|
39
|
-
|
|
40
|
-
# Return whether the usage is below the limit
|
|
41
|
-
#
|
|
42
|
-
# @param [Float] value the percentage run read
|
|
43
|
-
#
|
|
44
|
-
# @return [Boolean] true when it's below the limit
|
|
45
|
-
#
|
|
46
|
-
# @api private
|
|
47
|
-
def check(value)
|
|
48
|
-
value < @below
|
|
49
|
-
end
|
|
50
26
|
end
|
|
51
27
|
end
|
|
52
28
|
end
|
|
@@ -10,7 +10,7 @@ module Bluepill
|
|
|
10
10
|
# process.checks :file_time, every: 60.seconds, below: 3.minutes, filename: "/tmp/some_file.log", times: 2
|
|
11
11
|
#
|
|
12
12
|
# @api private
|
|
13
|
-
class FileTime <
|
|
13
|
+
class FileTime < BelowThreshold
|
|
14
14
|
# Create the check
|
|
15
15
|
#
|
|
16
16
|
# @param [Hash{Symbol => Object}] options the check's options, with
|
|
@@ -22,7 +22,6 @@ module Bluepill
|
|
|
22
22
|
# @api private
|
|
23
23
|
def initialize(options = {})
|
|
24
24
|
super
|
|
25
|
-
@below = options[:below]
|
|
26
25
|
@filename = options[:filename]
|
|
27
26
|
end
|
|
28
27
|
|
|
@@ -37,7 +36,7 @@ module Bluepill
|
|
|
37
36
|
# @api private
|
|
38
37
|
def run(_pid, _include_children)
|
|
39
38
|
Time.now - File.mtime(@filename) if File.exist?(@filename)
|
|
40
|
-
rescue
|
|
39
|
+
rescue => e
|
|
41
40
|
e
|
|
42
41
|
end
|
|
43
42
|
|
|
@@ -49,8 +48,8 @@ module Bluepill
|
|
|
49
48
|
# for a missing file or an error
|
|
50
49
|
#
|
|
51
50
|
# @api private
|
|
52
|
-
def check(value)
|
|
53
|
-
value.is_a?(Numeric) &&
|
|
51
|
+
def check?(value)
|
|
52
|
+
value.is_a?(Numeric) && super
|
|
54
53
|
end
|
|
55
54
|
end
|
|
56
55
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "uri"
|
|
3
3
|
|
|
4
4
|
module Bluepill
|
|
5
5
|
module ProcessConditions
|
|
@@ -31,15 +31,7 @@ module Bluepill
|
|
|
31
31
|
def initialize(options = {})
|
|
32
32
|
super
|
|
33
33
|
@uri = URI.parse(options[:url]) #: URI::HTTP
|
|
34
|
-
@kind =
|
|
35
|
-
when Integer
|
|
36
|
-
Net::HTTPResponse::CODE_TO_OBJ.fetch(options.fetch(:kind).to_s)
|
|
37
|
-
when String, Symbol
|
|
38
|
-
Net.const_get("HTTP#{options.fetch(:kind).to_s.camelize}")
|
|
39
|
-
else
|
|
40
|
-
Net::HTTPSuccess
|
|
41
|
-
end
|
|
42
|
-
|
|
34
|
+
@kind = response_class(options[:kind])
|
|
43
35
|
@pattern = options[:pattern]
|
|
44
36
|
@open_timeout = (options[:open_timeout] || options[:timeout] || 5).to_i
|
|
45
37
|
@read_timeout = (options[:read_timeout] || options[:timeout] || 5).to_i
|
|
@@ -57,21 +49,13 @@ module Bluepill
|
|
|
57
49
|
#
|
|
58
50
|
# @api private
|
|
59
51
|
def run(_pid, _include_children)
|
|
60
|
-
|
|
61
|
-
session = Net::HTTP.new(host, @uri.port)
|
|
62
|
-
if @uri.scheme.eql?('https')
|
|
63
|
-
require 'net/https'
|
|
64
|
-
session.use_ssl = true
|
|
65
|
-
session.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
66
|
-
end
|
|
67
|
-
session.open_timeout = @open_timeout
|
|
68
|
-
session.read_timeout = @read_timeout
|
|
52
|
+
session = new_session
|
|
69
53
|
hide_net_http_bug do
|
|
70
54
|
session.start do |http|
|
|
71
55
|
http.get(@uri.request_uri)
|
|
72
56
|
end
|
|
73
57
|
end
|
|
74
|
-
rescue
|
|
58
|
+
rescue => e
|
|
75
59
|
e
|
|
76
60
|
end
|
|
77
61
|
|
|
@@ -83,15 +67,59 @@ module Bluepill
|
|
|
83
67
|
# expects, and its body matches the pattern if the check has one
|
|
84
68
|
#
|
|
85
69
|
# @api private
|
|
86
|
-
def check(value)
|
|
70
|
+
def check?(value)
|
|
87
71
|
return false unless value.is_a?(@kind)
|
|
88
|
-
return true
|
|
72
|
+
return true unless @pattern
|
|
89
73
|
|
|
90
74
|
response = value #: Net::HTTPResponse
|
|
91
75
|
!(@pattern =~ response.body).nil?
|
|
92
76
|
end
|
|
93
77
|
|
|
94
|
-
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# Return the Net::HTTPResponse class of a kind of response
|
|
81
|
+
#
|
|
82
|
+
# @param [Integer, String, Symbol, nil] kind a status code, such as
|
|
83
|
+
# 200, or a Net::HTTP class name, such as :success or :ok
|
|
84
|
+
#
|
|
85
|
+
# @return [Class] the response class, Net::HTTPSuccess when there's no
|
|
86
|
+
# kind
|
|
87
|
+
#
|
|
88
|
+
# @raise [KeyError] for a numeric kind that isn't an HTTP status
|
|
89
|
+
#
|
|
90
|
+
# @raise [NameError] for a kind that names no Net::HTTP response
|
|
91
|
+
#
|
|
92
|
+
# @api private
|
|
93
|
+
def response_class(kind)
|
|
94
|
+
case kind
|
|
95
|
+
when Integer
|
|
96
|
+
Net::HTTPResponse::CODE_TO_OBJ.fetch(kind.to_s)
|
|
97
|
+
when String, Symbol
|
|
98
|
+
Net.const_get("HTTP#{Util.camelize(kind)}")
|
|
99
|
+
else
|
|
100
|
+
Net::HTTPSuccess
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Return a session with the URL's host, set up to get the URL
|
|
105
|
+
#
|
|
106
|
+
# HTTPS is checked without verifying the certificate.
|
|
107
|
+
#
|
|
108
|
+
# @return [Net::HTTP] the session, not started yet
|
|
109
|
+
#
|
|
110
|
+
# @api private
|
|
111
|
+
def new_session
|
|
112
|
+
host = @uri.host #: String
|
|
113
|
+
session = Net::HTTP.new(host, @uri.port)
|
|
114
|
+
if @uri.scheme.eql?("https")
|
|
115
|
+
require "net/https"
|
|
116
|
+
session.use_ssl = true
|
|
117
|
+
session.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
118
|
+
end
|
|
119
|
+
session.open_timeout = @open_timeout
|
|
120
|
+
session.read_timeout = @read_timeout
|
|
121
|
+
session
|
|
122
|
+
end
|
|
95
123
|
|
|
96
124
|
# Run the block, raising a refused connection as one
|
|
97
125
|
#
|
|
@@ -10,39 +10,26 @@ module Bluepill
|
|
|
10
10
|
# process.checks :mem_usage, every: 10.seconds, below: 100.megabytes, times: [3, 5]
|
|
11
11
|
#
|
|
12
12
|
# @api private
|
|
13
|
-
class MemUsage <
|
|
13
|
+
class MemUsage < BelowThreshold
|
|
14
14
|
# The bytes in a megabyte, from which a value shows in megabytes
|
|
15
15
|
#
|
|
16
16
|
# @api private
|
|
17
|
-
MB
|
|
17
|
+
MB = (1024**2) #: Integer
|
|
18
18
|
|
|
19
19
|
# How a value shows in the history: a number and its unit
|
|
20
20
|
#
|
|
21
21
|
# @api private
|
|
22
|
-
FORMAT_STR =
|
|
22
|
+
FORMAT_STR = "%d%s".freeze
|
|
23
23
|
|
|
24
24
|
# The unit of a value that shows in megabytes
|
|
25
25
|
#
|
|
26
26
|
# @api private
|
|
27
|
-
MB_LABEL
|
|
27
|
+
MB_LABEL = "MB".freeze
|
|
28
28
|
|
|
29
29
|
# The unit of a value that shows in kilobytes
|
|
30
30
|
#
|
|
31
31
|
# @api private
|
|
32
|
-
KB_LABEL
|
|
33
|
-
|
|
34
|
-
# Create the check
|
|
35
|
-
#
|
|
36
|
-
# @param [Hash{Symbol => Object}] options the check's options, with
|
|
37
|
-
# :below, the most memory that's fine, in bytes
|
|
38
|
-
#
|
|
39
|
-
# @return [void]
|
|
40
|
-
#
|
|
41
|
-
# @api private
|
|
42
|
-
def initialize(options = {})
|
|
43
|
-
super
|
|
44
|
-
@below = options[:below]
|
|
45
|
-
end
|
|
32
|
+
KB_LABEL = "KB".freeze
|
|
46
33
|
|
|
47
34
|
# Read the process's memory usage from the tick's ps table
|
|
48
35
|
#
|
|
@@ -67,9 +54,9 @@ module Bluepill
|
|
|
67
54
|
# @return [Boolean] true when it's below the limit
|
|
68
55
|
#
|
|
69
56
|
# @api private
|
|
70
|
-
def check(value)
|
|
71
|
-
value
|
|
72
|
-
rescue
|
|
57
|
+
def check?(value)
|
|
58
|
+
value * 1024 < @below
|
|
59
|
+
rescue
|
|
73
60
|
true
|
|
74
61
|
end
|
|
75
62
|
|
|
@@ -81,7 +68,7 @@ module Bluepill
|
|
|
81
68
|
#
|
|
82
69
|
# @api private
|
|
83
70
|
def format_value(value)
|
|
84
|
-
if value
|
|
71
|
+
if value * 1024 >= MB
|
|
85
72
|
format(FORMAT_STR, (value / 1024).round, MB_LABEL)
|
|
86
73
|
else
|
|
87
74
|
format(FORMAT_STR, value, KB_LABEL)
|