bluepill 0.2.7 → 0.3.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 +144 -1
- data/README.md +17 -10
- data/bin/bluepill +8 -2
- data/bluepill.gemspec +1 -1
- data/lib/bluepill/application/lifecycle.rb +43 -31
- data/lib/bluepill/application/listener.rb +1 -1
- data/lib/bluepill/application/previous_daemon.rb +14 -14
- data/lib/bluepill/application.rb +48 -28
- data/lib/bluepill/base_directory.rb +18 -20
- data/lib/bluepill/check_registry.rb +104 -0
- data/lib/bluepill/clock.rb +1 -3
- data/lib/bluepill/condition_watch.rb +61 -67
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +46 -38
- data/lib/bluepill/dsl/app_proxy.rb +69 -58
- data/lib/bluepill/dsl/attributes.rb +135 -0
- data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
- data/lib/bluepill/dsl/process_factory.rb +86 -70
- data/lib/bluepill/dsl/process_proxy.rb +61 -158
- data/lib/bluepill/dsl.rb +15 -6
- data/lib/bluepill/errors.rb +190 -0
- data/lib/bluepill/group.rb +14 -37
- data/lib/bluepill/logger.rb +55 -97
- data/lib/bluepill/process/attributes.rb +66 -32
- data/lib/bluepill/process/children.rb +4 -6
- data/lib/bluepill/process/pid.rb +41 -35
- data/lib/bluepill/process/state_machine.rb +15 -59
- data/lib/bluepill/process/stop_signals.rb +120 -0
- data/lib/bluepill/process/transition.rb +1 -3
- data/lib/bluepill/process/transition_callbacks.rb +2 -19
- data/lib/bluepill/process/triggers.rb +90 -0
- data/lib/bluepill/process/user_commands.rb +1 -1
- data/lib/bluepill/process/watches.rb +9 -23
- data/lib/bluepill/process.rb +70 -55
- data/lib/bluepill/process_commands/execution.rb +14 -22
- data/lib/bluepill/process_commands/hooks.rb +1 -31
- data/lib/bluepill/process_commands/stop_signals.rb +12 -17
- data/lib/bluepill/process_commands.rb +10 -8
- data/lib/bluepill/process_conditions/always_true.rb +2 -6
- data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
- data/lib/bluepill/process_conditions/file_time.rb +8 -9
- data/lib/bluepill/process_conditions/http.rb +28 -43
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +30 -32
- data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
- data/lib/bluepill/process_conditions.rb +5 -14
- data/lib/bluepill/process_journal/cleanup.rb +5 -53
- data/lib/bluepill/process_journal/config.rb +73 -0
- data/lib/bluepill/process_journal/entries.rb +128 -0
- data/lib/bluepill/process_journal/journal.rb +69 -0
- data/lib/bluepill/process_journal/kind.rb +60 -0
- data/lib/bluepill/process_journal/lock.rb +9 -4
- data/lib/bluepill/process_journal.rb +13 -324
- data/lib/bluepill/process_statistics.rb +3 -7
- data/lib/bluepill/socket.rb +19 -17
- data/lib/bluepill/system/command_options.rb +135 -0
- data/lib/bluepill/system/command_result.rb +73 -0
- data/lib/bluepill/system/command_setup.rb +3 -4
- data/lib/bluepill/system/commands.rb +21 -18
- data/lib/bluepill/system/daemon.rb +19 -14
- data/lib/bluepill/system/files.rb +35 -0
- data/lib/bluepill/system/ps.rb +20 -17
- data/lib/bluepill/system/ps_table.rb +7 -9
- data/lib/bluepill/system/usage.rb +155 -0
- data/lib/bluepill/system.rb +7 -192
- data/lib/bluepill/trigger.rb +22 -32
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/util/log_error.rb +28 -0
- data/lib/bluepill/version.rb +12 -22
- data/lib/bluepill.rb +1 -0
- data/sig/bluepill/application.rbs +17 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +43 -30
- data/sig/bluepill/dsl.rbs +98 -55
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +42 -304
- data/sig/bluepill/process_commands.rbs +235 -0
- data/sig/bluepill/stop_signals.rbs +21 -0
- data/sig/bluepill/system.rbs +154 -99
- data/sig/bluepill/version.rbs +17 -0
- data/sig/bluepill.rbs +4 -17
- data/sig/manifest.yaml +1 -0
- metadata +22 -3
- data/lib/bluepill/dsl/process_factory/children.rb +0 -73
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
class Process
|
|
5
|
+
# The triggers a Process tells of its transitions, such as Flapping
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
class Triggers
|
|
9
|
+
include Enumerable
|
|
10
|
+
|
|
11
|
+
# Return the logger the triggers write to
|
|
12
|
+
#
|
|
13
|
+
# @return [Logger, nil]
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
16
|
+
attr_reader :logger
|
|
17
|
+
|
|
18
|
+
# Create an empty set of triggers, which write to a logger
|
|
19
|
+
#
|
|
20
|
+
# @param [Logger, nil] logger the process's logger, which each trigger
|
|
21
|
+
# added gets too
|
|
22
|
+
#
|
|
23
|
+
# @return [void]
|
|
24
|
+
#
|
|
25
|
+
# @api private
|
|
26
|
+
def initialize(logger:)
|
|
27
|
+
@logger = logger
|
|
28
|
+
@triggers = []
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Add a trigger of a class, for a process, with the triggers' logger
|
|
32
|
+
#
|
|
33
|
+
# @param [Class] klass the Trigger subclass, such as Triggers::Flapping
|
|
34
|
+
# @param [Process] process the process it watches
|
|
35
|
+
# @param [Hash{Symbol => Object}] options the trigger's options
|
|
36
|
+
#
|
|
37
|
+
# @return [Triggers] the triggers
|
|
38
|
+
#
|
|
39
|
+
# @api private
|
|
40
|
+
def add(klass, process, **options) = self << klass.new(process, **options, logger: logger)
|
|
41
|
+
|
|
42
|
+
# Add a trigger
|
|
43
|
+
#
|
|
44
|
+
# @param [Trigger] trigger the trigger
|
|
45
|
+
#
|
|
46
|
+
# @return [Triggers] the triggers
|
|
47
|
+
#
|
|
48
|
+
# @api private
|
|
49
|
+
def <<(trigger)
|
|
50
|
+
@triggers << trigger
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Yield each trigger
|
|
55
|
+
#
|
|
56
|
+
# @yieldparam [Trigger] trigger a trigger
|
|
57
|
+
#
|
|
58
|
+
# @return [Array<Trigger>] the triggers
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
def each(&block) = @triggers.each(&block)
|
|
62
|
+
|
|
63
|
+
# Tell every trigger about a transition, before it happens
|
|
64
|
+
#
|
|
65
|
+
# A trigger can throw Trigger::HALT to stop the transition. An error a
|
|
66
|
+
# trigger raises stops it too, and Process#tick logs it.
|
|
67
|
+
#
|
|
68
|
+
# @param [#from_name, #to_name, #loopback?] transition the transition
|
|
69
|
+
#
|
|
70
|
+
# @return [Array<Trigger>] the triggers
|
|
71
|
+
#
|
|
72
|
+
# @raise [StandardError] what a trigger raised
|
|
73
|
+
#
|
|
74
|
+
# @api private
|
|
75
|
+
def notify(transition)
|
|
76
|
+
# @triggers and not each, to save a call on every tick
|
|
77
|
+
@triggers.each { |trigger| trigger.notify(transition) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Cancel the events every trigger scheduled
|
|
81
|
+
#
|
|
82
|
+
# The process does this when the user unmonitors it.
|
|
83
|
+
#
|
|
84
|
+
# @return [Array<Trigger>] the triggers
|
|
85
|
+
#
|
|
86
|
+
# @api private
|
|
87
|
+
def reset! = each(&:reset!)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -25,12 +25,16 @@ module Bluepill
|
|
|
25
25
|
# @api private
|
|
26
26
|
attr_reader :logger
|
|
27
27
|
|
|
28
|
-
# Create an empty set of watches
|
|
28
|
+
# Create an empty set of watches, which write to a logger
|
|
29
|
+
#
|
|
30
|
+
# @param [Logger, nil] logger the process's logger, which each watch
|
|
31
|
+
# added gets too
|
|
29
32
|
#
|
|
30
33
|
# @return [void]
|
|
31
34
|
#
|
|
32
35
|
# @api private
|
|
33
|
-
def initialize
|
|
36
|
+
def initialize(logger:)
|
|
37
|
+
@logger = logger
|
|
34
38
|
@watches = []
|
|
35
39
|
end
|
|
36
40
|
|
|
@@ -42,9 +46,7 @@ module Bluepill
|
|
|
42
46
|
# @return [Watches] the watches
|
|
43
47
|
#
|
|
44
48
|
# @api private
|
|
45
|
-
def add(name, options)
|
|
46
|
-
self << ConditionWatch.new(name, options.merge(logger: logger))
|
|
47
|
-
end
|
|
49
|
+
def add(name, **options) = self << ConditionWatch.new(name, **options, logger: logger)
|
|
48
50
|
|
|
49
51
|
# Add a watch
|
|
50
52
|
#
|
|
@@ -65,30 +67,14 @@ module Bluepill
|
|
|
65
67
|
# @return [Array<ConditionWatch>] the watches
|
|
66
68
|
#
|
|
67
69
|
# @api private
|
|
68
|
-
def each(&block)
|
|
69
|
-
@watches.each(&block)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# Set the logger, for the watches and for any added later
|
|
73
|
-
#
|
|
74
|
-
# @param [Logger] logger the logger to use
|
|
75
|
-
#
|
|
76
|
-
# @return [void]
|
|
77
|
-
#
|
|
78
|
-
# @api private
|
|
79
|
-
def logger=(logger)
|
|
80
|
-
@logger = logger
|
|
81
|
-
each { |watch| watch.logger = logger }
|
|
82
|
-
end
|
|
70
|
+
def each(&block) = @watches.each(&block)
|
|
83
71
|
|
|
84
72
|
# Forget what the watches have read, as when the process changes state
|
|
85
73
|
#
|
|
86
74
|
# @return [void]
|
|
87
75
|
#
|
|
88
76
|
# @api private
|
|
89
|
-
def clear_history!
|
|
90
|
-
each(&:clear_history!)
|
|
91
|
-
end
|
|
77
|
+
def clear_history! = each(&:clear_history!)
|
|
92
78
|
|
|
93
79
|
# Run every watch that's due, and return the events they fire
|
|
94
80
|
#
|
data/lib/bluepill/process.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "bluepill/system"
|
|
4
|
+
require "bluepill/check_registry"
|
|
5
|
+
require "bluepill/util/log_error"
|
|
4
6
|
require "bluepill/process_journal"
|
|
5
7
|
require "bluepill/process_commands"
|
|
6
8
|
require "bluepill/process/attributes"
|
|
@@ -10,6 +12,7 @@ require "bluepill/process/transition"
|
|
|
10
12
|
require "bluepill/process/state_machine"
|
|
11
13
|
require "bluepill/process/transition_callbacks"
|
|
12
14
|
require "bluepill/process/watches"
|
|
15
|
+
require "bluepill/process/triggers"
|
|
13
16
|
require "bluepill/process/user_commands"
|
|
14
17
|
|
|
15
18
|
module Bluepill
|
|
@@ -45,10 +48,10 @@ module Bluepill
|
|
|
45
48
|
|
|
46
49
|
# Return the triggers notified of the process's transitions
|
|
47
50
|
#
|
|
48
|
-
# @return [
|
|
51
|
+
# @return [Triggers]
|
|
49
52
|
#
|
|
50
53
|
# @api private
|
|
51
|
-
|
|
54
|
+
attr_reader :triggers
|
|
52
55
|
|
|
53
56
|
# Return the time, from Clock.now, before which ticks are skipped
|
|
54
57
|
#
|
|
@@ -99,7 +102,10 @@ module Bluepill
|
|
|
99
102
|
# @api private
|
|
100
103
|
attr_reader :statistics
|
|
101
104
|
|
|
102
|
-
# Return the logger, prefixed with the process's
|
|
105
|
+
# Return the logger, prefixed with the process's names
|
|
106
|
+
#
|
|
107
|
+
# The process gets it when it's made, prefixed with the names of its app,
|
|
108
|
+
# its group and itself, and gives it to its watches and triggers.
|
|
103
109
|
#
|
|
104
110
|
# @return [Logger]
|
|
105
111
|
#
|
|
@@ -113,24 +119,29 @@ module Bluepill
|
|
|
113
119
|
# and children left alone.
|
|
114
120
|
#
|
|
115
121
|
# @param [String] process_name the process's name
|
|
116
|
-
# @param [Hash{Symbol => Hash{Symbol => Object}}
|
|
117
|
-
# check name
|
|
118
|
-
#
|
|
119
|
-
#
|
|
122
|
+
# @param [Hash{Symbol => Hash{Symbol => Object}}, Array<Array(Symbol, Hash{Symbol => Object})>]
|
|
123
|
+
# checks each check's name and options, as a Hash, or as a list of
|
|
124
|
+
# pairs, which can name a check twice
|
|
125
|
+
# @param [Logger, nil] logger the process's logger, already prefixed with
|
|
126
|
+
# its names, which its watches and triggers get too
|
|
127
|
+
# @param [Integer, nil] actual_pid the pid of a child process that's
|
|
128
|
+
# already running
|
|
129
|
+
# @param [Hash{Symbol => Object}] attributes the process's attributes,
|
|
130
|
+
# of which those in CONFIGURABLE_ATTRIBUTES are set
|
|
120
131
|
#
|
|
121
132
|
# @return [void]
|
|
122
133
|
#
|
|
123
134
|
# @api private
|
|
124
|
-
def initialize(process_name, checks,
|
|
135
|
+
def initialize(process_name, checks, logger: nil, actual_pid: nil, **attributes)
|
|
125
136
|
@name = process_name
|
|
126
137
|
@event_mutex = Monitor.new
|
|
127
138
|
@children = []
|
|
128
139
|
@threads = []
|
|
129
140
|
@statistics = ProcessStatistics.new
|
|
130
|
-
@actual_pid =
|
|
131
|
-
add_checks(checks,
|
|
141
|
+
@actual_pid = actual_pid
|
|
142
|
+
add_checks(checks, logger)
|
|
132
143
|
@state = :unmonitored
|
|
133
|
-
configure(
|
|
144
|
+
configure(attributes)
|
|
134
145
|
end
|
|
135
146
|
|
|
136
147
|
# Check on the process once a tick
|
|
@@ -138,52 +149,54 @@ module Bluepill
|
|
|
138
149
|
# Unless ticks are being skipped, fire the state machine's tick event, and
|
|
139
150
|
# then, for a process that's up, run its watches and tick its children.
|
|
140
151
|
#
|
|
152
|
+
# An error, such as one a trigger raised, is logged with its backtrace,
|
|
153
|
+
# and the process ticks again next time, so one broken check or trigger
|
|
154
|
+
# doesn't stop the daemon.
|
|
155
|
+
#
|
|
141
156
|
# @return [void]
|
|
142
157
|
#
|
|
143
158
|
# @api private
|
|
144
159
|
def tick
|
|
145
160
|
return if skipping_ticks?
|
|
146
161
|
|
|
147
|
-
|
|
148
|
-
|
|
162
|
+
@skip_ticks_until = nil
|
|
149
163
|
# clear the memoization per tick
|
|
150
164
|
@process_running = nil
|
|
151
|
-
|
|
152
165
|
# Deal with thread cleanup here since the stopping state isn't used
|
|
153
166
|
clean_threads if unmonitored?
|
|
154
|
-
|
|
155
167
|
fire_event(:tick)
|
|
156
|
-
|
|
157
168
|
return unless up?
|
|
158
169
|
|
|
159
170
|
run_watches
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
refresh_children!
|
|
164
|
-
children.each(&:tick)
|
|
171
|
+
tick_children if monitor_children?
|
|
172
|
+
rescue => e
|
|
173
|
+
Util.log_error(logger, "tick failed", e)
|
|
165
174
|
end
|
|
166
175
|
|
|
167
|
-
#
|
|
176
|
+
# Find the process's children, and tick each of them
|
|
168
177
|
#
|
|
169
|
-
#
|
|
178
|
+
# Each child's tick logs its own errors, so one that fails doesn't keep
|
|
179
|
+
# the others from ticking.
|
|
170
180
|
#
|
|
171
181
|
# @return [void]
|
|
172
182
|
#
|
|
173
183
|
# @api private
|
|
174
|
-
def
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
triggers.each { |t| t.logger = logger }
|
|
184
|
+
def tick_children
|
|
185
|
+
refresh_children!
|
|
186
|
+
children.each(&:tick)
|
|
178
187
|
end
|
|
179
188
|
|
|
180
189
|
# Record an event and fire it on the state machine
|
|
181
190
|
#
|
|
182
|
-
# @param [Symbol
|
|
191
|
+
# @param [Symbol] event the event, one of
|
|
192
|
+
# StateMachine::TRANSITIONS, such as :start or :restart
|
|
183
193
|
# @param [String, nil] reason why, for the process's event history
|
|
184
194
|
#
|
|
185
195
|
# @return [Boolean] whether the state machine made the transition
|
|
186
196
|
#
|
|
197
|
+
# @raise [KeyError] for an event that isn't one of
|
|
198
|
+
# StateMachine::TRANSITIONS, after recording it
|
|
199
|
+
#
|
|
187
200
|
# @api private
|
|
188
201
|
def dispatch!(event, reason = nil)
|
|
189
202
|
@event_mutex.synchronize do
|
|
@@ -191,7 +204,7 @@ module Bluepill
|
|
|
191
204
|
# The reason, for notify_command
|
|
192
205
|
@transition_reason = reason
|
|
193
206
|
begin
|
|
194
|
-
|
|
207
|
+
fire_event(event)
|
|
195
208
|
ensure
|
|
196
209
|
@transition_reason = nil
|
|
197
210
|
end
|
|
@@ -200,39 +213,45 @@ module Bluepill
|
|
|
200
213
|
|
|
201
214
|
# Set the logger, and add each check
|
|
202
215
|
#
|
|
203
|
-
# A check is a trigger if
|
|
204
|
-
# gets the logger.
|
|
216
|
+
# A check is a trigger if CheckRegistry finds a Trigger by its name, and
|
|
217
|
+
# a watch otherwise. Each gets the logger, which the process keeps.
|
|
205
218
|
#
|
|
206
|
-
# @param [Hash{Symbol => Hash{Symbol => Object}}
|
|
207
|
-
# check name
|
|
219
|
+
# @param [Hash{Symbol => Hash{Symbol => Object}}, Array<Array(Symbol, Hash{Symbol => Object})>]
|
|
220
|
+
# checks each check's name and options, as a Hash, or as a list of
|
|
221
|
+
# pairs, which can name a check twice
|
|
208
222
|
# @param [Logger] logger the logger to use
|
|
209
223
|
#
|
|
210
224
|
# @return [void]
|
|
211
225
|
#
|
|
226
|
+
# @raise [ConfigError] for a name that no check or trigger has
|
|
227
|
+
#
|
|
212
228
|
# @api private
|
|
213
229
|
def add_checks(checks, logger)
|
|
214
|
-
@
|
|
215
|
-
@
|
|
216
|
-
|
|
217
|
-
checks.each
|
|
218
|
-
if Trigger[name]
|
|
219
|
-
add_trigger(name, opts)
|
|
220
|
-
else
|
|
221
|
-
watches.add(name, opts)
|
|
222
|
-
end
|
|
223
|
-
end
|
|
230
|
+
@logger = logger
|
|
231
|
+
@watches = Watches.new(logger: logger)
|
|
232
|
+
@triggers = Triggers.new(logger: logger)
|
|
233
|
+
checks.each { |name, options| add_check(name, options) }
|
|
224
234
|
end
|
|
225
235
|
|
|
226
|
-
# Add a
|
|
236
|
+
# Add a check, as a trigger or as a watch
|
|
227
237
|
#
|
|
228
|
-
#
|
|
229
|
-
# @param [Hash{Symbol => Object}] options the trigger's options
|
|
238
|
+
# It's a trigger if CheckRegistry finds a Trigger by its name.
|
|
230
239
|
#
|
|
231
|
-
# @
|
|
240
|
+
# @param [Symbol] name the check's name, such as :mem_usage
|
|
241
|
+
# @param [Hash{Symbol => Object}] options the check's options
|
|
242
|
+
#
|
|
243
|
+
# @return [void]
|
|
244
|
+
#
|
|
245
|
+
# @raise [ConfigError] for a name that no check or trigger has
|
|
232
246
|
#
|
|
233
247
|
# @api private
|
|
234
|
-
def
|
|
235
|
-
|
|
248
|
+
def add_check(name, options)
|
|
249
|
+
check = CheckRegistry.fetch(name)
|
|
250
|
+
if check < Trigger
|
|
251
|
+
triggers.add(check, self, **options)
|
|
252
|
+
else
|
|
253
|
+
watches.add(name, **options)
|
|
254
|
+
end
|
|
236
255
|
end
|
|
237
256
|
|
|
238
257
|
# Run every watch that's due, and dispatch the events they fire
|
|
@@ -270,9 +289,7 @@ module Bluepill
|
|
|
270
289
|
# @return [Boolean]
|
|
271
290
|
#
|
|
272
291
|
# @api private
|
|
273
|
-
def daemonize?
|
|
274
|
-
!!daemonize
|
|
275
|
-
end
|
|
292
|
+
def daemonize? = !!daemonize
|
|
276
293
|
|
|
277
294
|
# Skip ticks for a while, such as for a command's grace time
|
|
278
295
|
#
|
|
@@ -294,8 +311,6 @@ module Bluepill
|
|
|
294
311
|
# @return [Boolean, nil]
|
|
295
312
|
#
|
|
296
313
|
# @api private
|
|
297
|
-
def skipping_ticks?
|
|
298
|
-
skip_ticks_until && skip_ticks_until > Clock.now
|
|
299
|
-
end
|
|
314
|
+
def skipping_ticks? = skip_ticks_until && skip_ticks_until > Clock.now
|
|
300
315
|
end
|
|
301
316
|
end
|
|
@@ -14,9 +14,7 @@ module Bluepill
|
|
|
14
14
|
# @return [String]
|
|
15
15
|
#
|
|
16
16
|
# @api private
|
|
17
|
-
def prepare_command(command)
|
|
18
|
-
substitute_pid(command, actual_pid)
|
|
19
|
-
end
|
|
17
|
+
def prepare_command(command) = substitute_pid(command, actual_pid)
|
|
20
18
|
|
|
21
19
|
# Return a command with <tt>{{PID}}</tt> replaced by a pid
|
|
22
20
|
#
|
|
@@ -26,28 +24,25 @@ module Bluepill
|
|
|
26
24
|
# @return [String]
|
|
27
25
|
#
|
|
28
26
|
# @api private
|
|
29
|
-
def substitute_pid(command, pid)
|
|
30
|
-
command.gsub("{{PID}}", pid.to_s)
|
|
31
|
-
end
|
|
27
|
+
def substitute_pid(command, pid) = command.gsub("{{PID}}", pid.to_s)
|
|
32
28
|
|
|
33
29
|
# Return the options the process's commands run with
|
|
34
30
|
#
|
|
35
|
-
# @return [
|
|
36
|
-
#
|
|
31
|
+
# @return [System::CommandOptions] its uid, gid, supplementary_groups,
|
|
32
|
+
# working_dir, environment, stdin, stdout and stderr
|
|
37
33
|
#
|
|
38
34
|
# @api private
|
|
39
|
-
def
|
|
40
|
-
|
|
41
|
-
uid: uid, gid: gid, supplementary_groups: supplementary_groups,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
35
|
+
def command_options
|
|
36
|
+
System::CommandOptions.new(
|
|
37
|
+
uid: uid, gid: gid, supplementary_groups: supplementary_groups, working_dir: working_dir,
|
|
38
|
+
environment: environment, stdin: stdin, stdout: stdout, stderr: stderr
|
|
39
|
+
)
|
|
45
40
|
end
|
|
46
41
|
|
|
47
42
|
# Run a block, and dispatch an event if it takes too long
|
|
48
43
|
#
|
|
49
44
|
# @param [Integer, Float] secs how long the block may take
|
|
50
|
-
# @param [
|
|
45
|
+
# @param [Symbol] next_state the event to dispatch if it times out
|
|
51
46
|
#
|
|
52
47
|
# @yield the work to time
|
|
53
48
|
# @yieldparam [Numeric] secs the time limit
|
|
@@ -71,20 +66,17 @@ module Bluepill
|
|
|
71
66
|
# @param [String] cmd the command
|
|
72
67
|
# @param [String] description what the command is, for the log, such as
|
|
73
68
|
# Start
|
|
74
|
-
# @param [
|
|
75
|
-
# system_command_options returns
|
|
69
|
+
# @param [System::CommandOptions] options how to run it
|
|
76
70
|
#
|
|
77
71
|
# @return [void]
|
|
78
72
|
#
|
|
79
73
|
# @api private
|
|
80
|
-
def run_command(cmd, description, options =
|
|
74
|
+
def run_command(cmd, description, options = command_options)
|
|
81
75
|
result = System.execute_blocking(cmd, options)
|
|
82
|
-
|
|
83
|
-
exit_code = result.fetch(:exit_code)
|
|
84
|
-
return if exit_code&.zero?
|
|
76
|
+
return if result.success?
|
|
85
77
|
|
|
86
78
|
logger.warning "#{description} command execution returned non-zero exit code:"
|
|
87
|
-
logger.warning result.inspect
|
|
79
|
+
logger.warning result.to_h.inspect
|
|
88
80
|
end
|
|
89
81
|
end
|
|
90
82
|
end
|
|
@@ -128,31 +128,13 @@ module Bluepill
|
|
|
128
128
|
def run_hook(command, description, pid, env = {})
|
|
129
129
|
cmd = substitute_pid(command, pid)
|
|
130
130
|
logger.info "Executing #{description.downcase} command: #{cmd}"
|
|
131
|
-
run_command(cmd, description,
|
|
131
|
+
run_command(cmd, description, command_options.for_hook(env, hook_timeout))
|
|
132
132
|
rescue Timeout::Error
|
|
133
133
|
logger.warning "#{description} command took longer than #{hook_timeout} seconds: #{command}"
|
|
134
134
|
rescue => e
|
|
135
135
|
logger.err "#{description} command failed: #{e}"
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
-
# Return the options a hook command runs with
|
|
139
|
-
#
|
|
140
|
-
# They're the process's command options, with more environment variables
|
|
141
|
-
# and hook_timeout.
|
|
142
|
-
#
|
|
143
|
-
# @param [Hash{String => String}] env more environment variables
|
|
144
|
-
#
|
|
145
|
-
# @return [Hash{Symbol => Object}] the options, as system_command_options
|
|
146
|
-
# returns, with :timeout
|
|
147
|
-
#
|
|
148
|
-
# @api private
|
|
149
|
-
def hook_options(env)
|
|
150
|
-
options = system_command_options
|
|
151
|
-
options[:environment] = (environment || {}).merge(env)
|
|
152
|
-
options[:timeout] = hook_timeout
|
|
153
|
-
options
|
|
154
|
-
end
|
|
155
|
-
|
|
156
138
|
private
|
|
157
139
|
|
|
158
140
|
# Return the pid of the process being stopped
|
|
@@ -171,18 +153,6 @@ module Bluepill
|
|
|
171
153
|
#
|
|
172
154
|
# @api private
|
|
173
155
|
attr_reader :transition_reason
|
|
174
|
-
|
|
175
|
-
# Return the process's pid, or the last one it had if it has died
|
|
176
|
-
#
|
|
177
|
-
# A process that has died has no pid by the time its state changes,
|
|
178
|
-
# but notify_command still gets the one it had.
|
|
179
|
-
#
|
|
180
|
-
# @return [Integer, nil]
|
|
181
|
-
#
|
|
182
|
-
# @api private
|
|
183
|
-
def cached_pid
|
|
184
|
-
@actual_pid || @last_pid
|
|
185
|
-
end
|
|
186
156
|
end
|
|
187
157
|
end
|
|
188
158
|
end
|
|
@@ -9,15 +9,15 @@ module Bluepill
|
|
|
9
9
|
module StopSignals
|
|
10
10
|
# Send the stop signals from a thread of their own, with their delays
|
|
11
11
|
#
|
|
12
|
-
# @param [
|
|
12
|
+
# @param [Process::StopSignals] signals the signals, with the delays
|
|
13
|
+
# between
|
|
13
14
|
#
|
|
14
15
|
# @return [Array<Thread>] the process's threads, with the new one
|
|
15
16
|
#
|
|
16
17
|
# @api private
|
|
17
18
|
def send_stop_signals_in_background(signals)
|
|
18
|
-
# issue stop signals with configurable delay between each
|
|
19
19
|
logger.warning "Sending stop signals to #{actual_pid}"
|
|
20
|
-
@threads << Thread.new
|
|
20
|
+
@threads << Thread.new { send_stop_signals(signals) }
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
# Send stop signals, with their delays, until the process stops
|
|
@@ -26,13 +26,14 @@ module Bluepill
|
|
|
26
26
|
# the process is still running. Then gives the last signal the rest of
|
|
27
27
|
# stop_grace_time to work before cleaning up.
|
|
28
28
|
#
|
|
29
|
-
# @param [
|
|
29
|
+
# @param [Process::StopSignals] signals the signals, with the delays
|
|
30
|
+
# between
|
|
30
31
|
#
|
|
31
32
|
# @return [void]
|
|
32
33
|
#
|
|
33
34
|
# @api private
|
|
34
35
|
def send_stop_signals(signals)
|
|
35
|
-
send_stop_signal(signals.
|
|
36
|
+
send_stop_signal(signals.first_signal)
|
|
36
37
|
remaining = send_later_stop_signals(signals)
|
|
37
38
|
wait_for_stop(remaining) if remaining
|
|
38
39
|
cleanup_process
|
|
@@ -42,30 +43,25 @@ module Bluepill
|
|
|
42
43
|
#
|
|
43
44
|
# It stops once the process isn't running.
|
|
44
45
|
#
|
|
45
|
-
# @param [
|
|
46
|
-
#
|
|
46
|
+
# @param [Process::StopSignals] signals the signals, with the delays
|
|
47
|
+
# between
|
|
47
48
|
#
|
|
48
49
|
# @return [Integer, Float, nil] how much of stop_grace_time is left, or
|
|
49
50
|
# nil if the process stopped
|
|
50
51
|
#
|
|
51
52
|
# @api private
|
|
52
53
|
def send_later_stop_signals(signals)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
# we already checked to make sure signals had an odd number of items
|
|
56
|
-
delay = signals.shift #: Integer
|
|
57
|
-
signal = signals.shift
|
|
58
|
-
remaining -= delay
|
|
59
|
-
return unless running_after?(delay)
|
|
54
|
+
signals.later_signals.each do |delay, signal|
|
|
55
|
+
return nil unless running_after?(delay)
|
|
60
56
|
|
|
61
57
|
send_stop_signal(signal)
|
|
62
58
|
end
|
|
63
|
-
|
|
59
|
+
stop_grace_time - signals.total_delay
|
|
64
60
|
end
|
|
65
61
|
|
|
66
62
|
# Send one of the stop signals, and log it
|
|
67
63
|
#
|
|
68
|
-
# @param [Symbol
|
|
64
|
+
# @param [Symbol] signal the signal
|
|
69
65
|
#
|
|
70
66
|
# @return [Boolean] whether the signal was sent
|
|
71
67
|
#
|
|
@@ -86,7 +82,6 @@ module Bluepill
|
|
|
86
82
|
logger.debug "Sleeping for #{delay} seconds"
|
|
87
83
|
# Kernel.sleep, not sleep, so the specs can stand in for the clock
|
|
88
84
|
Kernel.sleep(delay)
|
|
89
|
-
# break unless signal_process(0) #break unless the process can be reached
|
|
90
85
|
return true if signal_process(0)
|
|
91
86
|
|
|
92
87
|
logger.debug "Process has terminated."
|
|
@@ -27,7 +27,7 @@ module Bluepill
|
|
|
27
27
|
def start_process
|
|
28
28
|
# An old stop signal thread would otherwise signal and clean up the new process
|
|
29
29
|
clean_threads
|
|
30
|
-
|
|
30
|
+
journal.kill_all # be sure nothing else is running from previous runs
|
|
31
31
|
pre_start_process
|
|
32
32
|
# ProcessFactory makes sure every process has a start command
|
|
33
33
|
command = start_command #: String
|
|
@@ -68,13 +68,15 @@ module Bluepill
|
|
|
68
68
|
#
|
|
69
69
|
# @api private
|
|
70
70
|
def daemonize_process(command)
|
|
71
|
-
|
|
71
|
+
# ProcessFactory gives every process that bluepill daemonizes a pid file
|
|
72
|
+
file = pid_file #: String
|
|
73
|
+
daemon_id = System.daemonize(command, command_options, pid_file: file, logger: logger)
|
|
72
74
|
return unless daemon_id
|
|
73
75
|
|
|
74
|
-
|
|
76
|
+
journal.append(daemon_id)
|
|
75
77
|
# Only refresh_children! fills children, and only when monitoring them
|
|
76
78
|
children.each do |child|
|
|
77
|
-
|
|
79
|
+
journal.append(child.actual_pid)
|
|
78
80
|
end
|
|
79
81
|
end
|
|
80
82
|
|
|
@@ -103,7 +105,7 @@ module Bluepill
|
|
|
103
105
|
# @api private
|
|
104
106
|
def journal_children
|
|
105
107
|
System.get_children(actual_pid).each do |child_pid|
|
|
106
|
-
|
|
108
|
+
journal.append(child_pid)
|
|
107
109
|
end
|
|
108
110
|
end
|
|
109
111
|
|
|
@@ -135,7 +137,7 @@ module Bluepill
|
|
|
135
137
|
cmd = prepare_command(command)
|
|
136
138
|
logger.warning "Executing stop command: #{cmd}"
|
|
137
139
|
|
|
138
|
-
with_timeout(stop_grace_time,
|
|
140
|
+
with_timeout(stop_grace_time, :stop) do
|
|
139
141
|
run_command(cmd, "Stop")
|
|
140
142
|
end
|
|
141
143
|
cleanup_process
|
|
@@ -171,7 +173,7 @@ module Bluepill
|
|
|
171
173
|
|
|
172
174
|
logger.warning "Executing restart command: #{cmd}"
|
|
173
175
|
|
|
174
|
-
with_timeout(restart_grace_time,
|
|
176
|
+
with_timeout(restart_grace_time, :restart) do
|
|
175
177
|
run_command(cmd, "Restart")
|
|
176
178
|
end
|
|
177
179
|
|
|
@@ -189,7 +191,7 @@ module Bluepill
|
|
|
189
191
|
#
|
|
190
192
|
# @api private
|
|
191
193
|
def cleanup_process
|
|
192
|
-
|
|
194
|
+
journal.kill_all # finish cleanup
|
|
193
195
|
unlink_pid
|
|
194
196
|
clear_pid
|
|
195
197
|
post_stop_process
|