bluepill 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -21
- data/CONTRIBUTING.md +2 -2
- data/README.md +100 -20
- 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 +59 -259
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +129 -65
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -118
- 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 -152
- data/lib/bluepill/dsl/process_proxy.rb +100 -51
- data/lib/bluepill/dsl.rb +0 -23
- 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 +64 -667
- 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 +48 -45
- 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 -25
- 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 +27 -35
- 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 +47 -994
- metadata +39 -49
- data/lib/bluepill/clock.rb +0 -48
- data/lib/bluepill/deprecation.rb +0 -74
- data/lib/bluepill/trigger/custom.rb +0 -147
- data/lib/bluepill/util/log_error.rb +0 -29
data/lib/bluepill/process.rb
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require
|
|
7
|
-
require
|
|
8
|
-
require
|
|
1
|
+
require "bluepill/system"
|
|
2
|
+
require "bluepill/process_journal"
|
|
3
|
+
require "bluepill/process_commands"
|
|
4
|
+
require "bluepill/process/attributes"
|
|
5
|
+
require "bluepill/process/pid"
|
|
6
|
+
require "bluepill/process/children"
|
|
7
|
+
require "bluepill/process/transition"
|
|
8
|
+
require "bluepill/process/state_machine"
|
|
9
|
+
require "bluepill/process/transition_callbacks"
|
|
10
|
+
require "bluepill/process/watches"
|
|
11
|
+
require "bluepill/process/user_commands"
|
|
9
12
|
|
|
10
13
|
module Bluepill
|
|
11
14
|
# A process that bluepill monitors
|
|
@@ -17,50 +20,12 @@ module Bluepill
|
|
|
17
20
|
# @api private
|
|
18
21
|
class Process
|
|
19
22
|
include ProcessCommands
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
:restart_command,
|
|
27
|
-
|
|
28
|
-
:stdout,
|
|
29
|
-
:stderr,
|
|
30
|
-
:stdin,
|
|
31
|
-
|
|
32
|
-
:daemonize,
|
|
33
|
-
:pid_file,
|
|
34
|
-
:working_dir,
|
|
35
|
-
:environment,
|
|
36
|
-
|
|
37
|
-
:start_grace_time,
|
|
38
|
-
:stop_grace_time,
|
|
39
|
-
:restart_grace_time,
|
|
40
|
-
|
|
41
|
-
:uid,
|
|
42
|
-
:gid,
|
|
43
|
-
|
|
44
|
-
:cache_actual_pid,
|
|
45
|
-
|
|
46
|
-
:monitor_children,
|
|
47
|
-
:child_process_factory,
|
|
48
|
-
|
|
49
|
-
:pid_command,
|
|
50
|
-
:auto_start,
|
|
51
|
-
|
|
52
|
-
:supplementary_groups,
|
|
53
|
-
|
|
54
|
-
:stop_signals,
|
|
55
|
-
|
|
56
|
-
:on_start_timeout,
|
|
57
|
-
|
|
58
|
-
:group_start_noblock,
|
|
59
|
-
:group_restart_noblock,
|
|
60
|
-
:group_stop_noblock,
|
|
61
|
-
:group_unmonitor_noblock,
|
|
62
|
-
|
|
63
|
-
].freeze
|
|
23
|
+
include Attributes
|
|
24
|
+
include Pid
|
|
25
|
+
include Children
|
|
26
|
+
include StateMachine
|
|
27
|
+
include TransitionCallbacks
|
|
28
|
+
include UserCommands
|
|
64
29
|
|
|
65
30
|
# Return the process's name, which commands and logs use
|
|
66
31
|
#
|
|
@@ -71,19 +36,19 @@ module Bluepill
|
|
|
71
36
|
|
|
72
37
|
# Return the checks that run on the process every interval
|
|
73
38
|
#
|
|
74
|
-
# @return [
|
|
39
|
+
# @return [Watches]
|
|
75
40
|
#
|
|
76
41
|
# @api private
|
|
77
|
-
|
|
42
|
+
attr_reader :watches
|
|
78
43
|
|
|
79
44
|
# Return the triggers notified of the process's transitions
|
|
80
45
|
#
|
|
81
|
-
# @return [Array<Trigger
|
|
46
|
+
# @return [Array<Trigger>]
|
|
82
47
|
#
|
|
83
48
|
# @api private
|
|
84
49
|
attr_accessor :triggers
|
|
85
50
|
|
|
86
|
-
# Return the time,
|
|
51
|
+
# Return the time, in epoch seconds, before which ticks are skipped
|
|
87
52
|
#
|
|
88
53
|
# Set while a start, stop or restart command has its grace time to take
|
|
89
54
|
# effect.
|
|
@@ -102,238 +67,19 @@ module Bluepill
|
|
|
102
67
|
# @api private
|
|
103
68
|
attr_accessor :process_running
|
|
104
69
|
|
|
105
|
-
# Return the
|
|
106
|
-
#
|
|
107
|
-
# Bluepill waits for it to finish, and starts the process even if it exits
|
|
108
|
-
# with a non-zero status. It runs in the process's working_dir, with its
|
|
109
|
-
# environment, uid and gid.
|
|
110
|
-
#
|
|
111
|
-
# @return [String, nil]
|
|
112
|
-
#
|
|
113
|
-
# @api private
|
|
114
|
-
attr_accessor :pre_start_command
|
|
115
|
-
|
|
116
|
-
# Return the command that starts the process
|
|
117
|
-
#
|
|
118
|
-
# Bluepill splits it into words and runs it without a shell.
|
|
119
|
-
#
|
|
120
|
-
# @return [String, nil]
|
|
121
|
-
#
|
|
122
|
-
# @api private
|
|
123
|
-
attr_accessor :start_command
|
|
124
|
-
|
|
125
|
-
# Return the command that stops the process, if any
|
|
126
|
-
#
|
|
127
|
-
# <tt>{{PID}}</tt> in it is replaced with the process's pid. Without
|
|
128
|
-
# one, bluepill sends stop_signals, or TERM.
|
|
129
|
-
#
|
|
130
|
-
# @return [String, nil]
|
|
131
|
-
#
|
|
132
|
-
# @api private
|
|
133
|
-
attr_accessor :stop_command
|
|
134
|
-
|
|
135
|
-
# Return the command that restarts the process, if any
|
|
136
|
-
#
|
|
137
|
-
# <tt>{{PID}}</tt> in it is replaced with the process's pid. Without
|
|
138
|
-
# one, bluepill stops the process and starts it again.
|
|
139
|
-
#
|
|
140
|
-
# @return [String, nil]
|
|
141
|
-
#
|
|
142
|
-
# @api private
|
|
143
|
-
attr_accessor :restart_command
|
|
144
|
-
|
|
145
|
-
# Return the file a daemonized process's standard output goes to
|
|
146
|
-
#
|
|
147
|
-
# @return [String, nil]
|
|
148
|
-
#
|
|
149
|
-
# @api private
|
|
150
|
-
attr_accessor :stdout
|
|
151
|
-
|
|
152
|
-
# Return the file a daemonized process's standard error goes to
|
|
153
|
-
#
|
|
154
|
-
# @return [String, nil]
|
|
155
|
-
#
|
|
156
|
-
# @api private
|
|
157
|
-
attr_accessor :stderr
|
|
158
|
-
|
|
159
|
-
# Return the file a daemonized process's standard input comes from
|
|
160
|
-
#
|
|
161
|
-
# @return [String, nil]
|
|
162
|
-
#
|
|
163
|
-
# @api private
|
|
164
|
-
attr_accessor :stdin
|
|
165
|
-
|
|
166
|
-
# Return whether bluepill daemonizes the process and writes its pid file
|
|
167
|
-
#
|
|
168
|
-
# Set it for a process that doesn't daemonize itself.
|
|
169
|
-
#
|
|
170
|
-
# @return [Boolean, nil]
|
|
171
|
-
#
|
|
172
|
-
# @api private
|
|
173
|
-
attr_accessor :daemonize
|
|
174
|
-
|
|
175
|
-
# Return the file that holds the process's pid
|
|
70
|
+
# Return the name of the app the process belongs to
|
|
176
71
|
#
|
|
177
72
|
# @return [String, nil]
|
|
178
73
|
#
|
|
179
74
|
# @api private
|
|
180
|
-
attr_accessor :
|
|
75
|
+
attr_accessor :app_name
|
|
181
76
|
|
|
182
|
-
# Return the
|
|
183
|
-
#
|
|
184
|
-
# A daemonized process starts in / unless this is set.
|
|
77
|
+
# Return the name of the group the process belongs to, if any
|
|
185
78
|
#
|
|
186
79
|
# @return [String, nil]
|
|
187
80
|
#
|
|
188
81
|
# @api private
|
|
189
|
-
attr_accessor :
|
|
190
|
-
|
|
191
|
-
# Return the environment variables the process's commands get
|
|
192
|
-
#
|
|
193
|
-
# @return [Hash{#to_s => #to_s}, nil]
|
|
194
|
-
#
|
|
195
|
-
# @api private
|
|
196
|
-
attr_accessor :environment
|
|
197
|
-
|
|
198
|
-
# Return how long to wait for the process to start, in seconds
|
|
199
|
-
#
|
|
200
|
-
# Bluepill skips ticks for this long after a start. The default is 3.
|
|
201
|
-
#
|
|
202
|
-
# @return [Integer, Float]
|
|
203
|
-
#
|
|
204
|
-
# @api private
|
|
205
|
-
attr_accessor :start_grace_time
|
|
206
|
-
|
|
207
|
-
# Return how long to wait for the process to stop, in seconds
|
|
208
|
-
#
|
|
209
|
-
# Bluepill skips ticks for this long after a stop. The default is 3.
|
|
210
|
-
#
|
|
211
|
-
# @return [Integer, Float]
|
|
212
|
-
#
|
|
213
|
-
# @api private
|
|
214
|
-
attr_accessor :stop_grace_time
|
|
215
|
-
|
|
216
|
-
# Return how long to wait for the process to restart, in seconds
|
|
217
|
-
#
|
|
218
|
-
# Bluepill skips ticks for this long after a restart. The default is 3.
|
|
219
|
-
#
|
|
220
|
-
# @return [Integer, Float]
|
|
221
|
-
#
|
|
222
|
-
# @api private
|
|
223
|
-
attr_accessor :restart_grace_time
|
|
224
|
-
|
|
225
|
-
# Return the user the process's commands run as
|
|
226
|
-
#
|
|
227
|
-
# @return [String, nil]
|
|
228
|
-
#
|
|
229
|
-
# @api private
|
|
230
|
-
attr_accessor :uid
|
|
231
|
-
|
|
232
|
-
# Return the group the process's commands run as
|
|
233
|
-
#
|
|
234
|
-
# @return [String, nil]
|
|
235
|
-
#
|
|
236
|
-
# @api private
|
|
237
|
-
attr_accessor :gid
|
|
238
|
-
|
|
239
|
-
# Return whether to read the pid file only until it gives a pid
|
|
240
|
-
#
|
|
241
|
-
# True by default. Set it to false for a process whose pid changes, such
|
|
242
|
-
# as one checked for zombies.
|
|
243
|
-
#
|
|
244
|
-
# @return [Boolean]
|
|
245
|
-
#
|
|
246
|
-
# @api private
|
|
247
|
-
attr_accessor :cache_actual_pid
|
|
248
|
-
|
|
249
|
-
# Return whether bluepill also watches the process's children
|
|
250
|
-
#
|
|
251
|
-
# @return [Boolean]
|
|
252
|
-
#
|
|
253
|
-
# @api private
|
|
254
|
-
attr_accessor :monitor_children
|
|
255
|
-
|
|
256
|
-
# Return the factory that makes a Process for each child found
|
|
257
|
-
#
|
|
258
|
-
# monitor_children sets it, with the children's checks.
|
|
259
|
-
#
|
|
260
|
-
# @return [ProcessFactory, nil]
|
|
261
|
-
#
|
|
262
|
-
# @api private
|
|
263
|
-
attr_accessor :child_process_factory
|
|
264
|
-
|
|
265
|
-
# Return the command whose output is the process's pid, if any
|
|
266
|
-
#
|
|
267
|
-
# Used instead of pid_file when it's set.
|
|
268
|
-
#
|
|
269
|
-
# @return [String, nil]
|
|
270
|
-
#
|
|
271
|
-
# @api private
|
|
272
|
-
attr_accessor :pid_command
|
|
273
|
-
|
|
274
|
-
# Return whether bluepill starts the process when it loads
|
|
275
|
-
#
|
|
276
|
-
# Only false keeps it from starting: the process is then unmonitored.
|
|
277
|
-
#
|
|
278
|
-
# @return [Boolean, nil]
|
|
279
|
-
#
|
|
280
|
-
# @api private
|
|
281
|
-
attr_accessor :auto_start
|
|
282
|
-
|
|
283
|
-
# Return the extra groups the process's commands run with
|
|
284
|
-
#
|
|
285
|
-
# @return [Array<String>, nil]
|
|
286
|
-
#
|
|
287
|
-
# @api private
|
|
288
|
-
attr_accessor :supplementary_groups
|
|
289
|
-
|
|
290
|
-
# Return the signals that stop the process, with the delays between
|
|
291
|
-
#
|
|
292
|
-
# A signal, then a delay in seconds and a signal, and so on, such as
|
|
293
|
-
# [:quit, 30, :term, 5, :kill]. Each later signal is only sent if the
|
|
294
|
-
# process is still running.
|
|
295
|
-
#
|
|
296
|
-
# @return [Array<Symbol, Integer>, nil]
|
|
297
|
-
#
|
|
298
|
-
# @api private
|
|
299
|
-
attr_accessor :stop_signals
|
|
300
|
-
|
|
301
|
-
# Return the event to dispatch when the start command takes too long
|
|
302
|
-
#
|
|
303
|
-
# The default is start, which tries again.
|
|
304
|
-
#
|
|
305
|
-
# @return [String, Symbol]
|
|
306
|
-
#
|
|
307
|
-
# @api private
|
|
308
|
-
attr_accessor :on_start_timeout
|
|
309
|
-
|
|
310
|
-
# Return whether a group's start runs without waiting for this process
|
|
311
|
-
#
|
|
312
|
-
# @return [Boolean]
|
|
313
|
-
#
|
|
314
|
-
# @api private
|
|
315
|
-
attr_accessor :group_start_noblock
|
|
316
|
-
|
|
317
|
-
# Return whether a group's restart runs without waiting for this process
|
|
318
|
-
#
|
|
319
|
-
# @return [Boolean]
|
|
320
|
-
#
|
|
321
|
-
# @api private
|
|
322
|
-
attr_accessor :group_restart_noblock
|
|
323
|
-
|
|
324
|
-
# Return whether a group's stop runs without waiting for this process
|
|
325
|
-
#
|
|
326
|
-
# @return [Boolean]
|
|
327
|
-
#
|
|
328
|
-
# @api private
|
|
329
|
-
attr_accessor :group_stop_noblock
|
|
330
|
-
|
|
331
|
-
# Return whether a group's unmonitor runs without waiting for it
|
|
332
|
-
#
|
|
333
|
-
# @return [Boolean]
|
|
334
|
-
#
|
|
335
|
-
# @api private
|
|
336
|
-
attr_accessor :group_unmonitor_noblock
|
|
82
|
+
attr_accessor :group_name
|
|
337
83
|
|
|
338
84
|
# Return the processes made for the process's children
|
|
339
85
|
#
|
|
@@ -358,60 +104,6 @@ module Bluepill
|
|
|
358
104
|
# @api private
|
|
359
105
|
attr_reader :logger
|
|
360
106
|
|
|
361
|
-
state_machine initial: :unmonitored do
|
|
362
|
-
# These are the idle states, i.e. only an event (either external or internal) will trigger a transition.
|
|
363
|
-
# The distinction between down and unmonitored is that down
|
|
364
|
-
# means we know it is not running and unmonitored is that we don't care if it's running.
|
|
365
|
-
state :unmonitored, :up, :down
|
|
366
|
-
|
|
367
|
-
# These are transitionary states, we expect the process to change state after a certain period of time.
|
|
368
|
-
state :starting, :stopping, :restarting
|
|
369
|
-
|
|
370
|
-
event :tick do
|
|
371
|
-
transition starting: :up, if: :process_running?
|
|
372
|
-
transition starting: :down, unless: :process_running?
|
|
373
|
-
|
|
374
|
-
transition up: :up, if: :process_running?
|
|
375
|
-
transition up: :down, unless: :process_running?
|
|
376
|
-
|
|
377
|
-
# The process failed to die after entering the stopping state. Change the state to reflect
|
|
378
|
-
# reality.
|
|
379
|
-
transition stopping: :up, if: :process_running?
|
|
380
|
-
transition stopping: :down, unless: :process_running?
|
|
381
|
-
|
|
382
|
-
transition down: :up, if: :process_running?
|
|
383
|
-
transition down: :starting, unless: :process_running?
|
|
384
|
-
|
|
385
|
-
transition restarting: :up, if: :process_running?
|
|
386
|
-
transition restarting: :down, unless: :process_running?
|
|
387
|
-
end
|
|
388
|
-
|
|
389
|
-
event :start do
|
|
390
|
-
transition [:unmonitored, :down] => :starting
|
|
391
|
-
end
|
|
392
|
-
|
|
393
|
-
event :stop do
|
|
394
|
-
transition up: :stopping
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
event :unmonitor do
|
|
398
|
-
transition any => :unmonitored
|
|
399
|
-
end
|
|
400
|
-
|
|
401
|
-
event :restart do
|
|
402
|
-
transition [:up, :down] => :restarting
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
before_transition any => any, do: :notify_triggers
|
|
406
|
-
before_transition stopping: any, do: :clean_threads
|
|
407
|
-
|
|
408
|
-
after_transition any => :starting, do: :start_process
|
|
409
|
-
after_transition any => :stopping, do: :stop_process
|
|
410
|
-
after_transition any => :restarting, do: :restart_process
|
|
411
|
-
|
|
412
|
-
after_transition any => any, do: :record_transition
|
|
413
|
-
end
|
|
414
|
-
|
|
415
107
|
# Create a process from its checks and options
|
|
416
108
|
#
|
|
417
109
|
# Each check is a watch, or a trigger if one has its name. Options that
|
|
@@ -430,36 +122,13 @@ module Bluepill
|
|
|
430
122
|
def initialize(process_name, checks, options = {})
|
|
431
123
|
@name = process_name
|
|
432
124
|
@event_mutex = Monitor.new
|
|
433
|
-
@watches = []
|
|
434
|
-
@triggers = []
|
|
435
125
|
@children = []
|
|
436
126
|
@threads = []
|
|
437
127
|
@statistics = ProcessStatistics.new
|
|
438
128
|
@actual_pid = options[:actual_pid]
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if Trigger[name]
|
|
443
|
-
add_trigger(name, opts)
|
|
444
|
-
else
|
|
445
|
-
add_watch(name, opts)
|
|
446
|
-
end
|
|
447
|
-
end
|
|
448
|
-
|
|
449
|
-
# These defaults are overriden below if it's configured to be something else.
|
|
450
|
-
@monitor_children = false
|
|
451
|
-
@cache_actual_pid = true
|
|
452
|
-
@start_grace_time = @stop_grace_time = @restart_grace_time = 3
|
|
453
|
-
@environment = {}
|
|
454
|
-
@on_start_timeout = 'start'
|
|
455
|
-
@group_start_noblock = @group_stop_noblock = @group_restart_noblock = @group_unmonitor_noblock = true
|
|
456
|
-
|
|
457
|
-
CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
|
|
458
|
-
public_send("#{attribute_name}=", options.fetch(attribute_name)) if options.key?(attribute_name)
|
|
459
|
-
end
|
|
460
|
-
|
|
461
|
-
# Let state_machine do its initialization stuff
|
|
462
|
-
super() # no arguments intentional
|
|
129
|
+
add_checks(checks, options[:logger])
|
|
130
|
+
@state = :unmonitored
|
|
131
|
+
configure(options)
|
|
463
132
|
end
|
|
464
133
|
|
|
465
134
|
# Check on the process once a tick
|
|
@@ -467,10 +136,6 @@ module Bluepill
|
|
|
467
136
|
# Unless ticks are being skipped, fire the state machine's tick event, and
|
|
468
137
|
# then, for a process that's up, run its watches and tick its children.
|
|
469
138
|
#
|
|
470
|
-
# An error, such as one a trigger raised, is logged with its backtrace,
|
|
471
|
-
# and the process ticks again next time, so one broken check or trigger
|
|
472
|
-
# doesn't stop the daemon.
|
|
473
|
-
#
|
|
474
139
|
# @return [void]
|
|
475
140
|
#
|
|
476
141
|
# @api private
|
|
@@ -485,9 +150,7 @@ module Bluepill
|
|
|
485
150
|
# Deal with thread cleanup here since the stopping state isn't used
|
|
486
151
|
clean_threads if unmonitored?
|
|
487
152
|
|
|
488
|
-
|
|
489
|
-
# module it creates, which the signatures can't name.
|
|
490
|
-
super # steep:ignore UnexpectedSuper
|
|
153
|
+
fire_event(:tick)
|
|
491
154
|
|
|
492
155
|
return unless up?
|
|
493
156
|
|
|
@@ -497,8 +160,6 @@ module Bluepill
|
|
|
497
160
|
|
|
498
161
|
refresh_children!
|
|
499
162
|
children.each(&:tick)
|
|
500
|
-
rescue StandardError => e
|
|
501
|
-
Util.log_error(logger, 'tick failed', e)
|
|
502
163
|
end
|
|
503
164
|
|
|
504
165
|
# Set the logger, for the process and for its watches and triggers
|
|
@@ -510,7 +171,7 @@ module Bluepill
|
|
|
510
171
|
# @api private
|
|
511
172
|
def logger=(logger)
|
|
512
173
|
@logger = logger
|
|
513
|
-
watches.
|
|
174
|
+
watches.logger = logger
|
|
514
175
|
triggers.each { |t| t.logger = logger }
|
|
515
176
|
end
|
|
516
177
|
|
|
@@ -525,177 +186,73 @@ module Bluepill
|
|
|
525
186
|
def dispatch!(event, reason = nil)
|
|
526
187
|
@event_mutex.synchronize do
|
|
527
188
|
@statistics.record_event(event, reason)
|
|
528
|
-
|
|
189
|
+
# The reason, for notify_command
|
|
190
|
+
@transition_reason = reason
|
|
191
|
+
begin
|
|
192
|
+
public_send(event)
|
|
193
|
+
ensure
|
|
194
|
+
@transition_reason = nil
|
|
195
|
+
end
|
|
529
196
|
end
|
|
530
197
|
end
|
|
531
198
|
|
|
532
|
-
#
|
|
199
|
+
# Set the logger, and add each check
|
|
533
200
|
#
|
|
534
|
-
# A
|
|
535
|
-
#
|
|
201
|
+
# A check is a trigger if one has its name, and a watch otherwise. Each
|
|
202
|
+
# gets the logger.
|
|
536
203
|
#
|
|
537
|
-
# @param [
|
|
204
|
+
# @param [Hash{Symbol => Hash{Symbol => Object}}] checks options by
|
|
205
|
+
# check name
|
|
206
|
+
# @param [Logger] logger the logger to use
|
|
538
207
|
#
|
|
539
208
|
# @return [void]
|
|
540
209
|
#
|
|
541
210
|
# @api private
|
|
542
|
-
def
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
logger.warning 'Clearing child list'
|
|
553
|
-
children.clear
|
|
211
|
+
def add_checks(checks, logger)
|
|
212
|
+
@watches = Watches.new
|
|
213
|
+
@triggers = []
|
|
214
|
+
self.logger = logger
|
|
215
|
+
checks.each do |name, opts|
|
|
216
|
+
if Trigger[name]
|
|
217
|
+
add_trigger(name, opts)
|
|
218
|
+
else
|
|
219
|
+
watches.add(name, opts)
|
|
220
|
+
end
|
|
554
221
|
end
|
|
555
|
-
logger.info "Going from #{transition.from_name} => #{transition.to_name}"
|
|
556
|
-
end
|
|
557
|
-
|
|
558
|
-
# Tell every trigger about a transition
|
|
559
|
-
#
|
|
560
|
-
# An error a trigger raises stops the transition, and tick logs it.
|
|
561
|
-
#
|
|
562
|
-
# @param [#from_name, #to_name, #loopback?] transition the transition
|
|
563
|
-
#
|
|
564
|
-
# @return [void]
|
|
565
|
-
#
|
|
566
|
-
# @raise [StandardError] what a trigger raised
|
|
567
|
-
#
|
|
568
|
-
# @api private
|
|
569
|
-
def notify_triggers(transition)
|
|
570
|
-
triggers.each { |trigger| trigger.notify(transition) }
|
|
571
|
-
end
|
|
572
|
-
|
|
573
|
-
# Add a check to run on the process every interval
|
|
574
|
-
#
|
|
575
|
-
# @param [Symbol] name the check's name, such as :mem_usage
|
|
576
|
-
# @param [Hash{Symbol => Object}] options the check's options
|
|
577
|
-
#
|
|
578
|
-
# @return [Array<ConditionWatch>] the process's watches
|
|
579
|
-
#
|
|
580
|
-
# @api private
|
|
581
|
-
def add_watch(name, options)
|
|
582
|
-
watches << ConditionWatch.new(name, options.merge(logger: logger))
|
|
583
222
|
end
|
|
584
223
|
|
|
585
224
|
# Add a trigger to notify of the process's transitions
|
|
586
225
|
#
|
|
587
|
-
# A trigger that a config defines gets transitions that warn about what
|
|
588
|
-
# bluepill 0.2.0's don't have. Bluepill's own, Flapping, gets them as
|
|
589
|
-
# they are.
|
|
590
|
-
#
|
|
591
226
|
# @param [Symbol] name the trigger's name, such as :flapping
|
|
592
227
|
# @param [Hash{Symbol => Object}] options the trigger's options
|
|
593
228
|
#
|
|
594
|
-
# @return [Array<Trigger
|
|
229
|
+
# @return [Array<Trigger>] the process's triggers
|
|
595
230
|
#
|
|
596
231
|
# @api private
|
|
597
232
|
def add_trigger(name, options)
|
|
598
|
-
|
|
599
|
-
triggers << (trigger.instance_of?(Triggers::Flapping) ? trigger : Trigger::Custom.new(trigger))
|
|
233
|
+
triggers << Trigger[name].new(self, options.merge(logger: logger))
|
|
600
234
|
end
|
|
601
235
|
|
|
602
|
-
# Run every watch, and dispatch the events they fire
|
|
236
|
+
# Run every watch that's due, and dispatch the events they fire
|
|
603
237
|
#
|
|
604
|
-
#
|
|
605
|
-
#
|
|
238
|
+
# Once an event changes the process's state, the events after it are
|
|
239
|
+
# dropped, since the watches asked for them of the state it left.
|
|
606
240
|
#
|
|
607
241
|
# @return [void]
|
|
608
242
|
#
|
|
609
243
|
# @api private
|
|
610
244
|
def run_watches
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
threads = watches.collect do |watch|
|
|
614
|
-
[watch, Thread.new { watch.run(actual_pid, now) }] #: [ConditionWatch, Thread]
|
|
615
|
-
end
|
|
245
|
+
events = watches.run(Integer(Time.now)) { actual_pid }
|
|
246
|
+
return if events.empty?
|
|
616
247
|
|
|
617
248
|
@transitioned = false
|
|
618
|
-
|
|
619
|
-
threads.each_with_object([]) do |(watch, thread), events|
|
|
620
|
-
watch_events = thread.value #: Array[Symbol]
|
|
621
|
-
next if watch_events.empty?
|
|
622
|
-
|
|
623
|
-
logger.info "#{watch.name} dispatched: #{watch_events.join(',')}"
|
|
624
|
-
watch_events.each do |event|
|
|
625
|
-
events << [event, watch.to_s]
|
|
626
|
-
end
|
|
627
|
-
end.each do |event, reason| # rubocop:disable Style/MultilineBlockChain
|
|
249
|
+
events.each do |event, reason|
|
|
628
250
|
break if @transitioned
|
|
629
251
|
|
|
630
252
|
dispatch!(event, reason)
|
|
631
253
|
end
|
|
632
254
|
end
|
|
633
255
|
|
|
634
|
-
# Set the state to up, down or unmonitored when bluepill loads
|
|
635
|
-
#
|
|
636
|
-
# A running process is up. Otherwise it's down, so that it starts, unless
|
|
637
|
-
# auto_start is false.
|
|
638
|
-
#
|
|
639
|
-
# @return [void]
|
|
640
|
-
#
|
|
641
|
-
# @api private
|
|
642
|
-
def determine_initial_state
|
|
643
|
-
if process_running?(true)
|
|
644
|
-
self.state = 'up'
|
|
645
|
-
return
|
|
646
|
-
end
|
|
647
|
-
self.state = auto_start.equal?(false) ? 'unmonitored' : 'down' # we need to check for false value
|
|
648
|
-
end
|
|
649
|
-
|
|
650
|
-
# Carry out a command from the command line
|
|
651
|
-
#
|
|
652
|
-
# @param [String] cmd start, stop, restart or unmonitor
|
|
653
|
-
#
|
|
654
|
-
# @return [void]
|
|
655
|
-
#
|
|
656
|
-
# @api private
|
|
657
|
-
def handle_user_command(cmd)
|
|
658
|
-
case cmd
|
|
659
|
-
when 'start'
|
|
660
|
-
if process_running?(true)
|
|
661
|
-
logger.warning('Refusing to re-run start command on an already running process.')
|
|
662
|
-
else
|
|
663
|
-
dispatch!(:start, 'user initiated')
|
|
664
|
-
end
|
|
665
|
-
when 'stop'
|
|
666
|
-
stop_process
|
|
667
|
-
dispatch!(:unmonitor, 'user initiated')
|
|
668
|
-
when 'restart'
|
|
669
|
-
# Through the state machine, like a check's restart, so the process
|
|
670
|
-
# shows as restarting until it's up again. A state restart can't
|
|
671
|
-
# leave, such as unmonitored, restarts it directly.
|
|
672
|
-
restart_process unless dispatch!(:restart, 'user initiated')
|
|
673
|
-
when 'unmonitor'
|
|
674
|
-
# When the user issues an unmonitor cmd, reset any triggers so that
|
|
675
|
-
# scheduled events gets cleared
|
|
676
|
-
triggers.each(&:reset!)
|
|
677
|
-
dispatch!(:unmonitor, 'user initiated')
|
|
678
|
-
end
|
|
679
|
-
end
|
|
680
|
-
|
|
681
|
-
# Return whether the process is running, and forget its pid if not
|
|
682
|
-
#
|
|
683
|
-
# The answer is kept for the rest of the tick.
|
|
684
|
-
#
|
|
685
|
-
# @param [Boolean] force whether to check again this tick
|
|
686
|
-
#
|
|
687
|
-
# @return [Boolean]
|
|
688
|
-
#
|
|
689
|
-
# @api private
|
|
690
|
-
def process_running?(force = false)
|
|
691
|
-
@process_running = nil if force # clear existing state if forced
|
|
692
|
-
|
|
693
|
-
@process_running ||= signal_process(0)
|
|
694
|
-
# the process isn't running, so we should clear the PID
|
|
695
|
-
clear_pid unless @process_running
|
|
696
|
-
@process_running #: bool
|
|
697
|
-
end
|
|
698
|
-
|
|
699
256
|
# Kill the threads the process's commands left running
|
|
700
257
|
#
|
|
701
258
|
# @return [void]
|
|
@@ -715,136 +272,6 @@ module Bluepill
|
|
|
715
272
|
!!daemonize
|
|
716
273
|
end
|
|
717
274
|
|
|
718
|
-
# Return whether bluepill watches the process's children
|
|
719
|
-
#
|
|
720
|
-
# @return [Boolean]
|
|
721
|
-
#
|
|
722
|
-
# @api private
|
|
723
|
-
def monitor_children?
|
|
724
|
-
!!monitor_children
|
|
725
|
-
end
|
|
726
|
-
|
|
727
|
-
# Send a signal to the process
|
|
728
|
-
#
|
|
729
|
-
# A process without a pid isn't running, so it gets no signal and nothing
|
|
730
|
-
# is logged. A failure is logged.
|
|
731
|
-
#
|
|
732
|
-
# @param [Integer, String, Symbol] code the signal, such as 0 or :term
|
|
733
|
-
#
|
|
734
|
-
# @return [Boolean] whether the signal was sent
|
|
735
|
-
#
|
|
736
|
-
# @api private
|
|
737
|
-
def signal_process(code)
|
|
738
|
-
code = code.to_s.upcase if code.is_a?(String) || code.instance_of?(Symbol)
|
|
739
|
-
pid = actual_pid
|
|
740
|
-
# Without a pid the process isn't running. That's expected before the
|
|
741
|
-
# first start, and actual_pid already warns about a missing pid file.
|
|
742
|
-
return false unless pid
|
|
743
|
-
|
|
744
|
-
# kill with a pid of 0 or less signals a whole process group, which
|
|
745
|
-
# could include bluepill itself, so only signal a real pid
|
|
746
|
-
raise "invalid pid #{pid}" unless pid > 0
|
|
747
|
-
|
|
748
|
-
::Process.kill(code, pid)
|
|
749
|
-
true
|
|
750
|
-
rescue StandardError => e
|
|
751
|
-
logger.err "Failed to signal process #{pid} with code #{code}: #{e}"
|
|
752
|
-
false
|
|
753
|
-
end
|
|
754
|
-
|
|
755
|
-
# Return whether the pid is kept once it's read
|
|
756
|
-
#
|
|
757
|
-
# @return [Boolean]
|
|
758
|
-
#
|
|
759
|
-
# @api private
|
|
760
|
-
def cache_actual_pid?
|
|
761
|
-
!!@cache_actual_pid
|
|
762
|
-
end
|
|
763
|
-
|
|
764
|
-
# Return the process's pid, from pid_command or the pid file
|
|
765
|
-
#
|
|
766
|
-
# @return [Integer, nil] the pid, or nil if there's none yet
|
|
767
|
-
#
|
|
768
|
-
# @api private
|
|
769
|
-
def actual_pid
|
|
770
|
-
pid_command ? pid_from_command : pid_from_file
|
|
771
|
-
end
|
|
772
|
-
|
|
773
|
-
# Return the pid in the pid file, cached unless cache_actual_pid is false
|
|
774
|
-
#
|
|
775
|
-
# A missing pid file is logged as a warning.
|
|
776
|
-
#
|
|
777
|
-
# @return [Integer, nil] the pid, or nil without a pid file or a pid
|
|
778
|
-
#
|
|
779
|
-
# @api private
|
|
780
|
-
def pid_from_file
|
|
781
|
-
return @actual_pid if cache_actual_pid? && @actual_pid
|
|
782
|
-
|
|
783
|
-
@actual_pid = if pid_file
|
|
784
|
-
if File.exist?(pid_file)
|
|
785
|
-
positive_pid(File.read(pid_file).to_i)
|
|
786
|
-
else
|
|
787
|
-
logger.warning("pid_file #{pid_file} does not exist or cannot be read")
|
|
788
|
-
nil
|
|
789
|
-
end
|
|
790
|
-
end
|
|
791
|
-
end
|
|
792
|
-
|
|
793
|
-
# Return the pid that pid_command prints
|
|
794
|
-
#
|
|
795
|
-
# Output that isn't only digits gives no pid.
|
|
796
|
-
#
|
|
797
|
-
# @return [Integer, nil] the pid, or nil if the output isn't one
|
|
798
|
-
#
|
|
799
|
-
# @api private
|
|
800
|
-
def pid_from_command
|
|
801
|
-
# Output that is not just digits reads as nil, so as 0
|
|
802
|
-
positive_pid(`#{pid_command}`.strip[/\A\d+\z/].to_i)
|
|
803
|
-
end
|
|
804
|
-
|
|
805
|
-
# Return the pid, unless it's 0 or less
|
|
806
|
-
#
|
|
807
|
-
# An empty or garbled pid file reads as 0, which is never a real process.
|
|
808
|
-
#
|
|
809
|
-
# @param [Integer] pid the pid that was read
|
|
810
|
-
#
|
|
811
|
-
# @return [Integer, nil] the pid, or nil if it's not a real one
|
|
812
|
-
#
|
|
813
|
-
# @api private
|
|
814
|
-
def positive_pid(pid)
|
|
815
|
-
pid if pid > 0
|
|
816
|
-
end
|
|
817
|
-
|
|
818
|
-
# Set the pid, and add it to the process's journal
|
|
819
|
-
#
|
|
820
|
-
# @param [Integer] pid the process's pid
|
|
821
|
-
#
|
|
822
|
-
# @return [void]
|
|
823
|
-
#
|
|
824
|
-
# @api private
|
|
825
|
-
def actual_pid=(pid)
|
|
826
|
-
ProcessJournal.append_pid_to_journal(name, pid) # be sure to always log the pid
|
|
827
|
-
@actual_pid = pid
|
|
828
|
-
end
|
|
829
|
-
|
|
830
|
-
# Forget the process's pid
|
|
831
|
-
#
|
|
832
|
-
# @return [nil]
|
|
833
|
-
#
|
|
834
|
-
# @api private
|
|
835
|
-
def clear_pid
|
|
836
|
-
@actual_pid = nil
|
|
837
|
-
end
|
|
838
|
-
|
|
839
|
-
# Delete the pid file, if there is one
|
|
840
|
-
#
|
|
841
|
-
# @return [void]
|
|
842
|
-
#
|
|
843
|
-
# @api private
|
|
844
|
-
def unlink_pid
|
|
845
|
-
System.delete_if_exists(pid_file)
|
|
846
|
-
end
|
|
847
|
-
|
|
848
275
|
# Skip ticks for a while, such as for a command's grace time
|
|
849
276
|
#
|
|
850
277
|
# The times add up, so a stop followed by a start, which is how
|
|
@@ -853,11 +280,11 @@ module Bluepill
|
|
|
853
280
|
#
|
|
854
281
|
# @param [Integer, Float] seconds how long to skip ticks for
|
|
855
282
|
#
|
|
856
|
-
# @return [Integer] when ticks resume,
|
|
283
|
+
# @return [Integer] when ticks resume, in epoch seconds
|
|
857
284
|
#
|
|
858
285
|
# @api private
|
|
859
286
|
def skip_ticks_for(seconds)
|
|
860
|
-
self.skip_ticks_until = (skip_ticks_until ||
|
|
287
|
+
self.skip_ticks_until = (skip_ticks_until || Integer(Time.now)) + Integer(seconds)
|
|
861
288
|
end
|
|
862
289
|
|
|
863
290
|
# Return whether ticks are being skipped
|
|
@@ -866,37 +293,7 @@ module Bluepill
|
|
|
866
293
|
#
|
|
867
294
|
# @api private
|
|
868
295
|
def skipping_ticks?
|
|
869
|
-
skip_ticks_until && skip_ticks_until >
|
|
870
|
-
end
|
|
871
|
-
|
|
872
|
-
# Update the list of children from the tick's ps table
|
|
873
|
-
#
|
|
874
|
-
# Children that are no longer running are dropped, and a Process is made
|
|
875
|
-
# for each new one, with the checks monitor_children gave.
|
|
876
|
-
#
|
|
877
|
-
# @return [void]
|
|
878
|
-
#
|
|
879
|
-
# @api private
|
|
880
|
-
def refresh_children!
|
|
881
|
-
# First prune the list of dead children
|
|
882
|
-
@children.delete_if { |child| !child.process_running?(true) }
|
|
883
|
-
|
|
884
|
-
# Add new found children to the list
|
|
885
|
-
new_children_pids = System.get_children(actual_pid) - @children.collect(&:actual_pid)
|
|
886
|
-
|
|
887
|
-
logger.info "Existing children: #{@children.collect(&:actual_pid).join(',')}. Got new children: #{new_children_pids} for #{actual_pid}" unless new_children_pids.empty?
|
|
888
|
-
|
|
889
|
-
# Construct a new process wrapper for each new found children
|
|
890
|
-
# monitor_children sets the factory
|
|
891
|
-
factory = child_process_factory #: ProcessFactory
|
|
892
|
-
new_children_pids.each do |child_pid|
|
|
893
|
-
ProcessJournal.append_pid_to_journal(name, child_pid) if daemonize?
|
|
894
|
-
child_name = "<child(pid:#{child_pid})>"
|
|
895
|
-
child_logger = logger.prefix_with(child_name)
|
|
896
|
-
|
|
897
|
-
child = factory.create_child_process(child_name, child_pid, child_logger)
|
|
898
|
-
@children << child
|
|
899
|
-
end
|
|
296
|
+
skip_ticks_until && skip_ticks_until > Integer(Time.now)
|
|
900
297
|
end
|
|
901
298
|
end
|
|
902
299
|
end
|