bluepill 0.1.7 → 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 +40 -21
- data/CONTRIBUTING.md +2 -2
- data/README.md +100 -20
- 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 +59 -259
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +129 -65
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -118
- 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 -152
- data/lib/bluepill/dsl/process_proxy.rb +100 -51
- data/lib/bluepill/dsl.rb +0 -23
- 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 +64 -667
- 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 +106 -174
- 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 +48 -45
- 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 -25
- data/lib/bluepill/triggers/flapping.rb +8 -16
- 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 +27 -35
- 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 -994
- metadata +39 -49
- data/lib/bluepill/clock.rb +0 -48
- data/lib/bluepill/deprecation.rb +0 -74
- data/lib/bluepill/trigger/custom.rb +0 -147
- data/lib/bluepill/util/log_error.rb +0 -29
|
@@ -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
|
|
@@ -11,7 +19,7 @@ module Bluepill
|
|
|
11
19
|
# and then on_start_timeout is dispatched. Ticks are skipped for
|
|
12
20
|
# start_grace_time either way.
|
|
13
21
|
#
|
|
14
|
-
# @return [Integer] when ticks resume,
|
|
22
|
+
# @return [Integer] when ticks resume, in epoch seconds
|
|
15
23
|
#
|
|
16
24
|
# @api private
|
|
17
25
|
def start_process
|
|
@@ -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
|
|
@@ -59,206 +82,115 @@ module Bluepill
|
|
|
59
82
|
# Stop signals are sent from a thread of their own, with their delays.
|
|
60
83
|
# Ticks are skipped for stop_grace_time.
|
|
61
84
|
#
|
|
62
|
-
# @return [Integer] when ticks resume,
|
|
85
|
+
# @return [Integer] when ticks resume, in epoch seconds
|
|
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
|
-
#
|
|
125
|
+
# Run the stop command, and clean up after the process
|
|
136
126
|
#
|
|
137
|
-
# @param [String] command the stop
|
|
127
|
+
# @param [String] command the stop command
|
|
138
128
|
#
|
|
139
|
-
# @return [
|
|
129
|
+
# @return [void]
|
|
140
130
|
#
|
|
141
131
|
# @api private
|
|
142
|
-
def
|
|
143
|
-
command
|
|
144
|
-
|
|
132
|
+
def run_stop_command(command)
|
|
133
|
+
cmd = prepare_command(command)
|
|
134
|
+
logger.warning "Executing stop command: #{cmd}"
|
|
145
135
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
#
|
|
151
|
-
# @api private
|
|
152
|
-
def system_command_options
|
|
153
|
-
{
|
|
154
|
-
uid: uid,
|
|
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
|
|
166
|
-
|
|
167
|
-
# Run a block, and dispatch an event if it takes too long
|
|
168
|
-
#
|
|
169
|
-
# @param [Integer, Float] secs how long the block may take
|
|
170
|
-
# @param [String, Symbol] next_state the event to dispatch if it times out
|
|
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
|
-
#
|
|
209
|
-
#
|
|
210
|
-
# Sends the first signal, then each later one after its delay, as long as
|
|
211
|
-
# the process is still running. Then gives the last signal the rest of
|
|
212
|
-
# stop_grace_time to work before cleaning up.
|
|
160
|
+
# Run the restart command, and skip ticks for restart_grace_time
|
|
213
161
|
#
|
|
214
|
-
# @param [
|
|
162
|
+
# @param [String] command the restart command
|
|
215
163
|
#
|
|
216
|
-
# @return [
|
|
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
|
|
@@ -9,23 +9,17 @@ module Bluepill
|
|
|
9
9
|
# response it should be (a Net::HTTP class name such as :success or
|
|
10
10
|
# :ok, or a status code such as 200, :success by default), pattern: a
|
|
11
11
|
# Regexp its body should match, and timeout: (or open_timeout: and
|
|
12
|
-
# read_timeout:) in seconds, 5 by default.
|
|
13
|
-
# https URL's certificate must be valid. Bluepill 0.1 doesn't verify it
|
|
14
|
-
# by default, but bluepill 0.3.0 does.
|
|
12
|
+
# read_timeout:) in seconds, 5 by default.
|
|
15
13
|
#
|
|
16
14
|
# @example
|
|
17
15
|
# process.checks :http, url: 'http://localhost:4567/', kind: :success, pattern: /html/, timeout: 3.seconds
|
|
18
16
|
#
|
|
19
|
-
# @example A server whose certificate must be valid
|
|
20
|
-
# process.checks :http, url: 'https://localhost:4443/', verify_ssl: true
|
|
21
|
-
#
|
|
22
17
|
# @api private
|
|
23
18
|
class Http < ProcessCondition
|
|
24
19
|
# Create the check
|
|
25
20
|
#
|
|
26
21
|
# @param [Hash{Symbol => Object}] options the check's options, with
|
|
27
|
-
# :url, :kind, :pattern, :timeout, :open_timeout
|
|
28
|
-
# :verify_ssl
|
|
22
|
+
# :url, :kind, :pattern, :timeout, :open_timeout and :read_timeout
|
|
29
23
|
#
|
|
30
24
|
# @return [void]
|
|
31
25
|
#
|
|
@@ -37,25 +31,15 @@ module Bluepill
|
|
|
37
31
|
def initialize(options = {})
|
|
38
32
|
super
|
|
39
33
|
@uri = URI.parse(options[:url]) #: URI::HTTP
|
|
40
|
-
@kind =
|
|
41
|
-
when Integer
|
|
42
|
-
Net::HTTPResponse::CODE_TO_OBJ.fetch(options.fetch(:kind).to_s)
|
|
43
|
-
when String, Symbol
|
|
44
|
-
Net.const_get("HTTP#{options.fetch(:kind).to_s.camelize}")
|
|
45
|
-
else
|
|
46
|
-
Net::HTTPSuccess
|
|
47
|
-
end
|
|
48
|
-
|
|
34
|
+
@kind = response_class(options[:kind])
|
|
49
35
|
@pattern = options[:pattern]
|
|
50
36
|
@open_timeout = (options[:open_timeout] || options[:timeout] || 5).to_i
|
|
51
37
|
@read_timeout = (options[:read_timeout] || options[:timeout] || 5).to_i
|
|
52
|
-
@verify_ssl = options[:verify_ssl]
|
|
53
|
-
warn_unverified unless options.key?(:verify_ssl)
|
|
54
38
|
end
|
|
55
39
|
|
|
56
40
|
# Get the URL
|
|
57
41
|
#
|
|
58
|
-
# HTTPS
|
|
42
|
+
# HTTPS is checked without verifying the certificate.
|
|
59
43
|
#
|
|
60
44
|
# @param [Integer, nil] _pid the process's pid
|
|
61
45
|
# @param [Boolean] _include_children whether to include its children
|
|
@@ -65,21 +49,13 @@ module Bluepill
|
|
|
65
49
|
#
|
|
66
50
|
# @api private
|
|
67
51
|
def run(_pid, _include_children)
|
|
68
|
-
|
|
69
|
-
session = Net::HTTP.new(host, @uri.port)
|
|
70
|
-
if @uri.scheme.eql?('https')
|
|
71
|
-
require 'net/https'
|
|
72
|
-
session.use_ssl = true
|
|
73
|
-
session.verify_mode = @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
|
|
74
|
-
end
|
|
75
|
-
session.open_timeout = @open_timeout
|
|
76
|
-
session.read_timeout = @read_timeout
|
|
52
|
+
session = new_session
|
|
77
53
|
hide_net_http_bug do
|
|
78
54
|
session.start do |http|
|
|
79
55
|
http.get(@uri.request_uri)
|
|
80
56
|
end
|
|
81
57
|
end
|
|
82
|
-
rescue
|
|
58
|
+
rescue => e
|
|
83
59
|
e
|
|
84
60
|
end
|
|
85
61
|
|
|
@@ -91,31 +67,58 @@ module Bluepill
|
|
|
91
67
|
# expects, and its body matches the pattern if the check has one
|
|
92
68
|
#
|
|
93
69
|
# @api private
|
|
94
|
-
def check(value)
|
|
70
|
+
def check?(value)
|
|
95
71
|
return false unless value.is_a?(@kind)
|
|
96
|
-
return true
|
|
72
|
+
return true unless @pattern
|
|
97
73
|
|
|
98
74
|
response = value #: Net::HTTPResponse
|
|
99
75
|
!(@pattern =~ response.body).nil?
|
|
100
76
|
end
|
|
101
77
|
|
|
102
|
-
|
|
78
|
+
private
|
|
103
79
|
|
|
104
|
-
#
|
|
80
|
+
# Return the Net::HTTPResponse class of a kind of response
|
|
105
81
|
#
|
|
106
|
-
#
|
|
107
|
-
#
|
|
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
|
|
108
84
|
#
|
|
109
|
-
# @return [
|
|
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
|
|
110
91
|
#
|
|
111
92
|
# @api private
|
|
112
|
-
def
|
|
113
|
-
|
|
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
|
|
114
103
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
122
|
end
|
|
120
123
|
|
|
121
124
|
# Run the block, raising a refused connection as one
|