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
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
class Process
|
|
3
|
+
# The options a config can set for a Process, and their defaults
|
|
4
|
+
#
|
|
5
|
+
# @api private
|
|
6
|
+
module Attributes
|
|
7
|
+
# The options a config can set for a process
|
|
8
|
+
CONFIGURABLE_ATTRIBUTES = %i[
|
|
9
|
+
pre_start_command
|
|
10
|
+
start_command
|
|
11
|
+
stop_command
|
|
12
|
+
restart_command
|
|
13
|
+
|
|
14
|
+
post_start_command
|
|
15
|
+
pre_stop_command
|
|
16
|
+
post_stop_command
|
|
17
|
+
notify_command
|
|
18
|
+
hook_timeout
|
|
19
|
+
|
|
20
|
+
stdout
|
|
21
|
+
stderr
|
|
22
|
+
stdin
|
|
23
|
+
|
|
24
|
+
daemonize
|
|
25
|
+
pid_file
|
|
26
|
+
working_dir
|
|
27
|
+
environment
|
|
28
|
+
|
|
29
|
+
start_grace_time
|
|
30
|
+
stop_grace_time
|
|
31
|
+
restart_grace_time
|
|
32
|
+
|
|
33
|
+
uid
|
|
34
|
+
gid
|
|
35
|
+
|
|
36
|
+
cache_actual_pid
|
|
37
|
+
|
|
38
|
+
monitor_children
|
|
39
|
+
child_process_factory
|
|
40
|
+
|
|
41
|
+
pid_command
|
|
42
|
+
auto_start
|
|
43
|
+
|
|
44
|
+
supplementary_groups
|
|
45
|
+
|
|
46
|
+
stop_signals
|
|
47
|
+
|
|
48
|
+
on_start_timeout
|
|
49
|
+
|
|
50
|
+
group_start_noblock
|
|
51
|
+
group_restart_noblock
|
|
52
|
+
group_stop_noblock
|
|
53
|
+
group_unmonitor_noblock
|
|
54
|
+
].freeze
|
|
55
|
+
|
|
56
|
+
# Return the command run before every start, if any
|
|
57
|
+
#
|
|
58
|
+
# Bluepill waits for it to finish, and starts the process even if it exits
|
|
59
|
+
# with a non-zero status. It runs in the process's working_dir, with its
|
|
60
|
+
# environment, uid and gid.
|
|
61
|
+
#
|
|
62
|
+
# @return [String, nil]
|
|
63
|
+
#
|
|
64
|
+
# @api private
|
|
65
|
+
attr_accessor :pre_start_command
|
|
66
|
+
|
|
67
|
+
# Return the command that starts the process
|
|
68
|
+
#
|
|
69
|
+
# Bluepill splits it into words and runs it without a shell.
|
|
70
|
+
#
|
|
71
|
+
# @return [String, nil]
|
|
72
|
+
#
|
|
73
|
+
# @api private
|
|
74
|
+
attr_accessor :start_command
|
|
75
|
+
|
|
76
|
+
# Return the command run each time the process comes up after a start
|
|
77
|
+
#
|
|
78
|
+
# It runs when the process reaches up, after a start or a restart, so a
|
|
79
|
+
# server that daemonizes itself has written its pid file by then.
|
|
80
|
+
# <tt>{{PID}}</tt> in it is replaced with the process's pid. It runs in
|
|
81
|
+
# the background, and a failure is only logged.
|
|
82
|
+
#
|
|
83
|
+
# @return [String, nil]
|
|
84
|
+
#
|
|
85
|
+
# @api private
|
|
86
|
+
attr_accessor :post_start_command
|
|
87
|
+
|
|
88
|
+
# Return the command run before the process is stopped, if any
|
|
89
|
+
#
|
|
90
|
+
# The stop waits for it to finish, for up to hook_timeout, when it's
|
|
91
|
+
# killed, and then goes ahead either way. <tt>{{PID}}</tt> in it is replaced with the
|
|
92
|
+
# process's pid.
|
|
93
|
+
#
|
|
94
|
+
# @return [String, nil]
|
|
95
|
+
#
|
|
96
|
+
# @api private
|
|
97
|
+
attr_accessor :pre_stop_command
|
|
98
|
+
|
|
99
|
+
# Return the command run after the process has stopped, if any
|
|
100
|
+
#
|
|
101
|
+
# It runs in the background once bluepill has cleaned up after the
|
|
102
|
+
# process. <tt>{{PID}}</tt> in it is replaced with the pid that stopped.
|
|
103
|
+
#
|
|
104
|
+
# @return [String, nil]
|
|
105
|
+
#
|
|
106
|
+
# @api private
|
|
107
|
+
attr_accessor :post_stop_command
|
|
108
|
+
|
|
109
|
+
# Return the command run each time the process changes state, if any
|
|
110
|
+
#
|
|
111
|
+
# It runs in the background, with BLUEPILL_APP, BLUEPILL_GROUP,
|
|
112
|
+
# BLUEPILL_PROCESS, BLUEPILL_FROM, BLUEPILL_TO, BLUEPILL_REASON and
|
|
113
|
+
# BLUEPILL_PID in its environment. An app can set it for all its
|
|
114
|
+
# processes.
|
|
115
|
+
#
|
|
116
|
+
# @return [String, nil]
|
|
117
|
+
#
|
|
118
|
+
# @api private
|
|
119
|
+
attr_accessor :notify_command
|
|
120
|
+
|
|
121
|
+
# Return how long a hook command may run before bluepill kills it
|
|
122
|
+
#
|
|
123
|
+
# It applies to post_start_command, pre_stop_command, post_stop_command
|
|
124
|
+
# and notify_command. The default is 30 seconds.
|
|
125
|
+
#
|
|
126
|
+
# @return [Integer, Float]
|
|
127
|
+
#
|
|
128
|
+
# @api private
|
|
129
|
+
attr_accessor :hook_timeout
|
|
130
|
+
|
|
131
|
+
# Return the command that stops the process, if any
|
|
132
|
+
#
|
|
133
|
+
# <tt>{{PID}}</tt> in it is replaced with the process's pid. Without
|
|
134
|
+
# one, bluepill sends stop_signals, or TERM.
|
|
135
|
+
#
|
|
136
|
+
# @return [String, nil]
|
|
137
|
+
#
|
|
138
|
+
# @api private
|
|
139
|
+
attr_accessor :stop_command
|
|
140
|
+
|
|
141
|
+
# Return the command that restarts the process, if any
|
|
142
|
+
#
|
|
143
|
+
# <tt>{{PID}}</tt> in it is replaced with the process's pid. Without
|
|
144
|
+
# one, bluepill stops the process and starts it again.
|
|
145
|
+
#
|
|
146
|
+
# @return [String, nil]
|
|
147
|
+
#
|
|
148
|
+
# @api private
|
|
149
|
+
attr_accessor :restart_command
|
|
150
|
+
|
|
151
|
+
# Return the file a daemonized process's standard output goes to
|
|
152
|
+
#
|
|
153
|
+
# @return [String, nil]
|
|
154
|
+
#
|
|
155
|
+
# @api private
|
|
156
|
+
attr_accessor :stdout
|
|
157
|
+
|
|
158
|
+
# Return the file a daemonized process's standard error goes to
|
|
159
|
+
#
|
|
160
|
+
# @return [String, nil]
|
|
161
|
+
#
|
|
162
|
+
# @api private
|
|
163
|
+
attr_accessor :stderr
|
|
164
|
+
|
|
165
|
+
# Return the file a daemonized process's standard input comes from
|
|
166
|
+
#
|
|
167
|
+
# @return [String, nil]
|
|
168
|
+
#
|
|
169
|
+
# @api private
|
|
170
|
+
attr_accessor :stdin
|
|
171
|
+
|
|
172
|
+
# Return whether bluepill daemonizes the process and writes its pid file
|
|
173
|
+
#
|
|
174
|
+
# Set it for a process that doesn't daemonize itself.
|
|
175
|
+
#
|
|
176
|
+
# @return [Boolean, nil]
|
|
177
|
+
#
|
|
178
|
+
# @api private
|
|
179
|
+
attr_accessor :daemonize
|
|
180
|
+
|
|
181
|
+
# Return the file that holds the process's pid
|
|
182
|
+
#
|
|
183
|
+
# @return [String, nil]
|
|
184
|
+
#
|
|
185
|
+
# @api private
|
|
186
|
+
attr_accessor :pid_file
|
|
187
|
+
|
|
188
|
+
# Return the directory the process's commands run in
|
|
189
|
+
#
|
|
190
|
+
# A daemonized process starts in / unless this is set.
|
|
191
|
+
#
|
|
192
|
+
# @return [String, nil]
|
|
193
|
+
#
|
|
194
|
+
# @api private
|
|
195
|
+
attr_accessor :working_dir
|
|
196
|
+
|
|
197
|
+
# Return the environment variables the process's commands get
|
|
198
|
+
#
|
|
199
|
+
# @return [Hash{#to_s => #to_s}, nil]
|
|
200
|
+
#
|
|
201
|
+
# @api private
|
|
202
|
+
attr_accessor :environment
|
|
203
|
+
|
|
204
|
+
# Return how long to wait for the process to start, in seconds
|
|
205
|
+
#
|
|
206
|
+
# Bluepill skips ticks for this long after a start. The default is 3.
|
|
207
|
+
#
|
|
208
|
+
# @return [Integer, Float]
|
|
209
|
+
#
|
|
210
|
+
# @api private
|
|
211
|
+
attr_accessor :start_grace_time
|
|
212
|
+
|
|
213
|
+
# Return how long to wait for the process to stop, in seconds
|
|
214
|
+
#
|
|
215
|
+
# Bluepill skips ticks for this long after a stop. The default is 3.
|
|
216
|
+
#
|
|
217
|
+
# @return [Integer, Float]
|
|
218
|
+
#
|
|
219
|
+
# @api private
|
|
220
|
+
attr_accessor :stop_grace_time
|
|
221
|
+
|
|
222
|
+
# Return how long to wait for the process to restart, in seconds
|
|
223
|
+
#
|
|
224
|
+
# Bluepill skips ticks for this long after a restart. The default is 3.
|
|
225
|
+
#
|
|
226
|
+
# @return [Integer, Float]
|
|
227
|
+
#
|
|
228
|
+
# @api private
|
|
229
|
+
attr_accessor :restart_grace_time
|
|
230
|
+
|
|
231
|
+
# Return the user the process's commands run as
|
|
232
|
+
#
|
|
233
|
+
# @return [String, nil]
|
|
234
|
+
#
|
|
235
|
+
# @api private
|
|
236
|
+
attr_accessor :uid
|
|
237
|
+
|
|
238
|
+
# Return the group the process's commands run as
|
|
239
|
+
#
|
|
240
|
+
# @return [String, nil]
|
|
241
|
+
#
|
|
242
|
+
# @api private
|
|
243
|
+
attr_accessor :gid
|
|
244
|
+
|
|
245
|
+
# Return whether to read the pid file only until it gives a pid
|
|
246
|
+
#
|
|
247
|
+
# True by default. Set it to false for a process whose pid changes, such
|
|
248
|
+
# as one checked for zombies.
|
|
249
|
+
#
|
|
250
|
+
# @return [Boolean]
|
|
251
|
+
#
|
|
252
|
+
# @api private
|
|
253
|
+
attr_accessor :cache_actual_pid
|
|
254
|
+
|
|
255
|
+
# Return whether bluepill also watches the process's children
|
|
256
|
+
#
|
|
257
|
+
# @return [Boolean]
|
|
258
|
+
#
|
|
259
|
+
# @api private
|
|
260
|
+
attr_accessor :monitor_children
|
|
261
|
+
|
|
262
|
+
# Return the factory that makes a Process for each child found
|
|
263
|
+
#
|
|
264
|
+
# monitor_children sets it, with the children's checks.
|
|
265
|
+
#
|
|
266
|
+
# @return [ProcessFactory, nil]
|
|
267
|
+
#
|
|
268
|
+
# @api private
|
|
269
|
+
attr_accessor :child_process_factory
|
|
270
|
+
|
|
271
|
+
# Return the command whose output is the process's pid, if any
|
|
272
|
+
#
|
|
273
|
+
# Used instead of pid_file when it's set.
|
|
274
|
+
#
|
|
275
|
+
# @return [String, nil]
|
|
276
|
+
#
|
|
277
|
+
# @api private
|
|
278
|
+
attr_accessor :pid_command
|
|
279
|
+
|
|
280
|
+
# Return whether bluepill starts the process when it loads
|
|
281
|
+
#
|
|
282
|
+
# Only false keeps it from starting: the process is then unmonitored.
|
|
283
|
+
#
|
|
284
|
+
# @return [Boolean, nil]
|
|
285
|
+
#
|
|
286
|
+
# @api private
|
|
287
|
+
attr_accessor :auto_start
|
|
288
|
+
|
|
289
|
+
# Return the extra groups the process's commands run with
|
|
290
|
+
#
|
|
291
|
+
# @return [Array<String>, nil]
|
|
292
|
+
#
|
|
293
|
+
# @api private
|
|
294
|
+
attr_accessor :supplementary_groups
|
|
295
|
+
|
|
296
|
+
# Return the signals that stop the process, with the delays between
|
|
297
|
+
#
|
|
298
|
+
# A signal, then a delay in seconds and a signal, and so on, such as
|
|
299
|
+
# [:quit, 30, :term, 5, :kill]. Each later signal is only sent if the
|
|
300
|
+
# process is still running.
|
|
301
|
+
#
|
|
302
|
+
# @return [Array<Symbol, Integer>, nil]
|
|
303
|
+
#
|
|
304
|
+
# @api private
|
|
305
|
+
attr_accessor :stop_signals
|
|
306
|
+
|
|
307
|
+
# Return the event to dispatch when the start command takes too long
|
|
308
|
+
#
|
|
309
|
+
# The default is start, which tries again.
|
|
310
|
+
#
|
|
311
|
+
# @return [String, Symbol]
|
|
312
|
+
#
|
|
313
|
+
# @api private
|
|
314
|
+
attr_accessor :on_start_timeout
|
|
315
|
+
|
|
316
|
+
# Return whether a group's start runs without waiting for this process
|
|
317
|
+
#
|
|
318
|
+
# @return [Boolean]
|
|
319
|
+
#
|
|
320
|
+
# @api private
|
|
321
|
+
attr_accessor :group_start_noblock
|
|
322
|
+
|
|
323
|
+
# Return whether a group's restart runs without waiting for this process
|
|
324
|
+
#
|
|
325
|
+
# @return [Boolean]
|
|
326
|
+
#
|
|
327
|
+
# @api private
|
|
328
|
+
attr_accessor :group_restart_noblock
|
|
329
|
+
|
|
330
|
+
# Return whether a group's stop runs without waiting for this process
|
|
331
|
+
#
|
|
332
|
+
# @return [Boolean]
|
|
333
|
+
#
|
|
334
|
+
# @api private
|
|
335
|
+
attr_accessor :group_stop_noblock
|
|
336
|
+
|
|
337
|
+
# Return whether a group's unmonitor runs without waiting for it
|
|
338
|
+
#
|
|
339
|
+
# @return [Boolean]
|
|
340
|
+
#
|
|
341
|
+
# @api private
|
|
342
|
+
attr_accessor :group_unmonitor_noblock
|
|
343
|
+
|
|
344
|
+
# Set the defaults, and then the options the config gives
|
|
345
|
+
#
|
|
346
|
+
# @param [Hash{Symbol => Object}] options the process's options, of
|
|
347
|
+
# which those in CONFIGURABLE_ATTRIBUTES are set
|
|
348
|
+
#
|
|
349
|
+
# @return [void]
|
|
350
|
+
#
|
|
351
|
+
# @api private
|
|
352
|
+
def configure(options)
|
|
353
|
+
apply_defaults
|
|
354
|
+
CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
|
|
355
|
+
public_send("#{attribute_name}=", options.fetch(attribute_name)) if options.key?(attribute_name)
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
# Set the defaults of the options that have one
|
|
360
|
+
#
|
|
361
|
+
# The grace times are 3 seconds, hooks get 30, the pid is cached,
|
|
362
|
+
# children are left alone, and a group doesn't wait for the process.
|
|
363
|
+
#
|
|
364
|
+
# @return [void]
|
|
365
|
+
#
|
|
366
|
+
# @api private
|
|
367
|
+
def apply_defaults
|
|
368
|
+
# These defaults are overriden by configure if it's configured to be something else.
|
|
369
|
+
@monitor_children = false
|
|
370
|
+
@cache_actual_pid = true
|
|
371
|
+
@start_grace_time = @stop_grace_time = @restart_grace_time = 3
|
|
372
|
+
@environment = {}
|
|
373
|
+
@on_start_timeout = "start"
|
|
374
|
+
@hook_timeout = 30
|
|
375
|
+
@group_start_noblock = @group_stop_noblock = @group_restart_noblock = @group_unmonitor_noblock = true
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
class Process
|
|
3
|
+
# Keeps a Process for each of a Process's children, when it monitors them
|
|
4
|
+
#
|
|
5
|
+
# @api private
|
|
6
|
+
module Children
|
|
7
|
+
# Return whether bluepill watches the process's children
|
|
8
|
+
#
|
|
9
|
+
# @return [Boolean]
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
def monitor_children?
|
|
13
|
+
!!monitor_children
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Update the list of children from the tick's ps table
|
|
17
|
+
#
|
|
18
|
+
# Children that are no longer running are dropped, and a Process is made
|
|
19
|
+
# for each new one, with the checks monitor_children gave.
|
|
20
|
+
#
|
|
21
|
+
# @return [void]
|
|
22
|
+
#
|
|
23
|
+
# @api private
|
|
24
|
+
def refresh_children!
|
|
25
|
+
# First prune the list of dead children
|
|
26
|
+
@children.delete_if { |child| !child.process_running?(true) }
|
|
27
|
+
|
|
28
|
+
# Add new found children to the list
|
|
29
|
+
new_children_pids = System.get_children(actual_pid) - @children.collect(&:actual_pid)
|
|
30
|
+
return if new_children_pids.empty?
|
|
31
|
+
|
|
32
|
+
logger.info "Existing children: #{@children.collect(&:actual_pid).join(",")}. Got new children: #{new_children_pids} for #{actual_pid}"
|
|
33
|
+
add_children(new_children_pids)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Make a Process for each new child, and add it to the list
|
|
37
|
+
#
|
|
38
|
+
# @param [Array<Integer>] child_pids the new children's pids
|
|
39
|
+
#
|
|
40
|
+
# @return [void]
|
|
41
|
+
#
|
|
42
|
+
# @api private
|
|
43
|
+
def add_children(child_pids)
|
|
44
|
+
# Construct a new process wrapper for each new found children
|
|
45
|
+
# monitor_children sets the factory
|
|
46
|
+
factory = child_process_factory #: ProcessFactory
|
|
47
|
+
child_pids.each do |child_pid|
|
|
48
|
+
@children << new_child(factory, child_pid)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Make a Process for a child
|
|
53
|
+
#
|
|
54
|
+
# A process that bluepill daemonizes also has the child's pid added to
|
|
55
|
+
# its journal.
|
|
56
|
+
#
|
|
57
|
+
# @param [ProcessFactory] factory the factory monitor_children set
|
|
58
|
+
# @param [Integer] child_pid the child's pid
|
|
59
|
+
#
|
|
60
|
+
# @return [Process] the child's process wrapper
|
|
61
|
+
#
|
|
62
|
+
# @api private
|
|
63
|
+
def new_child(factory, child_pid)
|
|
64
|
+
ProcessJournal.append_pid_to_journal(name, child_pid) if daemonize?
|
|
65
|
+
child_name = "<child(pid:#{child_pid})>"
|
|
66
|
+
factory.create_child_process(child_name, child_pid, logger.prefix_with(child_name))
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
module Bluepill
|
|
2
|
+
class Process
|
|
3
|
+
# Finds, signals and forgets a Process's pid
|
|
4
|
+
#
|
|
5
|
+
# The pid comes from pid_command or the pid file. Every tick signals it
|
|
6
|
+
# to see whether the process is still running.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
module Pid
|
|
10
|
+
# Return whether the process is running, and forget its pid if not
|
|
11
|
+
#
|
|
12
|
+
# The answer is kept for the rest of the tick.
|
|
13
|
+
#
|
|
14
|
+
# @param [Boolean] force whether to check again this tick
|
|
15
|
+
#
|
|
16
|
+
# @return [Boolean]
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
19
|
+
def process_running?(force = false)
|
|
20
|
+
@process_running = nil if force # clear existing state if forced
|
|
21
|
+
|
|
22
|
+
@process_running ||= signal_process(0)
|
|
23
|
+
# the process isn't running, so we should clear the PID
|
|
24
|
+
clear_pid unless @process_running
|
|
25
|
+
@process_running #: bool
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Send a signal to the process
|
|
29
|
+
#
|
|
30
|
+
# A process without a pid isn't running, so it gets no signal and nothing
|
|
31
|
+
# is logged. A failure is logged.
|
|
32
|
+
#
|
|
33
|
+
# @param [Integer, String, Symbol] code the signal, such as 0 or :term
|
|
34
|
+
#
|
|
35
|
+
# @return [Boolean] whether the signal was sent
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
def signal_process(code)
|
|
39
|
+
code = code.to_s.upcase if code.is_a?(String) || code.instance_of?(Symbol)
|
|
40
|
+
pid = actual_pid
|
|
41
|
+
# Without a pid the process isn't running. That's expected before the
|
|
42
|
+
# first start, and actual_pid already warns about a missing pid file.
|
|
43
|
+
return false unless pid
|
|
44
|
+
|
|
45
|
+
# kill with a pid of 0 or less signals a whole process group, which
|
|
46
|
+
# could include bluepill itself, so only signal a real pid
|
|
47
|
+
raise "invalid pid #{pid}" unless pid > 0
|
|
48
|
+
|
|
49
|
+
::Process.kill(code, pid)
|
|
50
|
+
true
|
|
51
|
+
rescue => e
|
|
52
|
+
logger.err "Failed to signal process #{pid} with code #{code}: #{e}"
|
|
53
|
+
false
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Return whether the pid is kept once it's read
|
|
57
|
+
#
|
|
58
|
+
# @return [Boolean]
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
def cache_actual_pid?
|
|
62
|
+
!!@cache_actual_pid
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Return the process's pid, from pid_command or the pid file
|
|
66
|
+
#
|
|
67
|
+
# @return [Integer, nil] the pid, or nil if there's none yet
|
|
68
|
+
#
|
|
69
|
+
# @api private
|
|
70
|
+
def actual_pid
|
|
71
|
+
pid_command ? pid_from_command : pid_from_file
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Return the pid in the pid file, cached unless cache_actual_pid is false
|
|
75
|
+
#
|
|
76
|
+
# A missing pid file is logged as a warning.
|
|
77
|
+
#
|
|
78
|
+
# @return [Integer, nil] the pid, or nil without a pid file or a pid
|
|
79
|
+
#
|
|
80
|
+
# @api private
|
|
81
|
+
def pid_from_file
|
|
82
|
+
return @actual_pid if cache_actual_pid? && @actual_pid
|
|
83
|
+
|
|
84
|
+
@actual_pid = if pid_file
|
|
85
|
+
if File.exist?(pid_file)
|
|
86
|
+
positive_pid(File.read(pid_file).to_i)
|
|
87
|
+
else
|
|
88
|
+
logger.warning("pid_file #{pid_file} does not exist or cannot be read")
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Return the pid that pid_command prints
|
|
95
|
+
#
|
|
96
|
+
# Output that isn't only digits gives no pid.
|
|
97
|
+
#
|
|
98
|
+
# @return [Integer, nil] the pid, or nil if the output isn't one
|
|
99
|
+
#
|
|
100
|
+
# @api private
|
|
101
|
+
def pid_from_command
|
|
102
|
+
# Output that is not just digits reads as nil, so as 0
|
|
103
|
+
positive_pid(`#{pid_command}`.strip[/\A\d+\z/].to_i)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Return the pid, unless it's 0 or less
|
|
107
|
+
#
|
|
108
|
+
# An empty or garbled pid file reads as 0, which is never a real process.
|
|
109
|
+
#
|
|
110
|
+
# @param [Integer] pid the pid that was read
|
|
111
|
+
#
|
|
112
|
+
# @return [Integer, nil] the pid, or nil if it's not a real one
|
|
113
|
+
#
|
|
114
|
+
# @api private
|
|
115
|
+
def positive_pid(pid)
|
|
116
|
+
pid if pid > 0
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Set the pid, and add it to the process's journal
|
|
120
|
+
#
|
|
121
|
+
# @param [Integer] pid the process's pid
|
|
122
|
+
#
|
|
123
|
+
# @return [void]
|
|
124
|
+
#
|
|
125
|
+
# @api private
|
|
126
|
+
def actual_pid=(pid)
|
|
127
|
+
ProcessJournal.append_pid_to_journal(name, pid) # be sure to always log the pid
|
|
128
|
+
@actual_pid = pid
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Forget the process's pid
|
|
132
|
+
#
|
|
133
|
+
# The last pid is kept for notify_command, since a process that has died
|
|
134
|
+
# has no pid by the time its state changes.
|
|
135
|
+
#
|
|
136
|
+
# @return [nil]
|
|
137
|
+
#
|
|
138
|
+
# @api private
|
|
139
|
+
def clear_pid
|
|
140
|
+
@last_pid = @actual_pid if @actual_pid
|
|
141
|
+
@actual_pid = nil
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Delete the pid file, if there is one
|
|
145
|
+
#
|
|
146
|
+
# @return [void]
|
|
147
|
+
#
|
|
148
|
+
# @api private
|
|
149
|
+
def unlink_pid
|
|
150
|
+
System.delete_if_exists(pid_file)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|