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
data/sig/bluepill.rbs CHANGED
@@ -1,9 +1,6 @@
1
1
  # Type signatures for bluepill
2
2
 
3
3
  module Bluepill
4
- # The value of Bluepill::System.ps_axu: ps fields by pid
5
- type ps_table = Hash[Integer, Array[String]]
6
-
7
4
  # A time in seconds. An ActiveSupport::Duration also works, since it
8
5
  # passes for its value.
9
6
  type seconds = Integer | Float
@@ -36,184 +33,7 @@ module Bluepill
36
33
  def self.to_s: () -> String
37
34
  end
38
35
 
39
- # The daemon: it holds the groups of processes and serves commands from
40
- # bluepill's socket
41
- class Application
42
- PROCESS_COMMANDS: Array[Symbol]
43
-
44
- attr_accessor name: String
45
- attr_accessor logger: Logger
46
- attr_accessor base_dir: String
47
- attr_accessor socket: UNIXServer?
48
- attr_accessor pid_file: String
49
- attr_accessor kill_timeout: Integer
50
- attr_accessor groups: Hash[String?, Group]
51
- # Not used by bluepill
52
- attr_accessor work_queue: untyped
53
- attr_accessor pids_dir: String
54
- attr_accessor log_file: String?
55
-
56
- @foreground: bool?
57
- @mutex: Thread::Mutex
58
- @listener_thread: Thread?
59
- @running: bool
60
-
61
- def initialize: (String name, ?Hash[Symbol, untyped] options) -> void
62
-
63
- def foreground?: () -> bool
64
-
65
- def mutex: [T] () { () -> T } -> T
66
-
67
- def load: () -> void
68
-
69
- # The PROCESS_COMMANDS, which return the affected processes, or the
70
- # status lines for status
71
- def start: (?String? group_name, ?String? process_name) -> Array[String]
72
-
73
- def stop: (?String? group_name, ?String? process_name) -> Array[String]
74
-
75
- def restart: (?String? group_name, ?String? process_name) -> Array[String]
76
-
77
- def unmonitor: (?String? group_name, ?String? process_name) -> Array[String]
78
-
79
- def status: (?String? group_name, ?String? process_name) -> Array[String]
80
-
81
- def add_process: (Process process, ?_ToS? group_name) -> Array[Process]
82
-
83
- def version: () -> singleton(Version)
84
-
85
- # Ruby's protected methods are private in RBS
86
-
87
- private
88
-
89
- def send_to_process_or_group: (Symbol method, String? group_name, String? process_name) -> Array[String]
90
-
91
- def start_listener: () -> Thread
92
-
93
- def start_server: () -> void
94
-
95
- def run: () -> void
96
-
97
- def cleanup: () -> void
98
-
99
- def setup_signal_traps: () -> void
100
-
101
- def setup_logger_and_pids_dir: () -> void
102
-
103
- def setup_pids_dir: () -> void
104
-
105
- def kill_previous_bluepill: () -> void
106
-
107
- def write_pid_file: () -> void
108
- end
109
-
110
- # Runs the bluepill commands, and sends them to the daemon over its socket
111
- class Controller
112
- attr_accessor base_dir: String
113
- attr_accessor log_file: String?
114
- attr_accessor sockets_dir: String
115
- attr_accessor pids_dir: String
116
-
117
- def initialize: (Hash[Symbol, untyped] options) -> void
118
-
119
- def running_applications: () -> Array[String]
120
-
121
- def handle_command: (String application, String | Symbol command, *String args) -> void
122
-
123
- # Returns what the daemon responds with, which is unmarshaled from the socket
124
- def send_to_daemon: (String application, String | Symbol command, *String args) -> untyped
125
-
126
- def grep_pattern: (String application, ?String? query) -> String
127
-
128
- private
129
-
130
- def cleanup_bluepill_directory: () -> void
131
-
132
- def pid_for: (String app) -> (Integer | false)
133
-
134
- def setup_dir_structure: () -> void
135
-
136
- def verify_version!: (String application) -> void
137
- end
138
-
139
- module Socket
140
- TIMEOUT: Integer
141
- MAX_ATTEMPTS: Integer
142
-
143
- def self.client: [T] (String base_dir, String name) { (UNIXSocket socket) -> T } -> T
144
-
145
- # Returns the daemon's response, unmarshaled
146
- def self.client_command: (String base_dir, String name, String command) -> untyped
147
-
148
- def self.server: (String base_dir, String name) -> UNIXServer
149
-
150
- def self.socket_path: (String base_dir, String name) -> String
151
- end
152
-
153
- # Runs commands for a Process, which is the only class that includes it
154
- module ProcessCommands : _Host
155
- # What ProcessCommands uses from Process
156
- interface _Host
157
- %a{pure} def name: () -> String
158
- %a{pure} def logger: () -> Logger
159
- %a{pure} def children: () -> Array[Process]
160
- %a{pure} def pre_start_command: () -> String?
161
- %a{pure} def start_command: () -> String?
162
- %a{pure} def stop_command: () -> String?
163
- %a{pure} def restart_command: () -> String?
164
- %a{pure} def stop_signals: () -> Array[Symbol | Integer]?
165
- %a{pure} def start_grace_time: () -> seconds
166
- %a{pure} def stop_grace_time: () -> seconds
167
- %a{pure} def restart_grace_time: () -> seconds
168
- %a{pure} def on_start_timeout: () -> (String | Symbol)
169
- %a{pure} def monitor_children: () -> bool
170
- %a{pure} def uid: () -> String?
171
- %a{pure} def gid: () -> String?
172
- %a{pure} def working_dir: () -> String?
173
- %a{pure} def environment: () -> Hash[_ToS, _ToS]?
174
- %a{pure} def pid_file: () -> String?
175
- %a{pure} def stdin: () -> String?
176
- %a{pure} def stdout: () -> String?
177
- %a{pure} def stderr: () -> String?
178
- %a{pure} def supplementary_groups: () -> Array[String]?
179
- def daemonize?: () -> bool
180
- def monitor_children?: () -> bool
181
- def actual_pid: () -> Integer?
182
- def clean_threads: () -> void
183
- def signal_process: (Integer | String | Symbol code) -> bool
184
- def unlink_pid: () -> void
185
- def clear_pid: () -> void
186
- def skip_ticks_for: (seconds seconds) -> Integer
187
- def dispatch!: (Symbol | String event, ?String? reason) -> bool
188
- end
189
-
190
- @threads: Array[Thread]
191
-
192
- def start_process: () -> Integer
193
-
194
- def pre_start_process: () -> void
195
-
196
- def stop_process: () -> Integer
197
-
198
- def restart_process: () -> Integer
199
-
200
- def cleanup_process: () -> void
201
-
202
- def prepare_command: (String command) -> String
203
-
204
- def system_command_options: () -> System::command_options
205
-
206
- # Runs the block, and dispatches next_state if it takes longer than secs
207
- def with_timeout: [T] (seconds secs, String | Symbol next_state) { (Numeric secs) -> T } -> (T | bool)
208
-
209
- def run_command: (String cmd, String description) -> void
210
-
211
- def send_stop_signals: (Array[Symbol | Integer] signals) -> void
212
-
213
- def wait_for_stop: (seconds seconds) -> void
214
- end
215
-
216
- # What state_machine passes to transition callbacks and triggers
36
+ # What a process passes to its transition callbacks and triggers
217
37
  interface _Transition
218
38
  def from_name: () -> Symbol?
219
39
 
@@ -222,711 +42,59 @@ module Bluepill
222
42
  def loopback?: () -> bool
223
43
  end
224
44
 
225
- # A monitored process
226
- class Process
227
- include ProcessCommands
228
-
229
- CONFIGURABLE_ATTRIBUTES: Array[Symbol]
230
-
231
- attr_accessor name: String
232
- attr_accessor watches: Array[ConditionWatch]
233
- attr_accessor triggers: Array[Trigger]
234
- attr_accessor skip_ticks_until: Integer?
235
- attr_accessor process_running: bool?
236
-
237
- # CONFIGURABLE_ATTRIBUTES
238
- attr_accessor pre_start_command: String?
239
- attr_accessor start_command: String?
240
- attr_accessor stop_command: String?
241
- attr_accessor restart_command: String?
242
- attr_accessor stdout: String?
243
- attr_accessor stderr: String?
244
- attr_accessor stdin: String?
245
- attr_accessor daemonize: bool?
246
- attr_accessor pid_file: String?
247
- attr_accessor working_dir: String?
248
- attr_accessor environment: Hash[_ToS, _ToS]?
249
- attr_accessor start_grace_time: seconds
250
- attr_accessor stop_grace_time: seconds
251
- attr_accessor restart_grace_time: seconds
252
- attr_accessor uid: String?
253
- attr_accessor gid: String?
254
- attr_accessor cache_actual_pid: bool
255
- attr_accessor monitor_children: bool
256
- attr_accessor child_process_factory: ProcessFactory?
257
- attr_accessor pid_command: String?
258
- attr_accessor auto_start: bool?
259
- attr_accessor supplementary_groups: Array[String]?
260
- # Signal names alternating with delays in seconds
261
- attr_accessor stop_signals: Array[Symbol | Integer]?
262
- attr_accessor on_start_timeout: String | Symbol
263
- attr_accessor group_start_noblock: bool
264
- attr_accessor group_restart_noblock: bool
265
- attr_accessor group_stop_noblock: bool
266
- attr_accessor group_unmonitor_noblock: bool
267
-
268
- attr_reader children: Array[Process]
269
- attr_reader statistics: ProcessStatistics
270
- attr_reader logger: Logger
271
-
272
- @event_mutex: Monitor
273
- @threads: Array[Thread]
274
- @actual_pid: Integer?
275
- @transitioned: bool
276
-
277
- # Methods that state_machine defines, in a module it includes
278
- interface _StateMachine
279
- def state: () -> String
280
-
281
- def state=: (String state) -> String
282
-
283
- def state_name: () -> Symbol
284
-
285
- def human_state_name: () -> String
286
-
287
- def state?: (Symbol state) -> bool
288
-
289
- def unmonitored?: () -> bool
290
-
291
- def up?: () -> bool
292
-
293
- def down?: () -> bool
294
-
295
- def starting?: () -> bool
296
-
297
- def stopping?: () -> bool
298
-
299
- def restarting?: () -> bool
300
-
301
- def state_events: () -> Array[Symbol]
302
-
303
- def state_transitions: () -> Array[_Transition]
304
-
305
- def fire_state_event: (Symbol | String event) -> bool
306
-
307
- def can_tick?: () -> bool
308
-
309
- def tick_transition: () -> _Transition?
310
-
311
- def tick!: () -> true
312
-
313
- def start: () -> bool
314
-
315
- def can_start?: () -> bool
316
-
317
- def start_transition: () -> _Transition?
318
-
319
- def start!: () -> true
320
-
321
- def stop: () -> bool
322
-
323
- def can_stop?: () -> bool
324
-
325
- def stop_transition: () -> _Transition?
326
-
327
- def stop!: () -> true
328
-
329
- def unmonitor: () -> bool
330
-
331
- def can_unmonitor?: () -> bool
332
-
333
- def unmonitor_transition: () -> _Transition?
334
-
335
- def unmonitor!: () -> true
336
-
337
- def restart: () -> bool
338
-
339
- def can_restart?: () -> bool
340
-
341
- def restart_transition: () -> _Transition?
342
-
343
- def restart!: () -> true
344
- end
345
-
346
- include _StateMachine
347
-
348
- # Methods bluepill defines
349
-
350
- def initialize: (String process_name, Hash[Symbol, Hash[Symbol, untyped]] checks, ?Hash[Symbol, untyped] options) -> void
351
-
352
- # Runs the state machine's tick event, then the watches and children
353
- def tick: () -> void
354
-
355
- def logger=: (Logger logger) -> void
356
-
357
- def dispatch!: (Symbol | String event, ?String? reason) -> bool
358
-
359
- def record_transition: (_Transition transition) -> void
360
-
361
- def notify_triggers: (_Transition transition) -> void
362
-
363
- def add_watch: (Symbol name, Hash[Symbol, untyped] options) -> Array[ConditionWatch]
364
-
365
- def add_trigger: (Symbol name, Hash[Symbol, untyped] options) -> Array[Trigger]
366
-
367
- def run_watches: () -> void
368
-
369
- def determine_initial_state: () -> void
370
-
371
- def handle_user_command: (String cmd) -> void
372
-
373
- def process_running?: (?bool force) -> bool
374
-
375
- def clean_threads: () -> void
376
-
377
- def daemonize?: () -> bool
378
-
379
- def monitor_children?: () -> bool
380
-
381
- def signal_process: (Integer | String | Symbol code) -> bool
382
-
383
- def cache_actual_pid?: () -> bool
384
-
385
- def actual_pid: () -> Integer?
386
-
387
- def pid_from_file: () -> Integer?
388
-
389
- def pid_from_command: () -> Integer?
390
-
391
- def positive_pid: (Integer pid) -> Integer?
392
-
393
- def actual_pid=: (Integer pid) -> void
394
-
395
- def clear_pid: () -> nil
396
-
397
- def unlink_pid: () -> void
398
-
399
- def skip_ticks_for: (seconds seconds) -> Integer
400
-
401
- def skipping_ticks?: () -> bool?
402
-
403
- def refresh_children!: () -> void
404
- end
405
-
406
- # Keeps a short history of a process's events
407
- class ProcessStatistics
408
- type event = [Symbol | String, String?, Time]
409
-
410
- STRFTIME: String
411
- EVENTS_TO_PERSIST: Integer
412
-
413
- attr_reader events: Util::RotationalArray[event]
414
-
415
- def initialize: () -> void
416
-
417
- def record_event: (Symbol | String event, String? reason) -> Util::RotationalArray[event]
418
-
419
- def to_s: () -> String
45
+ # The durations and sizes a config writes, as plain numbers of seconds
46
+ # and bytes. Numeric includes it, so its own methods come first.
47
+ # What Units needs of the number it's in
48
+ interface _Multipliable
49
+ def *: (Integer) -> Numeric
420
50
  end
421
51
 
422
- # Processes that share a group name. Processes without one are in the
423
- # group named nil.
424
- class Group
425
- attr_accessor name: String?
426
- attr_accessor processes: Array[Process]
427
- attr_accessor logger: Logger
428
- # Not used by bluepill
429
- attr_accessor process_logger: untyped
430
-
431
- def initialize: (String? name, ?{ ?logger: Logger } options) -> void
432
-
433
- def add_process: (Process process) -> Array[Process]
52
+ module Units : _Multipliable
53
+ MINUTE: Integer
54
+ HOUR: Integer
55
+ DAY: Integer
56
+ KILOBYTE: Integer
57
+ MEGABYTE: Integer
58
+ GIGABYTE: Integer
434
59
 
435
- def tick: () -> void
60
+ def seconds: () -> self
61
+ alias second seconds
436
62
 
437
- def determine_initial_state: () -> void
63
+ def minutes: () -> Numeric
64
+ alias minute minutes
438
65
 
439
- # Proxied events, which return the affected processes as group:process
66
+ def hours: () -> Numeric
67
+ alias hour hours
440
68
 
441
- def start: (?String? process_name) -> Array[String]
69
+ def days: () -> Numeric
70
+ alias day days
442
71
 
443
- def unmonitor: (?String? process_name) -> Array[String]
72
+ def kilobytes: () -> Numeric
73
+ alias kilobyte kilobytes
444
74
 
445
- def stop: (?String? process_name) -> Array[String]
75
+ def megabytes: () -> Numeric
76
+ alias megabyte megabytes
446
77
 
447
- def restart: (?String? process_name) -> Array[String]
448
-
449
- def status: (?String? process_name) -> Array[String]
78
+ def gigabytes: () -> Numeric
79
+ alias gigabyte gigabytes
450
80
  end
451
81
 
452
- # Logs to a file or syslog, with nested prefixes
453
- class Logger
454
- # Where a Logger without a parent logger writes: a LoggerAdapter, or
455
- # Syslog
456
- interface _Backend
457
- def emerg: (String message) -> void
458
-
459
- def alert: (String message) -> void
460
-
461
- def crit: (String message) -> void
462
-
463
- def err: (String message) -> void
464
-
465
- def warning: (String message) -> void
466
-
467
- def notice: (String message) -> void
468
-
469
- def info: (String message) -> void
470
-
471
- def debug: (String message) -> void
472
- end
473
-
474
- type options = {
475
- ?logger: Logger,
476
- ?prefix: String?,
477
- ?stdout: bool,
478
- ?log_file: String?,
479
- ?identity: String
480
- }
481
-
482
- LOG_METHODS: Array[Symbol]
483
-
484
- # A ::Logger that also answers to the syslog method names
485
- class LoggerAdapter < ::Logger
486
- LOGGER_EQUIVALENTS: Hash[Symbol, Symbol]
487
-
488
- alias err error
489
-
490
- alias warning warn
491
-
492
- alias emerg fatal
493
-
494
- alias alert warn
495
-
496
- alias crit fatal
497
-
498
- alias notice info
499
- end
500
-
501
- @options: options
502
- @logger: Logger | (Object & _Backend)
503
- @prefix: String?
504
- @stdout: bool?
505
- @prefixes: Hash[String?, Logger]
506
-
507
- def initialize: (?options options) -> void
508
-
509
- def emerg: (_ToS msg, ?Array[String?] prefix) -> void
510
-
511
- def alert: (_ToS msg, ?Array[String?] prefix) -> void
512
-
513
- def crit: (_ToS msg, ?Array[String?] prefix) -> void
514
-
515
- def err: (_ToS msg, ?Array[String?] prefix) -> void
516
-
517
- def warning: (_ToS msg, ?Array[String?] prefix) -> void
518
-
519
- def notice: (_ToS msg, ?Array[String?] prefix) -> void
520
-
521
- def info: (_ToS msg, ?Array[String?] prefix) -> void
522
-
523
- def debug: (_ToS msg, ?Array[String?] prefix) -> void
524
-
525
- def prefix_with: (String? prefix) -> Logger
526
-
527
- def reopen: () -> void
528
-
529
- private
530
-
531
- def create_logger: () -> (Object & _Backend)
532
-
533
- def require_syslog: () -> void
534
- end
535
-
536
- # A value a ConditionWatch got, and whether it passed the check
537
- class HistoryValue < Struct[_ToS]
538
- attr_accessor value: _ToS
539
- attr_accessor critical: bool
540
-
541
- def self.new: (_ToS value, bool critical) -> HistoryValue
542
- end
543
-
544
- # Runs a process condition every so often, and fires its events when the
545
- # check fails often enough
546
- class ConditionWatch
547
- EMPTY_ARRAY: Array[Symbol]
548
-
549
- attr_accessor logger: Logger
550
- attr_accessor name: Symbol
551
-
552
- @fires: Array[Symbol]
553
- @every: Integer
554
- @times: Array[Integer]
555
- @include_children: bool
556
- @history: Util::RotationalArray[HistoryValue]
557
- @process_condition: ProcessConditions::ProcessCondition[untyped]
558
- @last_ran_at: Integer?
559
-
560
- def initialize: (Symbol name, ?Hash[Symbol, untyped] options) -> void
561
-
562
- # Returns the events to fire
563
- def run: (Integer? pid, ?Integer tick_number) -> Array[Symbol]
564
-
565
- def clear_history!: () -> Util::RotationalArray[HistoryValue]
566
-
567
- def fired?: () -> bool
568
-
569
- def to_s: () -> String
570
- end
571
-
572
- # Reacts to a process's state transitions. Subclasses register themselves
573
- # by their underscored name.
574
- class Trigger
575
- self.@implementations: Hash[Symbol, singleton(Trigger)]
576
-
577
- def self.inherited: (singleton(Trigger) klass) -> void
578
-
579
- def self.[]: %a{implicitly-returns-nil} (Symbol name) -> singleton(Trigger)
580
-
581
- attr_accessor process: Process
582
- attr_accessor logger: Logger
583
- attr_accessor mutex: Thread::Mutex
584
- attr_accessor scheduled_events: Array[[Symbol, Thread]]
585
-
586
- def initialize: (Process process, ?Hash[Symbol, untyped] options) -> void
587
-
588
- def reset!: () -> void
589
-
590
- def notify: (_Transition transition) -> void
591
-
592
- def dispatch!: (Symbol event) -> bool
593
-
594
- def schedule_event: (Symbol event, seconds delay) -> Thread
595
-
596
- def cancel_all_events: () -> void
597
- end
598
-
599
- module Triggers
600
- # Unmonitors a process that starts or restarts too often, and starts it
601
- # again later
602
- class Flapping < Trigger
603
- TRIGGER_STATES: Array[Symbol]
604
- PARAMS: Array[Symbol]
605
-
606
- attr_accessor times: Integer
607
- attr_accessor within: Integer
608
- attr_accessor retry_in: Integer
609
- attr_reader timeline: Util::RotationalArray[Integer]
610
-
611
- def initialize: (Process process, ?Hash[Symbol, untyped] options) -> void
612
-
613
- def notify: (_Transition transition) -> void
614
-
615
- def reset!: () -> void
616
-
617
- def check_flapping: () -> void
618
- end
619
- end
620
-
621
- # self in an application block
622
- class AppProxy
623
- APP_ATTRIBUTES: Array[Symbol]
624
-
625
- attr_accessor working_dir: String?
626
- attr_accessor uid: String?
627
- attr_accessor gid: String?
628
- attr_accessor environment: Hash[_ToS, _ToS]?
629
- attr_accessor auto_start: bool?
630
- attr_reader app: Application
631
-
632
- def initialize: (_ToS app_name, Hash[Symbol, untyped] options) -> void
633
-
634
- # The block runs with self set to the ProcessProxy, and also gets it as
635
- # an argument
636
- def process: (String process_name) { (ProcessProxy process) [self: ProcessProxy] -> void } -> Array[Process]
637
- end
638
-
639
- # Builds processes from a process block in the config
640
- class ProcessFactory
641
- @@process_keys: Hash[Array[String?], nil]
642
- @@pid_files: Hash[String, nil]
643
-
644
- @process_block: process_block
645
-
646
- attr_reader attributes: Hash[Symbol, untyped]
647
-
648
- def initialize: (Hash[Symbol, untyped] attributes, process_block process_block) -> void
82
+ module Util
83
+ # Turns a snake_case name into CamelCase, as ActiveSupport's camelize
84
+ # does
85
+ def self.camelize: (_ToS name) -> String
649
86
 
650
- def create_process: (String name, String pids_dir) -> Process
87
+ # Turns a CamelCase name into snake_case, as ActiveSupport's underscore
88
+ # does
89
+ def self.underscore: (String name) -> String
651
90
 
652
- def create_child_process: (String name, Integer pid, Logger logger) -> Process
91
+ # The Levenshtein distance between two strings
92
+ def self.levenshtein_distance: (String from, String to) -> Integer
653
93
 
654
94
  private
655
95
 
656
- def assign_default_pid_file: (String process_name, String pids_dir) -> String?
657
-
658
- def validate_process!: (ProcessProxy process) -> void
659
-
660
- def validate_unique_process!: (ProcessProxy process) -> void
661
-
662
- def validate_required_attributes!: (ProcessProxy process) -> void
663
-
664
- def validate_unique_pid_file!: () -> void
665
-
666
- def validate_stop_signals!: () -> void
667
-
668
- def warn_unless_pid_known: (ProcessProxy process, String? default_pid_file) -> void
669
-
670
- def config_error!: (String message) -> bot
671
-
672
- def validate_child_process!: (ProcessProxy child) -> void
673
- end
674
-
675
- # self in a process block. Any attribute can be set with `name = value`,
676
- # `name value` or `name!` (for true), and read back with `name`.
677
- class ProcessProxy
678
- attr_reader attributes: Hash[Symbol, untyped]
679
- attr_reader watches: Hash[Symbol, Hash[Symbol, untyped]]
680
- attr_reader name: String
681
-
682
- def initialize: (String process_name, Hash[Symbol, untyped] attributes, process_block process_block) -> void
683
-
684
- # Sets or reads an attribute
685
- def method_missing: (Symbol name, *untyped args) -> untyped
686
-
687
- def respond_to_missing?: (Symbol name, bool? include_private) -> bool
688
-
689
- def checks: (Symbol name, ?Hash[Symbol, untyped] options) -> Hash[Symbol, untyped]
690
-
691
- # The block configures the children like a process block
692
- def monitor_children: () ?{ (ProcessProxy child) [self: ProcessProxy] -> void } -> process_block?
693
-
694
- def to_process: () -> Process
695
- end
696
-
697
- # Memoizes ps output for a tick, and runs commands as daemons or until
698
- # they finish
699
- module System
700
- # Options for daemonize and execute_blocking
701
- type command_options = {
702
- ?uid: String?,
703
- ?gid: String?,
704
- ?working_dir: String?,
705
- ?environment: Hash[_ToS, _ToS]?,
706
- ?pid_file: String?,
707
- ?logger: Logger,
708
- ?stdin: String?,
709
- ?stdout: String?,
710
- ?stderr: String?,
711
- ?supplementary_groups: Array[String]?
712
- }
713
-
714
- type blocking_result = { exit_code: Integer?, stdout: String, stderr: String }
715
-
716
- type grandchild_pipes = { out: [IO, IO], err: [IO, IO], result: IO }
717
-
718
- APPEND_MODE: String
719
-
720
- IDX_MAP: { pid: Integer, ppid: Integer, pcpu: Integer, rss: Integer, etime: Integer, command: Integer }
721
-
722
- PS_MUTEX: Thread::Mutex
723
-
724
- # The store lives on the module. Steep checks the module functions as
725
- # instance methods, so it needs the instance variable too.
726
- self.@store: Hash[Symbol, ps_table | String]
727
- @store: Hash[Symbol, ps_table | String]
728
-
729
- # Raised when ps could not be run, so there is no data for this tick
730
- class PsError < StandardError
731
- end
732
-
733
- def self.pid_alive?: (Integer pid) -> bool
734
-
735
- def self.started_at: (Integer pid) -> Integer?
736
-
737
- def self.boot_time: () -> Integer?
738
-
739
- def self.cpu_usage: (Integer? pid, boolish include_children) -> Float?
740
-
741
- def self.memory_usage: (Integer? pid, boolish include_children) -> Float?
742
-
743
- def self.running_time: (Integer? pid) -> Integer?
744
-
745
- def self.command: (Integer? pid) -> String?
746
-
747
- def self.get_children: (Integer? parent_pid) -> Array[Integer]
748
-
749
- def self.delete_if_exists: (String? filename) -> void
96
+ def self.next_levenshtein_row!: (Array[Integer] row, Integer from_length, String from_char, String to) -> void
750
97
 
751
- def self.store: () -> Hash[Symbol, ps_table | String]
752
-
753
- def self.reset_data: () -> void
754
-
755
- def self.ps_axu: () -> ps_table
756
-
757
- def self.parse_elapsed_time: (String? str) -> Integer
758
-
759
- # Returns the daemon's pid
760
- def self.daemonize: (String cmd, ?command_options options) -> Integer?
761
-
762
- def self.execute_blocking: (String cmd, ?command_options options) -> blocking_result
763
-
764
- def self.run_blocking_child: (String cmd, command_options options, IO result_pipe) -> bot
765
-
766
- def self.exec_blocking_grandchild: (String cmd, command_options options, grandchild_pipes pipes) -> bot
767
-
768
- def self.setup_command_environment: (command_options options) -> void
769
-
770
- def self.drop_privileges: (String? uid, String? gid, Array[String]? supplementary_groups) -> void
771
-
772
- def self.can_write_pid_file: (String pid_file, Logger logger) -> bool
773
-
774
- def self.redirect_io: (String? io_in, String? io_out, String? io_err) -> void
775
- end
776
-
777
- # Remembers the pids and process groups of the processes bluepill starts,
778
- # so it can kill any left over from an earlier run
779
- module ProcessJournal
780
- self.@logger: Logger
781
- self.@journal_base_dir: String
782
- self.@legacy_journal_dir: String
783
-
784
- def self.logger: () -> Logger
785
-
786
- def self.journal_base_dir: () -> String
787
-
788
- def self.legacy_journal_dir: () -> String
789
-
790
- # Only the first logger is kept
791
- def self.logger=: (Logger new_logger) -> Logger
792
-
793
- def self.setup: (String base_dir, _ToS app_name) -> void
794
-
795
- def self.skip_pid?: (Integer? pid) -> bool
796
-
797
- def self.skip_pgid?: (Integer? pgid) -> bool
798
-
799
- def self.acquire_atomic_fs_lock: [T] (String name) { () -> T } -> T
800
-
801
- def self.clear_all_atomic_fs_locks: () -> void
802
-
803
- def self.pid_journal_filename: (String journal_name, ?String dir) -> String
804
-
805
- def self.pgid_journal_filename: (String journal_name, ?String dir) -> String
806
-
807
- # Each id, with when its process started (nil before 0.2)
808
- def self.journal_entries: (String filename) -> Array[[Integer, Integer?]]
809
-
810
- def self.journal_line: (Integer id) -> String
811
-
812
- def self.pid_journal: (String filename) -> Array[Integer]
813
-
814
- def self.pgid_journal: (String filename) -> Array[Integer]
815
-
816
- def self.clear_atomic_fs_lock: (String name) -> void
817
-
818
- def self.kill_all_from_journal: (String journal_name) -> void
819
-
820
- def self.kill_all_pgids_from_journal: (String journal_name) -> void
821
-
822
- def self.kill_all_pids_from_journal: (String journal_name) -> void
823
-
824
- def self.journal_dirs: () -> Array[String]
825
-
826
- def self.kill_all_from_journal_file: (String filename, Array[Integer] ids, String label, Integer sign) -> void
827
-
828
- def self.journaled_ids: (String filename, Array[Integer] ids, String label) -> Array[Integer]
829
-
830
- def self.signal_journaled: (String signal, Array[Integer] ids, String label, Integer sign) -> void
831
-
832
- def self.append_pgid_to_journal: (String journal_name, Integer? pgid) -> void
833
-
834
- def self.append_pid_to_journal: (String journal_name, Integer? pid) -> void
835
- end
836
-
837
- module ProcessConditions
838
- def self.[]: (Symbol | String name) -> singleton(ProcessCondition)
839
-
840
- # Checks something about a process. V is the type of value that run
841
- # returns and check tests.
842
- class ProcessCondition[V]
843
- @options: Hash[Symbol, untyped]
844
-
845
- def initialize: (?Hash[Symbol, untyped] options) -> void
846
-
847
- def run: (Integer? pid, boolish include_children) -> V
848
-
849
- # Whether the value is fine
850
- def check: (V value) -> bool
851
-
852
- def format_value: (V value) -> V
853
- end
854
-
855
- class AlwaysTrue < ProcessCondition[Integer]
856
- def run: (Integer? pid, boolish include_children) -> Integer
857
-
858
- def check: (Integer value) -> true
859
- end
860
-
861
- class CpuUsage < ProcessCondition[Float]
862
- @below: Numeric
863
-
864
- def run: (Integer? pid, boolish include_children) -> Float
865
-
866
- def check: (Float value) -> bool
867
- end
868
-
869
- # Seconds since the file changed, or the error that stopped bluepill
870
- # from finding out
871
- class FileTime < ProcessCondition[Float | StandardError | nil]
872
- @below: Numeric
873
- @filename: String
874
-
875
- def run: (Integer? pid, boolish include_children) -> (Float | StandardError | nil)
876
-
877
- def check: (Float | StandardError | nil value) -> bool
878
- end
879
-
880
- class Http < ProcessCondition[Net::HTTPResponse | StandardError]
881
- @uri: URI::HTTP
882
- @kind: Class
883
- @pattern: Regexp?
884
- @open_timeout: Integer
885
- @read_timeout: Integer
886
-
887
- def run: (Integer? pid, boolish include_children) -> (Net::HTTPResponse | StandardError)
888
-
889
- def check: (Net::HTTPResponse | StandardError value) -> bool
890
-
891
- private
892
-
893
- def hide_net_http_bug: [T] () { () -> T } -> T
894
- end
895
-
896
- # Memory usage in kilobytes
897
- class MemUsage < ProcessCondition[Float]
898
- MB: Integer
899
- FORMAT_STR: String
900
- MB_LABEL: String
901
- KB_LABEL: String
902
-
903
- @below: Numeric
904
-
905
- def run: (Integer? pid, boolish include_children) -> Float
906
-
907
- def check: (Float value) -> bool
908
-
909
- def format_value: (Float value) -> String
910
- end
911
-
912
- # Seconds since the process started
913
- class RunningTime < ProcessCondition[Integer]
914
- @below: Numeric
915
-
916
- def run: (Integer? pid, boolish include_children) -> Integer
917
-
918
- def check: (Integer value) -> bool
919
- end
920
-
921
- # The process's command line, which says <defunct> for a zombie
922
- class ZombieProcess < ProcessCondition[String?]
923
- def run: (Integer? pid, boolish include_children) -> String?
924
-
925
- def check: (String? value) -> bool
926
- end
927
- end
928
-
929
- module Util
930
98
  # An array that drops its oldest element when it grows past its capacity
931
99
  class RotationalArray[Elem] < Array[Elem]
932
100
  @capacity: Integer
@@ -939,3 +107,13 @@ module Bluepill
939
107
  end
940
108
  end
941
109
  end
110
+
111
+ # Numeric includes Units, so these do. Numeric itself has no * to type it
112
+ # with.
113
+ class Integer
114
+ include Bluepill::Units
115
+ end
116
+
117
+ class Float
118
+ include Bluepill::Units
119
+ end