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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "bluepill/process/stop_signals"
|
|
4
|
+
|
|
3
5
|
module Bluepill
|
|
4
6
|
class Process
|
|
5
7
|
# The options a config can set for a Process, and their defaults
|
|
@@ -55,6 +57,39 @@ module Bluepill
|
|
|
55
57
|
group_unmonitor_noblock
|
|
56
58
|
].freeze
|
|
57
59
|
|
|
60
|
+
# The attributes of an application that its processes get, unless a
|
|
61
|
+
# process sets its own
|
|
62
|
+
APP_ATTRIBUTES = %i[working_dir uid gid supplementary_groups environment auto_start notify_command].freeze
|
|
63
|
+
|
|
64
|
+
# The attributes that a monitored child gets from its parent
|
|
65
|
+
#
|
|
66
|
+
# They're the grace times, and the user and groups, so the child's
|
|
67
|
+
# commands run as its parent's do.
|
|
68
|
+
CHILD_ATTRIBUTES = %i[start_grace_time stop_grace_time restart_grace_time uid gid supplementary_groups].freeze
|
|
69
|
+
|
|
70
|
+
# The environment of a process that sets none
|
|
71
|
+
NO_ENVIRONMENT = {} #: Hash[String, String]
|
|
72
|
+
NO_ENVIRONMENT.freeze
|
|
73
|
+
|
|
74
|
+
# The attributes that have a default, which a config can override
|
|
75
|
+
#
|
|
76
|
+
# The grace times are 3 seconds, hooks get 30, the pid is cached,
|
|
77
|
+
# children are left alone, and a group doesn't wait for the process.
|
|
78
|
+
DEFAULTS = {
|
|
79
|
+
monitor_children: false,
|
|
80
|
+
cache_actual_pid: true,
|
|
81
|
+
start_grace_time: 3,
|
|
82
|
+
stop_grace_time: 3,
|
|
83
|
+
restart_grace_time: 3,
|
|
84
|
+
environment: NO_ENVIRONMENT,
|
|
85
|
+
on_start_timeout: :start,
|
|
86
|
+
hook_timeout: 30,
|
|
87
|
+
group_start_noblock: true,
|
|
88
|
+
group_stop_noblock: true,
|
|
89
|
+
group_restart_noblock: true,
|
|
90
|
+
group_unmonitor_noblock: true
|
|
91
|
+
}.freeze
|
|
92
|
+
|
|
58
93
|
# Return the command run before every start, if any
|
|
59
94
|
#
|
|
60
95
|
# Bluepill waits for it to finish, and starts the process even if it exits
|
|
@@ -265,7 +300,7 @@ module Bluepill
|
|
|
265
300
|
#
|
|
266
301
|
# monitor_children sets it, with the children's checks.
|
|
267
302
|
#
|
|
268
|
-
# @return [ProcessFactory, nil]
|
|
303
|
+
# @return [ProcessFactory::ChildFactory, nil]
|
|
269
304
|
#
|
|
270
305
|
# @api private
|
|
271
306
|
attr_accessor :child_process_factory
|
|
@@ -297,20 +332,35 @@ module Bluepill
|
|
|
297
332
|
|
|
298
333
|
# Return the signals that stop the process, with the delays between
|
|
299
334
|
#
|
|
300
|
-
#
|
|
301
|
-
#
|
|
302
|
-
#
|
|
335
|
+
# Each later signal is only sent if the process is still running.
|
|
336
|
+
#
|
|
337
|
+
# @return [StopSignals, nil]
|
|
338
|
+
#
|
|
339
|
+
# @api private
|
|
340
|
+
attr_reader :stop_signals
|
|
341
|
+
|
|
342
|
+
# Set the signals that stop the process, with the delays between
|
|
343
|
+
#
|
|
344
|
+
# @param [StopSignals, Array<Symbol, Integer>, nil] signals the stop
|
|
345
|
+
# signals, or a signal, then a delay in seconds and a signal, and so
|
|
346
|
+
# on, such as [:quit, 30, :term, 5, :kill]
|
|
347
|
+
#
|
|
348
|
+
# @return [void]
|
|
303
349
|
#
|
|
304
|
-
# @
|
|
350
|
+
# @raise [ConfigError] for an Array that doesn't alternate signals and
|
|
351
|
+
# delays
|
|
305
352
|
#
|
|
306
353
|
# @api private
|
|
307
|
-
|
|
354
|
+
def stop_signals=(signals)
|
|
355
|
+
@stop_signals = signals && StopSignals.from(signals)
|
|
356
|
+
end
|
|
308
357
|
|
|
309
358
|
# Return the event to dispatch when the start command takes too long
|
|
310
359
|
#
|
|
311
|
-
# The default is start, which tries again.
|
|
360
|
+
# The default is :start, which tries again. The factory only accepts
|
|
361
|
+
# an event of the state machine, such as :unmonitor.
|
|
312
362
|
#
|
|
313
|
-
# @return [
|
|
363
|
+
# @return [Symbol]
|
|
314
364
|
#
|
|
315
365
|
# @api private
|
|
316
366
|
attr_accessor :on_start_timeout
|
|
@@ -345,36 +395,20 @@ module Bluepill
|
|
|
345
395
|
|
|
346
396
|
# Set the defaults, and then the options the config gives
|
|
347
397
|
#
|
|
348
|
-
# @param [Hash{Symbol => Object}] options the process's options,
|
|
349
|
-
#
|
|
398
|
+
# @param [Hash{Symbol => Object}] options the process's options, each
|
|
399
|
+
# one of CONFIGURABLE_ATTRIBUTES
|
|
350
400
|
#
|
|
351
401
|
# @return [void]
|
|
352
402
|
#
|
|
403
|
+
# @raise [ArgumentError] for an option that isn't one of
|
|
404
|
+
# CONFIGURABLE_ATTRIBUTES
|
|
405
|
+
#
|
|
353
406
|
# @api private
|
|
354
407
|
def configure(options)
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
public_send("#{attribute_name}=", options.fetch(attribute_name)) if options.key?(attribute_name)
|
|
358
|
-
end
|
|
359
|
-
end
|
|
408
|
+
unknown = options.keys - CONFIGURABLE_ATTRIBUTES
|
|
409
|
+
raise ArgumentError, "unknown attributes: #{unknown.collect(&:inspect).join(", ")}" unless unknown.empty?
|
|
360
410
|
|
|
361
|
-
|
|
362
|
-
#
|
|
363
|
-
# The grace times are 3 seconds, hooks get 30, the pid is cached,
|
|
364
|
-
# children are left alone, and a group doesn't wait for the process.
|
|
365
|
-
#
|
|
366
|
-
# @return [void]
|
|
367
|
-
#
|
|
368
|
-
# @api private
|
|
369
|
-
def apply_defaults
|
|
370
|
-
# These defaults are overriden by configure if it's configured to be something else.
|
|
371
|
-
@monitor_children = false
|
|
372
|
-
@cache_actual_pid = true
|
|
373
|
-
@start_grace_time = @stop_grace_time = @restart_grace_time = 3
|
|
374
|
-
@environment = {}
|
|
375
|
-
@on_start_timeout = "start"
|
|
376
|
-
@hook_timeout = 30
|
|
377
|
-
@group_start_noblock = @group_stop_noblock = @group_restart_noblock = @group_unmonitor_noblock = true
|
|
411
|
+
DEFAULTS.merge(options).each { |name, value| public_send(:"#{name}=", value) }
|
|
378
412
|
end
|
|
379
413
|
end
|
|
380
414
|
end
|
|
@@ -11,9 +11,7 @@ module Bluepill
|
|
|
11
11
|
# @return [Boolean]
|
|
12
12
|
#
|
|
13
13
|
# @api private
|
|
14
|
-
def monitor_children?
|
|
15
|
-
!!monitor_children
|
|
16
|
-
end
|
|
14
|
+
def monitor_children? = !!monitor_children
|
|
17
15
|
|
|
18
16
|
# Update the list of children from the tick's ps table
|
|
19
17
|
#
|
|
@@ -45,7 +43,7 @@ module Bluepill
|
|
|
45
43
|
def add_children(child_pids)
|
|
46
44
|
# Construct a new process wrapper for each new found children
|
|
47
45
|
# monitor_children sets the factory
|
|
48
|
-
factory = child_process_factory #: ProcessFactory
|
|
46
|
+
factory = child_process_factory #: ProcessFactory::ChildFactory
|
|
49
47
|
child_pids.each do |child_pid|
|
|
50
48
|
@children << new_child(factory, child_pid)
|
|
51
49
|
end
|
|
@@ -56,14 +54,14 @@ module Bluepill
|
|
|
56
54
|
# A process that bluepill daemonizes also has the child's pid added to
|
|
57
55
|
# its journal.
|
|
58
56
|
#
|
|
59
|
-
# @param [ProcessFactory] factory the factory monitor_children set
|
|
57
|
+
# @param [ProcessFactory::ChildFactory] factory the factory monitor_children set
|
|
60
58
|
# @param [Integer] child_pid the child's pid
|
|
61
59
|
#
|
|
62
60
|
# @return [Process] the child's process wrapper
|
|
63
61
|
#
|
|
64
62
|
# @api private
|
|
65
63
|
def new_child(factory, child_pid)
|
|
66
|
-
|
|
64
|
+
journal.append(child_pid) if daemonize?
|
|
67
65
|
child_name = "<child(pid:#{child_pid})>"
|
|
68
66
|
factory.create_child_process(child_name, child_pid, logger.prefix_with(child_name))
|
|
69
67
|
end
|
data/lib/bluepill/process/pid.rb
CHANGED
|
@@ -19,12 +19,11 @@ module Bluepill
|
|
|
19
19
|
#
|
|
20
20
|
# @api private
|
|
21
21
|
def process_running?(force = false)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@process_running ||= signal_process(0)
|
|
22
|
+
running = @process_running unless force
|
|
23
|
+
running = @process_running = signal_process(0) if running.nil?
|
|
25
24
|
# the process isn't running, so we should clear the PID
|
|
26
|
-
clear_pid unless
|
|
27
|
-
|
|
25
|
+
clear_pid unless running
|
|
26
|
+
running
|
|
28
27
|
end
|
|
29
28
|
|
|
30
29
|
# Send a signal to the process
|
|
@@ -60,18 +59,14 @@ module Bluepill
|
|
|
60
59
|
# @return [Boolean]
|
|
61
60
|
#
|
|
62
61
|
# @api private
|
|
63
|
-
def cache_actual_pid?
|
|
64
|
-
!!@cache_actual_pid
|
|
65
|
-
end
|
|
62
|
+
def cache_actual_pid? = !!@cache_actual_pid
|
|
66
63
|
|
|
67
64
|
# Return the process's pid, from pid_command or the pid file
|
|
68
65
|
#
|
|
69
66
|
# @return [Integer, nil] the pid, or nil if there's none yet
|
|
70
67
|
#
|
|
71
68
|
# @api private
|
|
72
|
-
def actual_pid
|
|
73
|
-
pid_command ? pid_from_command : pid_from_file
|
|
74
|
-
end
|
|
69
|
+
def actual_pid = pid_command ? pid_from_command : pid_from_file
|
|
75
70
|
|
|
76
71
|
# Return the pid in the pid file, cached unless cache_actual_pid is false
|
|
77
72
|
#
|
|
@@ -83,38 +78,34 @@ module Bluepill
|
|
|
83
78
|
def pid_from_file
|
|
84
79
|
return @actual_pid if cache_actual_pid? && @actual_pid
|
|
85
80
|
|
|
86
|
-
@actual_pid = if pid_file
|
|
87
|
-
if File.exist?(pid_file)
|
|
88
|
-
positive_pid(File.read(pid_file).to_i)
|
|
89
|
-
else
|
|
90
|
-
logger.warning("pid_file #{pid_file} does not exist or cannot be read")
|
|
91
|
-
nil
|
|
92
|
-
end
|
|
93
|
-
end
|
|
81
|
+
@actual_pid = (PidFile.read(pid_file) || warn_if_missing(pid_file) if pid_file)
|
|
94
82
|
end
|
|
95
83
|
|
|
96
|
-
#
|
|
84
|
+
# Warn if the pid file is missing
|
|
97
85
|
#
|
|
98
|
-
#
|
|
86
|
+
# An empty or garbled pid file has no pid either, but it's there, so
|
|
87
|
+
# it's not warned about.
|
|
99
88
|
#
|
|
100
|
-
# @
|
|
89
|
+
# @param [String] file the pid file
|
|
90
|
+
#
|
|
91
|
+
# @return [nil] no pid
|
|
101
92
|
#
|
|
102
93
|
# @api private
|
|
103
|
-
def
|
|
104
|
-
#
|
|
105
|
-
|
|
94
|
+
def warn_if_missing(file)
|
|
95
|
+
logger.warning("pid_file #{file} does not exist or cannot be read") unless File.exist?(file)
|
|
96
|
+
nil
|
|
106
97
|
end
|
|
107
98
|
|
|
108
|
-
# Return the pid
|
|
109
|
-
#
|
|
110
|
-
# An empty or garbled pid file reads as 0, which is never a real process.
|
|
99
|
+
# Return the pid that pid_command prints
|
|
111
100
|
#
|
|
112
|
-
#
|
|
101
|
+
# Output that isn't only digits gives no pid.
|
|
113
102
|
#
|
|
114
|
-
# @return [Integer, nil] the pid, or nil if
|
|
103
|
+
# @return [Integer, nil] the pid, or nil if the output isn't one
|
|
115
104
|
#
|
|
116
105
|
# @api private
|
|
117
|
-
def
|
|
106
|
+
def pid_from_command
|
|
107
|
+
# Output that is not just digits reads as nil, so as 0, which is no pid
|
|
108
|
+
pid = `#{pid_command}`.strip[/\A\d+\z/].to_i
|
|
118
109
|
pid if pid > 0
|
|
119
110
|
end
|
|
120
111
|
|
|
@@ -126,10 +117,27 @@ module Bluepill
|
|
|
126
117
|
#
|
|
127
118
|
# @api private
|
|
128
119
|
def actual_pid=(pid)
|
|
129
|
-
|
|
120
|
+
journal.append(pid) # be sure to always log the pid
|
|
130
121
|
@actual_pid = pid
|
|
131
122
|
end
|
|
132
123
|
|
|
124
|
+
# Return the process's pid, or the last one it had if it has died
|
|
125
|
+
#
|
|
126
|
+
# A process that has died has no pid by the time its state changes,
|
|
127
|
+
# but notify_command still gets the one it had.
|
|
128
|
+
#
|
|
129
|
+
# @return [Integer, nil]
|
|
130
|
+
#
|
|
131
|
+
# @api private
|
|
132
|
+
def cached_pid = @actual_pid || @last_pid
|
|
133
|
+
|
|
134
|
+
# Return the process's journals, of the pids and process groups it had
|
|
135
|
+
#
|
|
136
|
+
# @return [ProcessJournal::Journal]
|
|
137
|
+
#
|
|
138
|
+
# @api private
|
|
139
|
+
def journal = @journal ||= ProcessJournal.for(name)
|
|
140
|
+
|
|
133
141
|
# Forget the process's pid
|
|
134
142
|
#
|
|
135
143
|
# The last pid is kept for notify_command, since a process that has died
|
|
@@ -148,9 +156,7 @@ module Bluepill
|
|
|
148
156
|
# @return [void]
|
|
149
157
|
#
|
|
150
158
|
# @api private
|
|
151
|
-
def unlink_pid
|
|
152
|
-
System.delete_if_exists(pid_file)
|
|
153
|
-
end
|
|
159
|
+
def unlink_pid = System.delete_if_exists(pid_file)
|
|
154
160
|
end
|
|
155
161
|
end
|
|
156
162
|
end
|
|
@@ -6,7 +6,8 @@ module Bluepill
|
|
|
6
6
|
#
|
|
7
7
|
# A process is unmonitored, up, down, starting, stopping or restarting.
|
|
8
8
|
# Ticks and the start, stop, unmonitor and restart events move it, and
|
|
9
|
-
# TransitionCallbacks run on the way.
|
|
9
|
+
# TransitionCallbacks run on the way. Process#dispatch! fires the events
|
|
10
|
+
# that checks, triggers and commands ask for.
|
|
10
11
|
#
|
|
11
12
|
# @api private
|
|
12
13
|
module StateMachine
|
|
@@ -55,101 +56,56 @@ module Bluepill
|
|
|
55
56
|
# @return [Boolean]
|
|
56
57
|
#
|
|
57
58
|
# @api private
|
|
58
|
-
def unmonitored?
|
|
59
|
-
state.equal?(:unmonitored)
|
|
60
|
-
end
|
|
59
|
+
def unmonitored? = state.equal?(:unmonitored)
|
|
61
60
|
|
|
62
61
|
# Return whether the process is up
|
|
63
62
|
#
|
|
64
63
|
# @return [Boolean]
|
|
65
64
|
#
|
|
66
65
|
# @api private
|
|
67
|
-
def up?
|
|
68
|
-
state.equal?(:up)
|
|
69
|
-
end
|
|
66
|
+
def up? = state.equal?(:up)
|
|
70
67
|
|
|
71
68
|
# Return whether the process is down
|
|
72
69
|
#
|
|
73
70
|
# @return [Boolean]
|
|
74
71
|
#
|
|
75
72
|
# @api private
|
|
76
|
-
def down?
|
|
77
|
-
state.equal?(:down)
|
|
78
|
-
end
|
|
73
|
+
def down? = state.equal?(:down)
|
|
79
74
|
|
|
80
75
|
# Return whether the process is starting
|
|
81
76
|
#
|
|
82
77
|
# @return [Boolean]
|
|
83
78
|
#
|
|
84
79
|
# @api private
|
|
85
|
-
def starting?
|
|
86
|
-
state.equal?(:starting)
|
|
87
|
-
end
|
|
80
|
+
def starting? = state.equal?(:starting)
|
|
88
81
|
|
|
89
82
|
# Return whether the process is stopping
|
|
90
83
|
#
|
|
91
84
|
# @return [Boolean]
|
|
92
85
|
#
|
|
93
86
|
# @api private
|
|
94
|
-
def stopping?
|
|
95
|
-
state.equal?(:stopping)
|
|
96
|
-
end
|
|
87
|
+
def stopping? = state.equal?(:stopping)
|
|
97
88
|
|
|
98
89
|
# Return whether the process is restarting
|
|
99
90
|
#
|
|
100
91
|
# @return [Boolean]
|
|
101
92
|
#
|
|
102
93
|
# @api private
|
|
103
|
-
def restarting?
|
|
104
|
-
state.equal?(:restarting)
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
# Start the process, if it's unmonitored or down
|
|
108
|
-
#
|
|
109
|
-
# @return [Boolean] whether the process moved to starting
|
|
110
|
-
#
|
|
111
|
-
# @api private
|
|
112
|
-
def start
|
|
113
|
-
fire_event(:start)
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
# Stop the process, if it's up
|
|
117
|
-
#
|
|
118
|
-
# @return [Boolean] whether the process moved to stopping
|
|
119
|
-
#
|
|
120
|
-
# @api private
|
|
121
|
-
def stop
|
|
122
|
-
fire_event(:stop)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# Stop monitoring the process, from any state
|
|
126
|
-
#
|
|
127
|
-
# @return [Boolean] whether the process moved to unmonitored
|
|
128
|
-
#
|
|
129
|
-
# @api private
|
|
130
|
-
def unmonitor
|
|
131
|
-
fire_event(:unmonitor)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# Restart the process, if it's up or down
|
|
135
|
-
#
|
|
136
|
-
# @return [Boolean] whether the process moved to restarting
|
|
137
|
-
#
|
|
138
|
-
# @api private
|
|
139
|
-
def restart
|
|
140
|
-
fire_event(:restart)
|
|
141
|
-
end
|
|
94
|
+
def restarting? = state.equal?(:restarting)
|
|
142
95
|
|
|
143
96
|
# Move the process to the state an event leads to from its state
|
|
144
97
|
#
|
|
145
|
-
# The before callbacks see the old state, and can throw
|
|
146
|
-
# the move. The after callbacks see the new one.
|
|
98
|
+
# The before callbacks see the old state, and can throw Trigger::HALT
|
|
99
|
+
# to cancel the move. The after callbacks see the new one.
|
|
147
100
|
#
|
|
148
|
-
# @param [Symbol] event :tick, :start, :stop,
|
|
101
|
+
# @param [Symbol] event :tick, or one of TRANSITIONS: :start, :stop,
|
|
102
|
+
# :unmonitor or :restart
|
|
149
103
|
#
|
|
150
104
|
# @return [Boolean] whether the process moved, false when the event
|
|
151
105
|
# doesn't lead anywhere from its state or a callback halted it
|
|
152
106
|
#
|
|
107
|
+
# @raise [KeyError] for any other event
|
|
108
|
+
#
|
|
153
109
|
# @api private
|
|
154
110
|
def fire_event(event)
|
|
155
111
|
from = state
|
|
@@ -157,7 +113,7 @@ module Bluepill
|
|
|
157
113
|
return false unless to
|
|
158
114
|
|
|
159
115
|
transition = from.equal?(to) ? LOOPBACKS.fetch(from) : Transition.new(from, to)
|
|
160
|
-
return false unless catch(
|
|
116
|
+
return false unless catch(Trigger::HALT) { before_transition(transition) }
|
|
161
117
|
|
|
162
118
|
self.state = to
|
|
163
119
|
after_transition(transition)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
class Process
|
|
5
|
+
# The signals that stop a process, with the delays between them
|
|
6
|
+
#
|
|
7
|
+
# A pill file gives them as an Array that alternates signals and delays,
|
|
8
|
+
# starting and ending with a signal, such as [:quit, 30, :term, 5, :kill]:
|
|
9
|
+
# send QUIT, and if the process is still running 30 seconds later send
|
|
10
|
+
# TERM, and if it's still running 5 seconds after that send KILL.
|
|
11
|
+
#
|
|
12
|
+
# @api private
|
|
13
|
+
class StopSignals
|
|
14
|
+
# Return stop signals, from an Array or as they are
|
|
15
|
+
#
|
|
16
|
+
# @example
|
|
17
|
+
# StopSignals.from([:quit, 30, :kill]).total_delay # => 30
|
|
18
|
+
#
|
|
19
|
+
# @param [StopSignals, Array<Symbol, Integer>] signals the stop signals,
|
|
20
|
+
# or the signals and delays
|
|
21
|
+
#
|
|
22
|
+
# @return [StopSignals]
|
|
23
|
+
#
|
|
24
|
+
# @raise [ConfigError] for an Array that doesn't alternate signals and
|
|
25
|
+
# delays
|
|
26
|
+
#
|
|
27
|
+
# @api private
|
|
28
|
+
def self.from(signals) = signals.instance_of?(StopSignals) ? signals : new(signals)
|
|
29
|
+
|
|
30
|
+
# Return the signal sent first
|
|
31
|
+
#
|
|
32
|
+
# @example
|
|
33
|
+
# StopSignals.new([:quit, 30, :kill]).first_signal # => :quit
|
|
34
|
+
#
|
|
35
|
+
# @return [Symbol]
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
attr_reader :first_signal
|
|
39
|
+
|
|
40
|
+
# Return each signal after the first, with the delay before it
|
|
41
|
+
#
|
|
42
|
+
# @example
|
|
43
|
+
# StopSignals.new([:quit, 30, :term, 5, :kill]).later_signals
|
|
44
|
+
# # => [[30, :term], [5, :kill]]
|
|
45
|
+
#
|
|
46
|
+
# @return [Array<Array(Integer, Symbol)>] frozen pairs of the seconds to
|
|
47
|
+
# wait and the signal to send then
|
|
48
|
+
#
|
|
49
|
+
# @api private
|
|
50
|
+
attr_reader :later_signals
|
|
51
|
+
|
|
52
|
+
# Parse the signals and delays, and fail on a mistake
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
55
|
+
# StopSignals.new([:quit, 30, :term, 5, :kill])
|
|
56
|
+
#
|
|
57
|
+
# @param [Array<Symbol, Integer>] signals a signal, then a delay in
|
|
58
|
+
# seconds and a signal, and so on
|
|
59
|
+
#
|
|
60
|
+
# @return [void]
|
|
61
|
+
#
|
|
62
|
+
# @raise [ConfigError] unless the signals and delays alternate, starting
|
|
63
|
+
# and ending with a signal
|
|
64
|
+
#
|
|
65
|
+
# @api private
|
|
66
|
+
def initialize(signals)
|
|
67
|
+
validate!(signals)
|
|
68
|
+
# validate! checked that signals alternate, starting with a signal
|
|
69
|
+
@first_signal = signals.first #: Symbol
|
|
70
|
+
@later_signals = signals.drop(1).each_slice(2).to_a.freeze #: Array[[Integer, Symbol]]
|
|
71
|
+
freeze
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Return the delays added up
|
|
75
|
+
#
|
|
76
|
+
# @example
|
|
77
|
+
# StopSignals.new([:quit, 30, :term, 5, :kill]).total_delay # => 35
|
|
78
|
+
#
|
|
79
|
+
# @return [Integer] the seconds between the first signal and the last
|
|
80
|
+
#
|
|
81
|
+
# @api private
|
|
82
|
+
def total_delay = later_signals.sum { |delay, _signal| delay }
|
|
83
|
+
|
|
84
|
+
# Return the signals and delays, as a pill file gives them
|
|
85
|
+
#
|
|
86
|
+
# @example
|
|
87
|
+
# StopSignals.new([:quit, 30, :kill]).to_a # => [:quit, 30, :kill]
|
|
88
|
+
#
|
|
89
|
+
# @return [Array<Symbol, Integer>]
|
|
90
|
+
#
|
|
91
|
+
# @api private
|
|
92
|
+
def to_a = [first_signal, *later_signals.flat_map(&:itself)]
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
# Fail unless the signals and delays alternate
|
|
97
|
+
#
|
|
98
|
+
# The signals and delays must alternate, starting and ending with a
|
|
99
|
+
# signal. Checks the more specific problems before the 'odd number'
|
|
100
|
+
# message.
|
|
101
|
+
#
|
|
102
|
+
# @param [Array<Symbol, Integer>] signals the signals and delays
|
|
103
|
+
#
|
|
104
|
+
# @return [void]
|
|
105
|
+
#
|
|
106
|
+
# @raise [ConfigError] for a mistake
|
|
107
|
+
#
|
|
108
|
+
# @api private
|
|
109
|
+
def validate!(signals)
|
|
110
|
+
signals.each_with_index do |value, i|
|
|
111
|
+
expected, label = i.even? ? [Symbol, "a symbol (signal)"] : [Integer, "a number (delay)"]
|
|
112
|
+
next if value.is_a?(expected)
|
|
113
|
+
|
|
114
|
+
raise(ConfigError, "Config Error: Invalid stop_signals! Expected #{label} at position #{i} instead of '#{value}'.")
|
|
115
|
+
end
|
|
116
|
+
raise(ConfigError, "Config Error: Invalid stop_signals! Expected an odd number of elements.") unless signals.size.odd?
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -25,7 +25,8 @@ module Bluepill
|
|
|
25
25
|
#
|
|
26
26
|
# @api private
|
|
27
27
|
def before_transition(transition)
|
|
28
|
-
|
|
28
|
+
# @triggers and not triggers, to save a call on every tick
|
|
29
|
+
@triggers.notify(transition)
|
|
29
30
|
clean_threads if transition.from_name.equal?(:stopping)
|
|
30
31
|
true
|
|
31
32
|
end
|
|
@@ -53,24 +54,6 @@ module Bluepill
|
|
|
53
54
|
notify_transition(transition)
|
|
54
55
|
end
|
|
55
56
|
|
|
56
|
-
# Tell every trigger about a transition
|
|
57
|
-
#
|
|
58
|
-
# @param [#from_name, #to_name, #loopback?] transition the transition
|
|
59
|
-
#
|
|
60
|
-
# @return [void]
|
|
61
|
-
#
|
|
62
|
-
# @raise [StandardError] what a trigger raised, after logging it
|
|
63
|
-
#
|
|
64
|
-
# @api private
|
|
65
|
-
def notify_triggers(transition)
|
|
66
|
-
triggers.each do |trigger|
|
|
67
|
-
trigger.notify(transition)
|
|
68
|
-
rescue => e
|
|
69
|
-
logger.err e.backtrace
|
|
70
|
-
raise
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
57
|
# Clear the watches' history after the process changes state
|
|
75
58
|
#
|
|
76
59
|
# A process that's monitoring its children also clears their list, so it
|