bluepill 0.1.6 → 0.2.1
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 +61 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +106 -9
- 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 +56 -254
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/clock.rb +25 -0
- data/lib/bluepill/condition_watch.rb +136 -58
- 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 +60 -657
- 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 +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 +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 +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 +50 -871
- metadata +40 -45
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
module ProcessCommands
|
|
3
|
+
# Runs a Process's commands as its user, in its working_dir, with its
|
|
4
|
+
# environment and standard streams
|
|
5
|
+
#
|
|
6
|
+
# @api private
|
|
7
|
+
module Execution
|
|
8
|
+
# Return a command with <tt>{{PID}}</tt> replaced by the process's pid
|
|
9
|
+
#
|
|
10
|
+
# @param [String] command the stop or restart command
|
|
11
|
+
#
|
|
12
|
+
# @return [String]
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
def prepare_command(command)
|
|
16
|
+
substitute_pid(command, actual_pid)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Return a command with <tt>{{PID}}</tt> replaced by a pid
|
|
20
|
+
#
|
|
21
|
+
# @param [String] command the command
|
|
22
|
+
# @param [Integer, nil] pid the pid, or nil for an empty one
|
|
23
|
+
#
|
|
24
|
+
# @return [String]
|
|
25
|
+
#
|
|
26
|
+
# @api private
|
|
27
|
+
def substitute_pid(command, pid)
|
|
28
|
+
command.gsub("{{PID}}", pid.to_s)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Return the options the process's commands run with
|
|
32
|
+
#
|
|
33
|
+
# @return [Hash{Symbol => Object}] the uid, gid, working_dir, environment,
|
|
34
|
+
# pid_file, logger, stdin, stdout, stderr and supplementary_groups
|
|
35
|
+
#
|
|
36
|
+
# @api private
|
|
37
|
+
def system_command_options
|
|
38
|
+
{
|
|
39
|
+
uid: uid, gid: gid, supplementary_groups: supplementary_groups,
|
|
40
|
+
working_dir: working_dir, environment: environment, pid_file: pid_file,
|
|
41
|
+
logger: logger, stdin: stdin, stdout: stdout, stderr: stderr
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Run a block, and dispatch an event if it takes too long
|
|
46
|
+
#
|
|
47
|
+
# @param [Integer, Float] secs how long the block may take
|
|
48
|
+
# @param [String, Symbol] next_state the event to dispatch if it times out
|
|
49
|
+
#
|
|
50
|
+
# @yield the work to time
|
|
51
|
+
# @yieldparam [Numeric] secs the time limit
|
|
52
|
+
#
|
|
53
|
+
# @return [Object, Boolean] what the block returned, or whether the event's
|
|
54
|
+
# transition happened
|
|
55
|
+
#
|
|
56
|
+
# @api private
|
|
57
|
+
def with_timeout(secs, next_state, &blk)
|
|
58
|
+
Timeout.timeout(secs, &blk)
|
|
59
|
+
rescue Timeout::Error
|
|
60
|
+
logger.err "Execution is taking longer than expected."
|
|
61
|
+
logger.err "Did you forget to tell bluepill to daemonize this process?"
|
|
62
|
+
dispatch!(next_state)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Run a command, and wait for it to finish
|
|
66
|
+
#
|
|
67
|
+
# A command that fails, or that a signal kills, is logged as a warning.
|
|
68
|
+
#
|
|
69
|
+
# @param [String] cmd the command
|
|
70
|
+
# @param [String] description what the command is, for the log, such as
|
|
71
|
+
# Start
|
|
72
|
+
# @param [Hash{Symbol => Object}] options how to run it, as
|
|
73
|
+
# system_command_options returns
|
|
74
|
+
#
|
|
75
|
+
# @return [void]
|
|
76
|
+
#
|
|
77
|
+
# @api private
|
|
78
|
+
def run_command(cmd, description, options = system_command_options)
|
|
79
|
+
result = System.execute_blocking(cmd, options)
|
|
80
|
+
# The exit code is nil when a signal killed the command
|
|
81
|
+
exit_code = result.fetch(:exit_code)
|
|
82
|
+
return if exit_code && exit_code.zero?
|
|
83
|
+
|
|
84
|
+
logger.warning "#{description} command execution returned non-zero exit code:"
|
|
85
|
+
logger.warning result.inspect
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
module ProcessCommands
|
|
3
|
+
# Runs the commands a Process has around its starts and stops, and on
|
|
4
|
+
# each change of state
|
|
5
|
+
#
|
|
6
|
+
# Each runs for up to hook_timeout, and one that fails or times out is
|
|
7
|
+
# only logged.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
10
|
+
module Hooks
|
|
11
|
+
# Run the pre_start_command, if there is one, and wait for it
|
|
12
|
+
#
|
|
13
|
+
# The start waits for up to hook_timeout, and goes ahead either way.
|
|
14
|
+
# The process has no pid yet, so <tt>{{PID}}</tt> becomes nothing.
|
|
15
|
+
#
|
|
16
|
+
# @return [void]
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
19
|
+
def pre_start_process
|
|
20
|
+
command = pre_start_command
|
|
21
|
+
run_hook(command, "Pre start", nil) if command
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Run the pre_stop_command, if there is one, and wait for it
|
|
25
|
+
#
|
|
26
|
+
# The stop waits for up to hook_timeout, and goes ahead either way.
|
|
27
|
+
#
|
|
28
|
+
# @return [void]
|
|
29
|
+
#
|
|
30
|
+
# @api private
|
|
31
|
+
def pre_stop_process
|
|
32
|
+
command = pre_stop_command
|
|
33
|
+
run_hook(command, "Pre stop", stopping_pid) if command
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Run the post_stop_command in the background, if there is one
|
|
37
|
+
#
|
|
38
|
+
# @return [Thread, nil] the thread that runs it
|
|
39
|
+
#
|
|
40
|
+
# @api private
|
|
41
|
+
def post_stop_process
|
|
42
|
+
command = post_stop_command
|
|
43
|
+
run_hook_in_background(command, "Post stop", stopping_pid) if command
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Run the post_start_command in the background, if there is one
|
|
47
|
+
#
|
|
48
|
+
# @return [Thread, nil] the thread that runs it
|
|
49
|
+
#
|
|
50
|
+
# @api private
|
|
51
|
+
def post_start_process
|
|
52
|
+
command = post_start_command
|
|
53
|
+
run_hook_in_background(command, "Post start", actual_pid) if command
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Run the notify_command in the background, after a change of state
|
|
57
|
+
#
|
|
58
|
+
# The command gets the details in environment variables. A loopback, such
|
|
59
|
+
# as up to up, isn't a change, so it runs nothing.
|
|
60
|
+
#
|
|
61
|
+
# @param [#from_name, #to_name, #loopback?] transition the transition
|
|
62
|
+
#
|
|
63
|
+
# @return [Thread, nil] the thread that runs it
|
|
64
|
+
#
|
|
65
|
+
# @api private
|
|
66
|
+
def notify_transition(transition)
|
|
67
|
+
command = notify_command
|
|
68
|
+
return if command.nil? || transition.loopback?
|
|
69
|
+
|
|
70
|
+
pid = cached_pid
|
|
71
|
+
run_hook_in_background(command, "Notify", pid, notify_environment(transition, pid))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Return the environment variables notify_command gets for a transition
|
|
75
|
+
#
|
|
76
|
+
# @param [#from_name, #to_name] transition the transition
|
|
77
|
+
# @param [Integer, nil] pid the process's pid, or the last one it had
|
|
78
|
+
#
|
|
79
|
+
# @return [Hash{String => String}] BLUEPILL_APP, BLUEPILL_GROUP,
|
|
80
|
+
# BLUEPILL_PROCESS, BLUEPILL_FROM, BLUEPILL_TO, BLUEPILL_REASON and
|
|
81
|
+
# BLUEPILL_PID
|
|
82
|
+
#
|
|
83
|
+
# @api private
|
|
84
|
+
def notify_environment(transition, pid)
|
|
85
|
+
{
|
|
86
|
+
"BLUEPILL_APP" => app_name.to_s,
|
|
87
|
+
"BLUEPILL_GROUP" => group_name.to_s,
|
|
88
|
+
"BLUEPILL_PROCESS" => name.to_s,
|
|
89
|
+
"BLUEPILL_FROM" => transition.from_name.to_s,
|
|
90
|
+
"BLUEPILL_TO" => transition.to_name.to_s,
|
|
91
|
+
"BLUEPILL_REASON" => transition_reason.to_s,
|
|
92
|
+
"BLUEPILL_PID" => pid.to_s
|
|
93
|
+
}
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Run a hook command in a thread of its own
|
|
97
|
+
#
|
|
98
|
+
# @param [String] command the command
|
|
99
|
+
# @param [String] description what the hook is, for the log
|
|
100
|
+
# @param [Integer, nil] pid what <tt>{{PID}}</tt> in the command becomes
|
|
101
|
+
# @param [Hash{String => String}] env more environment variables
|
|
102
|
+
#
|
|
103
|
+
# @return [Thread] the thread that runs it
|
|
104
|
+
#
|
|
105
|
+
# @api private
|
|
106
|
+
def run_hook_in_background(command, description, pid, env = {})
|
|
107
|
+
Thread.new { run_hook(command, description, pid, env) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Run a hook command, and wait for it for up to hook_timeout
|
|
111
|
+
#
|
|
112
|
+
# The command runs like the process's other commands, with its
|
|
113
|
+
# working_dir, environment, uid and gid. A hook that runs longer than
|
|
114
|
+
# hook_timeout is killed, with anything it started. A hook that fails or
|
|
115
|
+
# times out is logged, and never changes what bluepill does with the
|
|
116
|
+
# process.
|
|
117
|
+
#
|
|
118
|
+
# @param [String] command the command
|
|
119
|
+
# @param [String] description what the hook is, for the log
|
|
120
|
+
# @param [Integer, nil] pid what <tt>{{PID}}</tt> in the command becomes
|
|
121
|
+
# @param [Hash{String => String}] env more environment variables
|
|
122
|
+
#
|
|
123
|
+
# @return [void]
|
|
124
|
+
#
|
|
125
|
+
# @api private
|
|
126
|
+
def run_hook(command, description, pid, env = {})
|
|
127
|
+
cmd = substitute_pid(command, pid)
|
|
128
|
+
logger.info "Executing #{description.downcase} command: #{cmd}"
|
|
129
|
+
run_command(cmd, description, hook_options(env))
|
|
130
|
+
rescue Timeout::Error
|
|
131
|
+
logger.warning "#{description} command took longer than #{hook_timeout} seconds: #{command}"
|
|
132
|
+
rescue => e
|
|
133
|
+
logger.err "#{description} command failed: #{e}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Return the options a hook command runs with
|
|
137
|
+
#
|
|
138
|
+
# They're the process's command options, with more environment variables
|
|
139
|
+
# and hook_timeout.
|
|
140
|
+
#
|
|
141
|
+
# @param [Hash{String => String}] env more environment variables
|
|
142
|
+
#
|
|
143
|
+
# @return [Hash{Symbol => Object}] the options, as system_command_options
|
|
144
|
+
# returns, with :timeout
|
|
145
|
+
#
|
|
146
|
+
# @api private
|
|
147
|
+
def hook_options(env)
|
|
148
|
+
options = system_command_options
|
|
149
|
+
options[:environment] = (environment || {}).merge(env)
|
|
150
|
+
options[:timeout] = hook_timeout
|
|
151
|
+
options
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
private
|
|
155
|
+
|
|
156
|
+
# Return the pid of the process being stopped
|
|
157
|
+
#
|
|
158
|
+
# stop_process reads it before cleanup forgets it, for the stop hooks.
|
|
159
|
+
#
|
|
160
|
+
# @return [Integer, nil]
|
|
161
|
+
#
|
|
162
|
+
# @api private
|
|
163
|
+
attr_reader :stopping_pid
|
|
164
|
+
|
|
165
|
+
# Return why the process is changing state, while dispatch! runs
|
|
166
|
+
#
|
|
167
|
+
# @return [String, nil] the reason, such as "user initiated", or nil
|
|
168
|
+
# when none was given
|
|
169
|
+
#
|
|
170
|
+
# @api private
|
|
171
|
+
attr_reader :transition_reason
|
|
172
|
+
|
|
173
|
+
# Return the process's pid, or the last one it had if it has died
|
|
174
|
+
#
|
|
175
|
+
# A process that has died has no pid by the time its state changes,
|
|
176
|
+
# but notify_command still gets the one it had.
|
|
177
|
+
#
|
|
178
|
+
# @return [Integer, nil]
|
|
179
|
+
#
|
|
180
|
+
# @api private
|
|
181
|
+
def cached_pid
|
|
182
|
+
@actual_pid || @last_pid
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
module ProcessCommands
|
|
3
|
+
# Stops a Process with its stop_signals: a signal, then a delay and a
|
|
4
|
+
# signal, and so on, until the process stops
|
|
5
|
+
#
|
|
6
|
+
# @api private
|
|
7
|
+
module StopSignals
|
|
8
|
+
# Send the stop signals from a thread of their own, with their delays
|
|
9
|
+
#
|
|
10
|
+
# @param [Array<Symbol, Integer>] signals signals with the delays between
|
|
11
|
+
#
|
|
12
|
+
# @return [Array<Thread>] the process's threads, with the new one
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
def send_stop_signals_in_background(signals)
|
|
16
|
+
# issue stop signals with configurable delay between each
|
|
17
|
+
logger.warning "Sending stop signals to #{actual_pid}"
|
|
18
|
+
@threads << Thread.new(signals.clone) { |copy| send_stop_signals(copy) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Send stop signals, with their delays, until the process stops
|
|
22
|
+
#
|
|
23
|
+
# Sends the first signal, then each later one after its delay, as long as
|
|
24
|
+
# the process is still running. Then gives the last signal the rest of
|
|
25
|
+
# stop_grace_time to work before cleaning up.
|
|
26
|
+
#
|
|
27
|
+
# @param [Array<Symbol, Integer>] signals signals with the delays between
|
|
28
|
+
#
|
|
29
|
+
# @return [void]
|
|
30
|
+
#
|
|
31
|
+
# @api private
|
|
32
|
+
def send_stop_signals(signals)
|
|
33
|
+
send_stop_signal(signals.shift) # send first signal
|
|
34
|
+
remaining = send_later_stop_signals(signals)
|
|
35
|
+
wait_for_stop(remaining) if remaining
|
|
36
|
+
cleanup_process
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Send each signal after the first, after its delay
|
|
40
|
+
#
|
|
41
|
+
# It stops once the process isn't running.
|
|
42
|
+
#
|
|
43
|
+
# @param [Array<Symbol, Integer>] signals the delays, each followed by
|
|
44
|
+
# its signal
|
|
45
|
+
#
|
|
46
|
+
# @return [Integer, Float, nil] how much of stop_grace_time is left, or
|
|
47
|
+
# nil if the process stopped
|
|
48
|
+
#
|
|
49
|
+
# @api private
|
|
50
|
+
def send_later_stop_signals(signals)
|
|
51
|
+
remaining = stop_grace_time
|
|
52
|
+
until signals.empty?
|
|
53
|
+
# we already checked to make sure signals had an odd number of items
|
|
54
|
+
delay = signals.shift #: Integer
|
|
55
|
+
signal = signals.shift
|
|
56
|
+
remaining -= delay
|
|
57
|
+
return unless running_after?(delay)
|
|
58
|
+
|
|
59
|
+
send_stop_signal(signal)
|
|
60
|
+
end
|
|
61
|
+
remaining
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Send one of the stop signals, and log it
|
|
65
|
+
#
|
|
66
|
+
# @param [Symbol, Integer] signal the signal
|
|
67
|
+
#
|
|
68
|
+
# @return [Boolean] whether the signal was sent
|
|
69
|
+
#
|
|
70
|
+
# @api private
|
|
71
|
+
def send_stop_signal(signal)
|
|
72
|
+
logger.info "Sending signal #{signal} to #{actual_pid}"
|
|
73
|
+
signal_process(signal)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Wait for a delay, and return whether the process is still running
|
|
77
|
+
#
|
|
78
|
+
# @param [Integer] delay how long to wait, in seconds
|
|
79
|
+
#
|
|
80
|
+
# @return [Boolean]
|
|
81
|
+
#
|
|
82
|
+
# @api private
|
|
83
|
+
def running_after?(delay)
|
|
84
|
+
logger.debug "Sleeping for #{delay} seconds"
|
|
85
|
+
# Kernel.sleep, not sleep, so the specs can stand in for the clock
|
|
86
|
+
Kernel.sleep(delay)
|
|
87
|
+
# break unless signal_process(0) #break unless the process can be reached
|
|
88
|
+
return true if signal_process(0)
|
|
89
|
+
|
|
90
|
+
logger.debug "Process has terminated."
|
|
91
|
+
false
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Wait for the process to exit, checking once a second
|
|
95
|
+
#
|
|
96
|
+
# It stops 2 seconds short of the grace time: ticks resume up to a second
|
|
97
|
+
# early (they count whole seconds) and can kill this thread, and the
|
|
98
|
+
# cleanup itself takes up to another second.
|
|
99
|
+
#
|
|
100
|
+
# @param [Integer, Float] seconds the grace time that's left
|
|
101
|
+
#
|
|
102
|
+
# @return [void]
|
|
103
|
+
#
|
|
104
|
+
# @api private
|
|
105
|
+
def wait_for_stop(seconds)
|
|
106
|
+
(seconds - 2).ceil.times do
|
|
107
|
+
break unless signal_process(0)
|
|
108
|
+
|
|
109
|
+
Kernel.sleep(1)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|