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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -21
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +100 -20
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -259
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +129 -65
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -118
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -152
  20. data/lib/bluepill/dsl/process_proxy.rb +100 -51
  21. data/lib/bluepill/dsl.rb +0 -23
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +64 -667
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +48 -45
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -25
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +27 -35
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +47 -994
  74. metadata +39 -49
  75. data/lib/bluepill/clock.rb +0 -48
  76. data/lib/bluepill/deprecation.rb +0 -74
  77. data/lib/bluepill/trigger/custom.rb +0 -147
  78. 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, from Clock.now
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
- daemon_id = System.daemonize(command, system_command_options)
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, 'Start')
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 pre_start_command, if there is one
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 pre_start_process
50
- return unless pre_start_command
51
-
52
- logger.warning "Executing pre start command: #{pre_start_command}"
53
- run_command(pre_start_command, 'Pre start')
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, from Clock.now
85
+ # @return [Integer] when ticks resume, in epoch seconds
63
86
  #
64
87
  # @api private
65
88
  def stop_process
66
- if monitor_children
67
- System.get_children(actual_pid).each do |child_pid|
68
- ProcessJournal.append_pid_to_journal(name, child_pid)
69
- end
70
- end
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
- # Restart the process with its restart command, or by stopping and starting
97
+ # Add the pids of the process's children to its journal
94
98
  #
95
- # @return [Integer] when ticks resume, from Clock.now
99
+ # @return [void]
96
100
  #
97
101
  # @api private
98
- def restart_process
99
- if restart_command
100
- cmd = prepare_command(restart_command)
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
- # Kill what's left of the process, and delete its pid file
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 cleanup_process
130
- ProcessJournal.kill_all_from_journal(name) # finish cleanup
131
- unlink_pid
132
- clear_pid
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
- # Return a command with <tt>{{PID}}</tt> replaced by the process's pid
125
+ # Run the stop command, and clean up after the process
136
126
  #
137
- # @param [String] command the stop or restart command
127
+ # @param [String] command the stop command
138
128
  #
139
- # @return [String]
129
+ # @return [void]
140
130
  #
141
131
  # @api private
142
- def prepare_command(command)
143
- command.gsub('{{PID}}', actual_pid.to_s)
144
- end
132
+ def run_stop_command(command)
133
+ cmd = prepare_command(command)
134
+ logger.warning "Executing stop command: #{cmd}"
145
135
 
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
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
- # Run a command, and wait for it to finish
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 [void]
144
+ # @return [Integer] when ticks resume, in epoch seconds
196
145
  #
197
146
  # @api private
198
- def run_command(cmd, description)
199
- result = System.execute_blocking(cmd, system_command_options)
200
- # The exit code is nil when a signal killed the command
201
- exit_code = result.fetch(:exit_code)
202
- return if exit_code && exit_code.zero?
203
-
204
- logger.warning "#{description} command execution returned non-zero exit code:"
205
- logger.warning result.inspect
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
- # Send stop signals, with their delays, until the process stops
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 [Array<Symbol, Integer>] signals signals with the delays between
162
+ # @param [String] command the restart command
215
163
  #
216
- # @return [void]
164
+ # @return [Integer] when ticks resume, in epoch seconds
217
165
  #
218
166
  # @api private
219
- def send_stop_signals(signals)
220
- remaining = stop_grace_time
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
- until signals.empty?
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
- logger.debug "Sleeping for #{delay} seconds"
232
- sleep delay
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
- wait_for_stop(remaining)
242
- cleanup_process
175
+
176
+ skip_ticks_for(restart_grace_time)
243
177
  end
244
178
 
245
- # Wait for the process to exit, checking once a second
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
- # @param [Integer, Float] seconds the grace time that's left
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 wait_for_stop(seconds)
257
- (seconds - 2).ceil.times do
258
- break unless signal_process(0)
259
-
260
- sleep 1
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
@@ -23,7 +23,7 @@ module Bluepill
23
23
  # @return [Boolean] always true
24
24
  #
25
25
  # @api private
26
- def check(_value)
26
+ def check?(_value)
27
27
  true
28
28
  end
29
29
  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 < ProcessCondition
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 < ProcessCondition
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 StandardError => e
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) && value < @below
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 'net/http'
2
- require 'uri'
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. With verify_ssl: true, an
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, :read_timeout and
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 = case options[: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 verifies the server's certificate only with verify_ssl.
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
- host = @uri.host #: String
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 StandardError => e
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 unless @pattern
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
- private
78
+ private
103
79
 
104
- # Warn that bluepill 0.3.0 verifies an https URL's certificate
80
+ # Return the Net::HTTPResponse class of a kind of response
105
81
  #
106
- # It warns about a check of an https URL that doesn't set verify_ssl,
107
- # once for each URL.
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 [void]
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 warn_unverified
113
- return unless @uri.scheme.eql?('https')
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
- message = "bluepill 0.3.0 verifies the TLS certificate of the http check of #{@uri}, where 0.1 and 0.2 don't. " \
116
- 'Add verify_ssl: true to verify it now, or verify_ssl: false to keep checking a self-signed one.'
117
- # The message names the URL, so it's the key
118
- Deprecation.warn(message, message)
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