bluepill 0.1.5 → 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.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +105 -8
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -231
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +134 -56
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -94
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -92
  20. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  21. data/lib/bluepill/group.rb +187 -78
  22. data/lib/bluepill/logger.rb +139 -48
  23. data/lib/bluepill/pid_file.rb +23 -0
  24. data/lib/bluepill/process/attributes.rb +379 -0
  25. data/lib/bluepill/process/children.rb +70 -0
  26. data/lib/bluepill/process/pid.rb +154 -0
  27. data/lib/bluepill/process/state_machine.rb +196 -0
  28. data/lib/bluepill/process/transition.rb +45 -0
  29. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  30. data/lib/bluepill/process/user_commands.rb +71 -0
  31. data/lib/bluepill/process/watches.rb +141 -0
  32. data/lib/bluepill/process.rb +56 -653
  33. data/lib/bluepill/process_commands/execution.rb +89 -0
  34. data/lib/bluepill/process_commands/hooks.rb +186 -0
  35. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  36. data/lib/bluepill/process_commands.rb +104 -172
  37. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  38. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  39. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  40. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  41. data/lib/bluepill/process_conditions/http.rb +52 -24
  42. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  43. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  44. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  45. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  46. data/lib/bluepill/process_conditions.rb +3 -2
  47. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  48. data/lib/bluepill/process_journal/lock.rb +106 -0
  49. data/lib/bluepill/process_journal.rb +126 -233
  50. data/lib/bluepill/process_statistics.rb +2 -2
  51. data/lib/bluepill/socket.rb +60 -20
  52. data/lib/bluepill/system/command_setup.rb +98 -0
  53. data/lib/bluepill/system/commands.rb +219 -248
  54. data/lib/bluepill/system/daemon.rb +161 -0
  55. data/lib/bluepill/system/ps.rb +85 -0
  56. data/lib/bluepill/system/ps_table.rb +163 -0
  57. data/lib/bluepill/system.rb +45 -126
  58. data/lib/bluepill/trigger.rb +52 -26
  59. data/lib/bluepill/triggers/flapping.rb +7 -15
  60. data/lib/bluepill/units.rb +126 -0
  61. data/lib/bluepill/util/inflections.rb +34 -0
  62. data/lib/bluepill/util/levenshtein.rb +53 -0
  63. data/lib/bluepill/util/rotational_array.rb +1 -1
  64. data/lib/bluepill/version.rb +13 -12
  65. data/lib/bluepill.rb +33 -36
  66. data/sig/bluepill/application.rbs +257 -0
  67. data/sig/bluepill/checks.rbs +203 -0
  68. data/sig/bluepill/dsl.rbs +126 -0
  69. data/sig/bluepill/logger.rbs +104 -0
  70. data/sig/bluepill/process.rbs +542 -0
  71. data/sig/bluepill/system.rbs +316 -0
  72. data/sig/bluepill.rbs +47 -869
  73. metadata +39 -45
@@ -1,11 +1,14 @@
1
- # fixes problem with loading on systems with rubyist-aasm installed
2
- gem 'state_machine'
3
-
4
- require 'state_machine'
5
- require 'daemons'
6
- require 'bluepill/system'
7
- require 'bluepill/process_journal'
8
- require 'bluepill/process_commands'
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
- # The options a config can set for a process
22
- CONFIGURABLE_ATTRIBUTES = [
23
- :pre_start_command,
24
- :start_command,
25
- :stop_command,
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,10 +36,10 @@ module Bluepill
71
36
 
72
37
  # Return the checks that run on the process every interval
73
38
  #
74
- # @return [Array<ConditionWatch>]
39
+ # @return [Watches]
75
40
  #
76
41
  # @api private
77
- attr_accessor :watches
42
+ attr_reader :watches
78
43
 
79
44
  # Return the triggers notified of the process's transitions
80
45
  #
@@ -102,238 +67,19 @@ module Bluepill
102
67
  # @api private
103
68
  attr_accessor :process_running
104
69
 
105
- # Return the command run before every start, if any
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 :pid_file
75
+ attr_accessor :app_name
181
76
 
182
- # Return the directory the process's commands run in
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 :working_dir
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
- self.logger = options[:logger]
440
-
441
- checks.each do |name, opts|
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
@@ -481,9 +150,7 @@ module Bluepill
481
150
  # Deal with thread cleanup here since the stopping state isn't used
482
151
  clean_threads if unmonitored?
483
152
 
484
- # run state machine transitions. state_machine defines tick in a
485
- # module it creates, which the signatures can't name.
486
- super # steep:ignore UnexpectedSuper
153
+ fire_event(:tick)
487
154
 
488
155
  return unless up?
489
156
 
@@ -504,7 +171,7 @@ module Bluepill
504
171
  # @api private
505
172
  def logger=(logger)
506
173
  @logger = logger
507
- watches.each { |w| w.logger = logger }
174
+ watches.logger = logger
508
175
  triggers.each { |t| t.logger = logger }
509
176
  end
510
177
 
@@ -519,68 +186,41 @@ module Bluepill
519
186
  def dispatch!(event, reason = nil)
520
187
  @event_mutex.synchronize do
521
188
  @statistics.record_event(event, reason)
522
- public_send(event)
189
+ # The reason, for notify_command
190
+ @transition_reason = reason
191
+ begin
192
+ public_send(event)
193
+ ensure
194
+ @transition_reason = nil
195
+ end
523
196
  end
524
197
  end
525
198
 
526
- # Clear the watches' history after the process changes state
199
+ # Set the logger, and add each check
527
200
  #
528
- # A process that's monitoring its children also clears their list, so it
529
- # finds them again. A loopback, such as up to up, changes nothing.
201
+ # A check is a trigger if one has its name, and a watch otherwise. Each
202
+ # gets the logger.
530
203
  #
531
- # @param [#from_name, #to_name, #loopback?] transition the transition
204
+ # @param [Hash{Symbol => Hash{Symbol => Object}}] checks options by
205
+ # check name
206
+ # @param [Logger] logger the logger to use
532
207
  #
533
208
  # @return [void]
534
209
  #
535
210
  # @api private
536
- def record_transition(transition)
537
- return if transition.loopback?
538
-
539
- @transitioned = true
540
-
541
- # When a process changes state, we should clear the memory of all the watches
542
- watches.each(&:clear_history!)
543
-
544
- # Also, when a process changes state, we should re-populate its child list
545
- if monitor_children?
546
- logger.warning 'Clearing child list'
547
- children.clear
548
- end
549
- logger.info "Going from #{transition.from_name} => #{transition.to_name}"
550
- end
551
-
552
- # Tell every trigger about a transition
553
- #
554
- # @param [#from_name, #to_name, #loopback?] transition the transition
555
- #
556
- # @return [void]
557
- #
558
- # @raise [StandardError] what a trigger raised, after logging it
559
- #
560
- # @api private
561
- def notify_triggers(transition)
562
- triggers.each do |trigger|
563
- begin
564
- trigger.notify(transition)
565
- rescue StandardError => e
566
- logger.err e.backtrace
567
- raise
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)
568
220
  end
569
221
  end
570
222
  end
571
223
 
572
- # Add a check to run on the process every interval
573
- #
574
- # @param [Symbol] name the check's name, such as :mem_usage
575
- # @param [Hash{Symbol => Object}] options the check's options
576
- #
577
- # @return [Array<ConditionWatch>] the process's watches
578
- #
579
- # @api private
580
- def add_watch(name, options)
581
- watches << ConditionWatch.new(name, options.merge(logger: logger))
582
- end
583
-
584
224
  # Add a trigger to notify of the process's transitions
585
225
  #
586
226
  # @param [Symbol] name the trigger's name, such as :flapping
@@ -593,103 +233,26 @@ module Bluepill
593
233
  triggers << Trigger[name].new(self, options.merge(logger: logger))
594
234
  end
595
235
 
596
- # Run every watch, and dispatch the events they fire
236
+ # Run every watch that's due, and dispatch the events they fire
597
237
  #
598
- # Each watch runs in a thread of its own. Once an event changes the
599
- # process's state, the events after it are dropped.
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.
600
240
  #
601
241
  # @return [void]
602
242
  #
603
243
  # @api private
604
244
  def run_watches
605
- now = Integer(Time.now)
606
-
607
- threads = watches.collect do |watch|
608
- [watch, Thread.new { watch.run(actual_pid, now) }] #: [ConditionWatch, Thread]
609
- end
245
+ events = watches.run(Integer(Time.now)) { actual_pid }
246
+ return if events.empty?
610
247
 
611
248
  @transitioned = false
612
-
613
- threads.each_with_object([]) do |(watch, thread), events|
614
- watch_events = thread.value #: Array[Symbol]
615
- next if watch_events.empty?
616
-
617
- logger.info "#{watch.name} dispatched: #{watch_events.join(',')}"
618
- watch_events.each do |event|
619
- events << [event, watch.to_s]
620
- end
621
- end.each do |event, reason| # rubocop:disable Style/MultilineBlockChain
249
+ events.each do |event, reason|
622
250
  break if @transitioned
623
251
 
624
252
  dispatch!(event, reason)
625
253
  end
626
254
  end
627
255
 
628
- # Set the state to up, down or unmonitored when bluepill loads
629
- #
630
- # A running process is up. Otherwise it's down, so that it starts, unless
631
- # auto_start is false.
632
- #
633
- # @return [void]
634
- #
635
- # @api private
636
- def determine_initial_state
637
- if process_running?(true)
638
- self.state = 'up'
639
- return
640
- end
641
- self.state = auto_start.equal?(false) ? 'unmonitored' : 'down' # we need to check for false value
642
- end
643
-
644
- # Carry out a command from the command line
645
- #
646
- # @param [String] cmd start, stop, restart or unmonitor
647
- #
648
- # @return [void]
649
- #
650
- # @api private
651
- def handle_user_command(cmd)
652
- case cmd
653
- when 'start'
654
- if process_running?(true)
655
- logger.warning('Refusing to re-run start command on an already running process.')
656
- else
657
- dispatch!(:start, 'user initiated')
658
- end
659
- when 'stop'
660
- stop_process
661
- dispatch!(:unmonitor, 'user initiated')
662
- when 'restart'
663
- # Through the state machine, like a check's restart, so the process
664
- # shows as restarting until it's up again. A state restart can't
665
- # leave, such as unmonitored, restarts it directly.
666
- restart_process unless dispatch!(:restart, 'user initiated')
667
- when 'unmonitor'
668
- # When the user issues an unmonitor cmd, reset any triggers so that
669
- # scheduled events gets cleared
670
- triggers.each(&:reset!)
671
- dispatch!(:unmonitor, 'user initiated')
672
- end
673
- end
674
-
675
- # Return whether the process is running, and forget its pid if not
676
- #
677
- # The answer is kept for the rest of the tick.
678
- #
679
- # @param [Boolean] force whether to check again this tick
680
- #
681
- # @return [Boolean]
682
- #
683
- # @api private
684
- def process_running?(force = false)
685
- @process_running = nil if force # clear existing state if forced
686
-
687
- @process_running ||= signal_process(0)
688
- # the process isn't running, so we should clear the PID
689
- clear_pid unless @process_running
690
- @process_running #: bool
691
- end
692
-
693
256
  # Kill the threads the process's commands left running
694
257
  #
695
258
  # @return [void]
@@ -709,136 +272,6 @@ module Bluepill
709
272
  !!daemonize
710
273
  end
711
274
 
712
- # Return whether bluepill watches the process's children
713
- #
714
- # @return [Boolean]
715
- #
716
- # @api private
717
- def monitor_children?
718
- !!monitor_children
719
- end
720
-
721
- # Send a signal to the process
722
- #
723
- # A process without a pid isn't running, so it gets no signal and nothing
724
- # is logged. A failure is logged.
725
- #
726
- # @param [Integer, String, Symbol] code the signal, such as 0 or :term
727
- #
728
- # @return [Boolean] whether the signal was sent
729
- #
730
- # @api private
731
- def signal_process(code)
732
- code = code.to_s.upcase if code.is_a?(String) || code.instance_of?(Symbol)
733
- pid = actual_pid
734
- # Without a pid the process isn't running. That's expected before the
735
- # first start, and actual_pid already warns about a missing pid file.
736
- return false unless pid
737
-
738
- # kill with a pid of 0 or less signals a whole process group, which
739
- # could include bluepill itself, so only signal a real pid
740
- raise "invalid pid #{pid}" unless pid > 0
741
-
742
- ::Process.kill(code, pid)
743
- true
744
- rescue StandardError => e
745
- logger.err "Failed to signal process #{pid} with code #{code}: #{e}"
746
- false
747
- end
748
-
749
- # Return whether the pid is kept once it's read
750
- #
751
- # @return [Boolean]
752
- #
753
- # @api private
754
- def cache_actual_pid?
755
- !!@cache_actual_pid
756
- end
757
-
758
- # Return the process's pid, from pid_command or the pid file
759
- #
760
- # @return [Integer, nil] the pid, or nil if there's none yet
761
- #
762
- # @api private
763
- def actual_pid
764
- pid_command ? pid_from_command : pid_from_file
765
- end
766
-
767
- # Return the pid in the pid file, cached unless cache_actual_pid is false
768
- #
769
- # A missing pid file is logged as a warning.
770
- #
771
- # @return [Integer, nil] the pid, or nil without a pid file or a pid
772
- #
773
- # @api private
774
- def pid_from_file
775
- return @actual_pid if cache_actual_pid? && @actual_pid
776
-
777
- @actual_pid = if pid_file
778
- if File.exist?(pid_file)
779
- positive_pid(File.read(pid_file).to_i)
780
- else
781
- logger.warning("pid_file #{pid_file} does not exist or cannot be read")
782
- nil
783
- end
784
- end
785
- end
786
-
787
- # Return the pid that pid_command prints
788
- #
789
- # Output that isn't only digits gives no pid.
790
- #
791
- # @return [Integer, nil] the pid, or nil if the output isn't one
792
- #
793
- # @api private
794
- def pid_from_command
795
- # Output that is not just digits reads as nil, so as 0
796
- positive_pid(`#{pid_command}`.strip[/\A\d+\z/].to_i)
797
- end
798
-
799
- # Return the pid, unless it's 0 or less
800
- #
801
- # An empty or garbled pid file reads as 0, which is never a real process.
802
- #
803
- # @param [Integer] pid the pid that was read
804
- #
805
- # @return [Integer, nil] the pid, or nil if it's not a real one
806
- #
807
- # @api private
808
- def positive_pid(pid)
809
- pid if pid > 0
810
- end
811
-
812
- # Set the pid, and add it to the process's journal
813
- #
814
- # @param [Integer] pid the process's pid
815
- #
816
- # @return [void]
817
- #
818
- # @api private
819
- def actual_pid=(pid)
820
- ProcessJournal.append_pid_to_journal(name, pid) # be sure to always log the pid
821
- @actual_pid = pid
822
- end
823
-
824
- # Forget the process's pid
825
- #
826
- # @return [nil]
827
- #
828
- # @api private
829
- def clear_pid
830
- @actual_pid = nil
831
- end
832
-
833
- # Delete the pid file, if there is one
834
- #
835
- # @return [void]
836
- #
837
- # @api private
838
- def unlink_pid
839
- System.delete_if_exists(pid_file)
840
- end
841
-
842
275
  # Skip ticks for a while, such as for a command's grace time
843
276
  #
844
277
  # The times add up, so a stop followed by a start, which is how
@@ -862,35 +295,5 @@ module Bluepill
862
295
  def skipping_ticks?
863
296
  skip_ticks_until && skip_ticks_until > Integer(Time.now)
864
297
  end
865
-
866
- # Update the list of children from the tick's ps table
867
- #
868
- # Children that are no longer running are dropped, and a Process is made
869
- # for each new one, with the checks monitor_children gave.
870
- #
871
- # @return [void]
872
- #
873
- # @api private
874
- def refresh_children!
875
- # First prune the list of dead children
876
- @children.delete_if { |child| !child.process_running?(true) }
877
-
878
- # Add new found children to the list
879
- new_children_pids = System.get_children(actual_pid) - @children.collect(&:actual_pid)
880
-
881
- logger.info "Existing children: #{@children.collect(&:actual_pid).join(',')}. Got new children: #{new_children_pids} for #{actual_pid}" unless new_children_pids.empty?
882
-
883
- # Construct a new process wrapper for each new found children
884
- # monitor_children sets the factory
885
- factory = child_process_factory #: ProcessFactory
886
- new_children_pids.each do |child_pid|
887
- ProcessJournal.append_pid_to_journal(name, child_pid) if daemonize?
888
- child_name = "<child(pid:#{child_pid})>"
889
- child_logger = logger.prefix_with(child_name)
890
-
891
- child = factory.create_child_process(child_name, child_pid, child_logger)
892
- @children << child
893
- end
894
- end
895
298
  end
896
299
  end