bluepill 0.2.9 → 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 +100 -2
- data/README.md +12 -7
- data/bin/bluepill +8 -2
- data/bluepill.gemspec +1 -1
- data/lib/bluepill/application/lifecycle.rb +44 -34
- data/lib/bluepill/application/listener.rb +1 -1
- data/lib/bluepill/application/previous_daemon.rb +12 -14
- data/lib/bluepill/application.rb +33 -29
- 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 +45 -54
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +32 -37
- data/lib/bluepill/dsl/app_proxy.rb +68 -64
- 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 +83 -84
- data/lib/bluepill/dsl/process_proxy.rb +61 -168
- data/lib/bluepill/dsl.rb +16 -15
- 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 -16
- 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 +55 -52
- 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 -67
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +29 -47
- 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 +18 -69
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/version.rb +12 -22
- data/lib/bluepill.rb +1 -1
- data/sig/bluepill/application.rbs +11 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +40 -40
- data/sig/bluepill/dsl.rbs +97 -60
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +39 -305
- 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 +1 -17
- data/sig/manifest.yaml +1 -0
- metadata +21 -8
- data/lib/bluepill/deprecation/custom_checks.rb +0 -193
- data/lib/bluepill/deprecation/dsl.rb +0 -141
- data/lib/bluepill/deprecation/options.rb +0 -140
- data/lib/bluepill/deprecation.rb +0 -92
- data/lib/bluepill/dsl/process_factory/children.rb +0 -99
- data/sig/bluepill/deprecation.rbs +0 -87
|
@@ -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,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "bluepill/system"
|
|
4
|
+
require "bluepill/check_registry"
|
|
4
5
|
require "bluepill/util/log_error"
|
|
5
6
|
require "bluepill/process_journal"
|
|
6
7
|
require "bluepill/process_commands"
|
|
@@ -11,6 +12,7 @@ require "bluepill/process/transition"
|
|
|
11
12
|
require "bluepill/process/state_machine"
|
|
12
13
|
require "bluepill/process/transition_callbacks"
|
|
13
14
|
require "bluepill/process/watches"
|
|
15
|
+
require "bluepill/process/triggers"
|
|
14
16
|
require "bluepill/process/user_commands"
|
|
15
17
|
|
|
16
18
|
module Bluepill
|
|
@@ -46,10 +48,10 @@ module Bluepill
|
|
|
46
48
|
|
|
47
49
|
# Return the triggers notified of the process's transitions
|
|
48
50
|
#
|
|
49
|
-
# @return [
|
|
51
|
+
# @return [Triggers]
|
|
50
52
|
#
|
|
51
53
|
# @api private
|
|
52
|
-
|
|
54
|
+
attr_reader :triggers
|
|
53
55
|
|
|
54
56
|
# Return the time, from Clock.now, before which ticks are skipped
|
|
55
57
|
#
|
|
@@ -100,7 +102,10 @@ module Bluepill
|
|
|
100
102
|
# @api private
|
|
101
103
|
attr_reader :statistics
|
|
102
104
|
|
|
103
|
-
# 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.
|
|
104
109
|
#
|
|
105
110
|
# @return [Logger]
|
|
106
111
|
#
|
|
@@ -114,24 +119,29 @@ module Bluepill
|
|
|
114
119
|
# and children left alone.
|
|
115
120
|
#
|
|
116
121
|
# @param [String] process_name the process's name
|
|
117
|
-
# @param [Hash{Symbol => Hash{Symbol => Object}}
|
|
118
|
-
# check name
|
|
119
|
-
#
|
|
120
|
-
#
|
|
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
|
|
121
131
|
#
|
|
122
132
|
# @return [void]
|
|
123
133
|
#
|
|
124
134
|
# @api private
|
|
125
|
-
def initialize(process_name, checks,
|
|
135
|
+
def initialize(process_name, checks, logger: nil, actual_pid: nil, **attributes)
|
|
126
136
|
@name = process_name
|
|
127
137
|
@event_mutex = Monitor.new
|
|
128
138
|
@children = []
|
|
129
139
|
@threads = []
|
|
130
140
|
@statistics = ProcessStatistics.new
|
|
131
|
-
@actual_pid =
|
|
132
|
-
add_checks(checks,
|
|
141
|
+
@actual_pid = actual_pid
|
|
142
|
+
add_checks(checks, logger)
|
|
133
143
|
@state = :unmonitored
|
|
134
|
-
configure(
|
|
144
|
+
configure(attributes)
|
|
135
145
|
end
|
|
136
146
|
|
|
137
147
|
# Check on the process once a tick
|
|
@@ -176,26 +186,17 @@ module Bluepill
|
|
|
176
186
|
children.each(&:tick)
|
|
177
187
|
end
|
|
178
188
|
|
|
179
|
-
# Set the logger, for the process and for its watches and triggers
|
|
180
|
-
#
|
|
181
|
-
# @param [Logger] logger the logger to use
|
|
182
|
-
#
|
|
183
|
-
# @return [void]
|
|
184
|
-
#
|
|
185
|
-
# @api private
|
|
186
|
-
def logger=(logger)
|
|
187
|
-
@logger = logger
|
|
188
|
-
watches.logger = logger
|
|
189
|
-
triggers.each { |t| t.logger = logger }
|
|
190
|
-
end
|
|
191
|
-
|
|
192
189
|
# Record an event and fire it on the state machine
|
|
193
190
|
#
|
|
194
|
-
# @param [Symbol
|
|
191
|
+
# @param [Symbol] event the event, one of
|
|
192
|
+
# StateMachine::TRANSITIONS, such as :start or :restart
|
|
195
193
|
# @param [String, nil] reason why, for the process's event history
|
|
196
194
|
#
|
|
197
195
|
# @return [Boolean] whether the state machine made the transition
|
|
198
196
|
#
|
|
197
|
+
# @raise [KeyError] for an event that isn't one of
|
|
198
|
+
# StateMachine::TRANSITIONS, after recording it
|
|
199
|
+
#
|
|
199
200
|
# @api private
|
|
200
201
|
def dispatch!(event, reason = nil)
|
|
201
202
|
@event_mutex.synchronize do
|
|
@@ -203,7 +204,7 @@ module Bluepill
|
|
|
203
204
|
# The reason, for notify_command
|
|
204
205
|
@transition_reason = reason
|
|
205
206
|
begin
|
|
206
|
-
|
|
207
|
+
fire_event(event)
|
|
207
208
|
ensure
|
|
208
209
|
@transition_reason = nil
|
|
209
210
|
end
|
|
@@ -212,39 +213,45 @@ module Bluepill
|
|
|
212
213
|
|
|
213
214
|
# Set the logger, and add each check
|
|
214
215
|
#
|
|
215
|
-
# A check is a trigger if
|
|
216
|
-
# 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.
|
|
217
218
|
#
|
|
218
|
-
# @param [Hash{Symbol => Hash{Symbol => Object}}
|
|
219
|
-
# 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
|
|
220
222
|
# @param [Logger] logger the logger to use
|
|
221
223
|
#
|
|
222
224
|
# @return [void]
|
|
223
225
|
#
|
|
226
|
+
# @raise [ConfigError] for a name that no check or trigger has
|
|
227
|
+
#
|
|
224
228
|
# @api private
|
|
225
229
|
def add_checks(checks, logger)
|
|
226
|
-
@
|
|
227
|
-
@
|
|
228
|
-
|
|
229
|
-
checks.each
|
|
230
|
-
if Trigger[name]
|
|
231
|
-
add_trigger(name, opts)
|
|
232
|
-
else
|
|
233
|
-
watches.add(name, opts)
|
|
234
|
-
end
|
|
235
|
-
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) }
|
|
236
234
|
end
|
|
237
235
|
|
|
238
|
-
# Add a
|
|
236
|
+
# Add a check, as a trigger or as a watch
|
|
237
|
+
#
|
|
238
|
+
# It's a trigger if CheckRegistry finds a Trigger by its name.
|
|
239
239
|
#
|
|
240
|
-
# @param [Symbol] name the
|
|
241
|
-
# @param [Hash{Symbol => Object}] options the
|
|
240
|
+
# @param [Symbol] name the check's name, such as :mem_usage
|
|
241
|
+
# @param [Hash{Symbol => Object}] options the check's options
|
|
242
242
|
#
|
|
243
|
-
# @return [
|
|
243
|
+
# @return [void]
|
|
244
|
+
#
|
|
245
|
+
# @raise [ConfigError] for a name that no check or trigger has
|
|
244
246
|
#
|
|
245
247
|
# @api private
|
|
246
|
-
def
|
|
247
|
-
|
|
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
|
|
248
255
|
end
|
|
249
256
|
|
|
250
257
|
# Run every watch that's due, and dispatch the events they fire
|
|
@@ -282,9 +289,7 @@ module Bluepill
|
|
|
282
289
|
# @return [Boolean]
|
|
283
290
|
#
|
|
284
291
|
# @api private
|
|
285
|
-
def daemonize?
|
|
286
|
-
!!daemonize
|
|
287
|
-
end
|
|
292
|
+
def daemonize? = !!daemonize
|
|
288
293
|
|
|
289
294
|
# Skip ticks for a while, such as for a command's grace time
|
|
290
295
|
#
|
|
@@ -306,8 +311,6 @@ module Bluepill
|
|
|
306
311
|
# @return [Boolean, nil]
|
|
307
312
|
#
|
|
308
313
|
# @api private
|
|
309
|
-
def skipping_ticks?
|
|
310
|
-
skip_ticks_until && skip_ticks_until > Clock.now
|
|
311
|
-
end
|
|
314
|
+
def skipping_ticks? = skip_ticks_until && skip_ticks_until > Clock.now
|
|
312
315
|
end
|
|
313
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
|
|
@@ -14,9 +14,7 @@ module Bluepill
|
|
|
14
14
|
# @return [Integer] always 1
|
|
15
15
|
#
|
|
16
16
|
# @api private
|
|
17
|
-
def run(_pid, _include_children)
|
|
18
|
-
1
|
|
19
|
-
end
|
|
17
|
+
def run(_pid, _include_children) = 1
|
|
20
18
|
|
|
21
19
|
# Pass whatever the value is
|
|
22
20
|
#
|
|
@@ -25,9 +23,7 @@ module Bluepill
|
|
|
25
23
|
# @return [Boolean] always true
|
|
26
24
|
#
|
|
27
25
|
# @api private
|
|
28
|
-
def check?(_value)
|
|
29
|
-
true
|
|
30
|
-
end
|
|
26
|
+
def check?(_value) = true
|
|
31
27
|
end
|
|
32
28
|
end
|
|
33
29
|
end
|