bluepill 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -2
  3. data/README.md +12 -7
  4. data/bin/bluepill +8 -2
  5. data/bluepill.gemspec +1 -1
  6. data/lib/bluepill/application/lifecycle.rb +44 -34
  7. data/lib/bluepill/application/listener.rb +1 -1
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +33 -29
  10. data/lib/bluepill/base_directory.rb +18 -20
  11. data/lib/bluepill/check_registry.rb +104 -0
  12. data/lib/bluepill/clock.rb +1 -3
  13. data/lib/bluepill/condition_watch.rb +45 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +32 -37
  16. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  17. data/lib/bluepill/dsl/attributes.rb +135 -0
  18. data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
  20. data/lib/bluepill/dsl/process_factory.rb +83 -84
  21. data/lib/bluepill/dsl/process_proxy.rb +61 -168
  22. data/lib/bluepill/dsl.rb +16 -15
  23. data/lib/bluepill/errors.rb +190 -0
  24. data/lib/bluepill/group.rb +14 -37
  25. data/lib/bluepill/logger.rb +55 -97
  26. data/lib/bluepill/process/attributes.rb +66 -32
  27. data/lib/bluepill/process/children.rb +4 -6
  28. data/lib/bluepill/process/pid.rb +41 -35
  29. data/lib/bluepill/process/state_machine.rb +15 -59
  30. data/lib/bluepill/process/stop_signals.rb +120 -0
  31. data/lib/bluepill/process/transition.rb +1 -3
  32. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  33. data/lib/bluepill/process/triggers.rb +90 -0
  34. data/lib/bluepill/process/user_commands.rb +1 -1
  35. data/lib/bluepill/process/watches.rb +9 -23
  36. data/lib/bluepill/process.rb +55 -52
  37. data/lib/bluepill/process_commands/execution.rb +14 -22
  38. data/lib/bluepill/process_commands/hooks.rb +1 -31
  39. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  40. data/lib/bluepill/process_commands.rb +10 -8
  41. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  42. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  43. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  44. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  45. data/lib/bluepill/process_conditions/http.rb +28 -67
  46. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  47. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  48. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  49. data/lib/bluepill/process_conditions.rb +5 -14
  50. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  51. data/lib/bluepill/process_journal/config.rb +73 -0
  52. data/lib/bluepill/process_journal/entries.rb +128 -0
  53. data/lib/bluepill/process_journal/journal.rb +69 -0
  54. data/lib/bluepill/process_journal/kind.rb +60 -0
  55. data/lib/bluepill/process_journal/lock.rb +9 -4
  56. data/lib/bluepill/process_journal.rb +13 -324
  57. data/lib/bluepill/process_statistics.rb +3 -7
  58. data/lib/bluepill/socket.rb +19 -17
  59. data/lib/bluepill/system/command_options.rb +135 -0
  60. data/lib/bluepill/system/command_result.rb +73 -0
  61. data/lib/bluepill/system/command_setup.rb +3 -4
  62. data/lib/bluepill/system/commands.rb +21 -18
  63. data/lib/bluepill/system/daemon.rb +19 -14
  64. data/lib/bluepill/system/files.rb +35 -0
  65. data/lib/bluepill/system/ps.rb +20 -17
  66. data/lib/bluepill/system/ps_table.rb +7 -9
  67. data/lib/bluepill/system/usage.rb +155 -0
  68. data/lib/bluepill/system.rb +7 -192
  69. data/lib/bluepill/trigger.rb +18 -69
  70. data/lib/bluepill/triggers/flapping.rb +12 -8
  71. data/lib/bluepill/units.rb +7 -21
  72. data/lib/bluepill/version.rb +12 -22
  73. data/lib/bluepill.rb +1 -1
  74. data/sig/bluepill/application.rbs +11 -12
  75. data/sig/bluepill/attributes.rbs +48 -0
  76. data/sig/bluepill/checks.rbs +40 -40
  77. data/sig/bluepill/dsl.rbs +97 -60
  78. data/sig/bluepill/errors.rbs +48 -0
  79. data/sig/bluepill/logger.rbs +15 -31
  80. data/sig/bluepill/process.rbs +39 -305
  81. data/sig/bluepill/process_commands.rbs +235 -0
  82. data/sig/bluepill/stop_signals.rbs +21 -0
  83. data/sig/bluepill/system.rbs +154 -99
  84. data/sig/bluepill/version.rbs +17 -0
  85. data/sig/bluepill.rbs +1 -17
  86. data/sig/manifest.yaml +1 -0
  87. metadata +21 -8
  88. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  89. data/lib/bluepill/deprecation/dsl.rb +0 -141
  90. data/lib/bluepill/deprecation/options.rb +0 -140
  91. data/lib/bluepill/deprecation.rb +0 -92
  92. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  93. data/sig/bluepill/deprecation.rbs +0 -87
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f99d17a71de30a8740c305e79371a5a41b8563291883ab151fd8dd09b1a043f6
4
- data.tar.gz: 568d20ca8eb2e480a3d54047e2d71ec6982b7f46653cffe8ca668252cd57122e
3
+ metadata.gz: fa9f8a199b7c2f30858345bc94b934e3a15be6776595ea4481c61722340812b8
4
+ data.tar.gz: b33c36a84b7bbe956866889eca7838211f4d40942e75b3e6fefd33b6c81b6958
5
5
  SHA512:
6
- metadata.gz: 0b8fcfe797993aaa1f5663e96b0a68028e1979181c2d2d5d36dc81ebaa44f8549c7c84d83f9598392d1f36740c8ffa1e89b710927045427ed3a945951e03135c
7
- data.tar.gz: 40be778dcf5a90336923599c6bc9d9cb8485ee986d8da46ea0169c4264023383e3416a3296d133106391a9bdcc709e4c42cd498208c130ca9e486ab5335cddf6
6
+ metadata.gz: 42b736d7e86289628a9a0b057f01d9e0b379a42048b6903da13090429a4831255adcefd07db3cccc3ab19b816ad9281ad58d692779e171a3ea767cb24c0f96f6
7
+ data.tar.gz: e8dcc83be7c7a2df1f6f030f13dc11ef75bd98b087dce958cbce07f13812193693bdd7dfafd3ee2e8d9753b913b41341bb8b1db5d588882aa748f789d7ff9445
data/CHANGELOG.md CHANGED
@@ -7,6 +7,74 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2026-11-02
11
+
12
+ ### Upgrading from 0.2
13
+
14
+ Most pill files work with 0.3.0 unchanged. Upgrade to 0.2.9 first, and run `bluepill load`: it warns, with a `DEPRECATION:` line, about each of these that it can find in your pill file and your custom checks and triggers, and a pill file that loads without any works with 0.3.0. Check these before you upgrade:
15
+
16
+ - **Bluepill needs Ruby 3.0 or newer, or JRuby 9.4 or newer.** On Ruby 2.7, stay on 0.2.9.
17
+ - **A custom check or trigger takes its options as keyword arguments.** One written as `def initialize(options = {})` that calls `super` now fails with an `ArgumentError`, since `super` passes the hash on as one positional argument. Name the options it needs and pass the rest on, as the README's example does: `def initialize(path:, below:, **options)`, then `super(**options)`. One written for bluepill 0.1, which defines `check`, also needs it renamed `check?`.
18
+ - **An application option that bluepill doesn't know fails the config.** `Bluepill.application("app", log_flie: "/var/log/app.log")` now stops with Ruby's unknown keyword error. Before, bluepill ignored the option.
19
+ - **A check without an option it needs fails the config.** `checks :mem_usage`, `:cpu_usage` or `:running_time` without `below:`, and `:file_time` without `filename:` or `below:`, now stop the pill file from loading, with Ruby's missing keyword error. Before, `mem_usage` without `below:` always passed, and the others raised an error each time they ran. `:http` without `url:` fails with the same error, where it failed with a `URI::InvalidURIError`.
20
+ - **A check or trigger option that bluepill doesn't know fails the config.** A misspelled option, such as `evry: 10`, or one meant for another check, such as `within:` on an `http` check, now stops the pill file from loading, with Ruby's unknown keyword error. Before, bluepill ignored it.
21
+ - **Pass options kept in a hash with `**`.** A pill file that keeps a check's or the application's options in a variable passes them as `process.checks :mem_usage, **options`, since a hash passed without `**` no longer counts as options.
22
+ - **An https `http` check verifies the server's certificate.** One against a self-signed or otherwise invalid certificate now fails the check. Add `verify_ssl: false` to keep checking it without verification.
23
+ - **Set attributes with `=`.** `process.daemonize!` and `process.start_command "cmd"` no longer work. Write `process.daemonize = true` and `process.start_command = "cmd"`.
24
+ - **`on_start_timeout`, `fires:` and a custom trigger's `dispatch!` take Symbols**, such as `process.on_start_timeout = :unmonitor` or `fires: :stop`, from `:start`, `:stop`, `:unmonitor` and `:restart`.
25
+ - **Two checks of the same kind both run.** A process with two `checks :http` of different URLs, of which 0.2 ran only the last, now runs both, so the first one can restart the process too.
26
+ - **A `monitor_children` block needs a `stop_command`, and runs once at load.** Bluepill runs it when the pill file loads, with `actual_pid` nil, to check it, and fails the config if it sets no `stop_command`. `monitor_children` needs a block, and a process block can't set `monitor_children` or `child_process_factory` itself.
27
+ - **A custom check that subclasses `MemUsage` gets bytes.** Its `run`, `check?` and `format_value` see the memory usage in bytes, where they saw kilobytes.
28
+ - **Call `super` first in a custom check's `initialize`.** A check gets its logger only through the `logger:` keyword, which `super` stores, so one that doesn't call `super` has no logger. Don't set a check's or trigger's `logger` yourself: the writer is gone.
29
+ - **Upgrade the daemon along with the CLI.** A 0.3.0 `bluepill` command says a 0.2 daemon is out of date, so run `bluepill load` again after upgrading. Bluepill monitors the running processes again without restarting them.
30
+
31
+ ### Added
32
+
33
+ - `Bluepill::Error`, the base of the errors that stop the `bluepill` command, with the status it exits with: `ConfigError`, for a mistake in a pill file, such as a misspelled process attribute or two processes with the same pid file, `StartupError`, `PreviousDaemonError`, `AlreadyRunningError`, `ConnectionError`, `VersionMismatchError`, `UnknownCommandError` and `DaemonError`.
34
+ - The `http` check's `verify_ssl:` option. Set it to false for a server with a self-signed certificate.
35
+ - A custom check or trigger can be defined in any module, since bluepill finds it by its class name.
36
+ - The daemon logs what stops it at `crit`, with the backtrace, since its stderr is `/dev/null`.
37
+
38
+ ### Changed
39
+
40
+ - **Breaking:** a misspelled process attribute raises `Bluepill::ConfigError` instead of `NoMethodError`. Like the other mistakes that a config check finds, such as a missing `start_command`, `bluepill load` prints its message and exits with status 6, where it showed a backtrace and exited with status 1.
41
+ - **Breaking:** the options of `Bluepill.application` and `checks` are keyword arguments, all the way to the application, the checks and the triggers, so a misspelled, unknown or missing option fails when the pill file loads. A custom check or trigger takes its options as keyword arguments too (see "Upgrading from 0.2").
42
+ - An exception in a check or trigger is logged, with its backtrace, and no longer stops the daemon. The check is skipped until its next interval, and doesn't count toward `times`.
43
+ - A config error's message starts with the pill file's line, as in `app.pill.rb:5: Unknown attribute ...`.
44
+ - **Breaking:** a misspelled application attribute, such as `app.uidd = "deploy"`, raises `Bluepill::ConfigError` with a suggestion, as a misspelled process attribute does.
45
+ - **Breaking:** an unknown check name raises `Bluepill::ConfigError` with a did-you-mean suggestion, where it raised `NameError`.
46
+ - **Breaking:** `fires:` takes only `:start`, `:stop`, `:unmonitor` or `:restart`, and anything else fails the pill file with a `Bluepill::ConfigError`. Before, bluepill called any public method of the process with that name.
47
+ - **Breaking:** `on_start_timeout` defaults to `:start`, and a value that isn't one of `:start`, `:stop`, `:unmonitor` or `:restart` fails the config.
48
+ - **Breaking:** a custom check that doesn't define `run` and `check?`, or a trigger that doesn't define `notify`, fails the pill file when it loads, where it raised each time it ran.
49
+ - **Breaking:** the `http` check verifies TLS certificates.
50
+ - **Breaking:** reading an attribute that isn't set returns nil instead of raising `NameError`.
51
+ - **Breaking:** the library raises a `Bluepill::Error` instead of calling `exit` or `abort`. `bin/bluepill` and `Bluepill.application` print its message and exit with the same statuses as before.
52
+ - **Breaking:** `Bluepill::Logger`'s log methods take only the message, and `prefix_with` is the only way to make a prefixed logger (the `prefix:` option is gone).
53
+ - A mistake in a `monitor_children` block, such as a missing `stop_command`, fails the config when it loads. Before, the daemon exited the first time it found a child. `monitor_children` without a block fails the config.
54
+ - The daemon traps signals and creates its pids directory when it starts, not while the pill file loads.
55
+ - **Breaking:** the commands a process runs take a `System::CommandOptions`, and `System.execute_blocking` returns a `System::CommandResult`, instead of Hashes. `System.daemonize` takes the pid file and the logger as keywords.
56
+ - **Breaking:** each process reaches its pid and process group journals through `ProcessJournal.for(name)`, and a journal lock held too long raises `ProcessJournal::LockTimeoutError`.
57
+ - **Breaking:** the ps table is kept in `System`'s own state, and `System.store` is gone.
58
+ - Checking a process's CPU takes 8% fewer instructions, and the `mem_usage` check 1.4% fewer.
59
+ - A process gets its logger, prefixed with the names of its app, its group and itself, when it's made, and passes it to its watches, checks and triggers as they're made, instead of being handed a new logger when it joins its group.
60
+ - A process's `stop_signals` is parsed and checked once, when the pill file loads, into a `Bluepill::Process::StopSignals`.
61
+ - **Breaking:** `Process#stop_signals` returns a `Bluepill::Process::StopSignals` instead of an Array (`to_a` gives the Array), and `Process#stop_signals=` raises `Bluepill::ConfigError` for a malformed Array, a monitored child's too.
62
+
63
+ ### Removed
64
+
65
+ - **Breaking:** a custom check that defines `check`, as in bluepill 0.1, no longer works. Name the method `check?`, as bluepill 0.2 does.
66
+ - **Breaking:** support for Ruby 2.7. Bluepill 0.3.0 needs Ruby 3.0 or newer, or JRuby 9.4 or newer. Bluepill 0.2.9 still runs on Ruby 2.7.
67
+ - **Breaking:** the undocumented `name!` and `name value` forms of setting a process attribute.
68
+ - **Breaking:** setting `monitor_children` or `child_process_factory` in a process block. Use `monitor_children` with a block.
69
+ - **Breaking:** the `logger=` writers of `Process`, `Process::Watches`, `Process::Triggers`, `ConditionWatch`, `ProcessConditions::ProcessCondition` and `Trigger`. A custom check or trigger gets its logger only through the `logger:` keyword.
70
+ - **Breaking:** `Process#start`, `#stop`, `#restart` and `#unmonitor` (use `dispatch!`), `Trigger.[]` and `ProcessConditions.[]` (use `CheckRegistry.fetch`), `Group#process_logger`, and `ConditionWatch::HistoryValue#critical` (now `passed`).
71
+
72
+ ### Fixed
73
+
74
+ - Two checks of the same kind, such as two `:http` checks of different URLs, both run. Before, only the last one did.
75
+ - The CLI's check that the daemon runs the same version of bluepill never failed: the daemon now answers with its version as a string.
76
+ - `bluepill log` quotes the log file's path and the pattern, so a path with spaces or shell characters works.
77
+
10
78
  ## [0.2.9] - 2026-11-02
11
79
 
12
80
  ### Added
@@ -138,11 +206,39 @@ To upgrade, install 0.2.0 and run `bluepill load` with the same pill file. It re
138
206
 
139
207
  - The README's group example gave every process the same pid file, and its `stop_signals` example waited longer than its stop grace time, so bluepill refused to load either config.
140
208
 
209
+ ## [0.1.7] - 2026-11-02
210
+
211
+ ### Added
212
+
213
+ - Warnings about what bluepill 0.2.0 and later will change, each in a line that starts with `DEPRECATION:`, once each, on stderr, where `bluepill load` shows it, and in bluepill's log. Each says what to do now. See Deprecated for what they warn about.
214
+
215
+ ### Deprecated
216
+
217
+ - An `http` check of an https URL that doesn't set `verify_ssl:`, since bluepill 0.3.0 verifies its certificate by default. Set `verify_ssl: true` to verify it now, or `verify_ssl: false` to keep checking a server with a self-signed certificate. Bluepill 0.2 ignores the option, so a pill file that sets it works with each version.
218
+ - Ruby 1.9.3 and JRuby 1.7. Bluepill 0.2.0 needs Ruby 2.0 or JRuby 9.0, and later 0.2 releases need Ruby 2.7 or JRuby 9.4. On those Rubies, stay on 0.1 until you upgrade Ruby.
219
+ - Setting a process attribute that bluepill doesn't know, such as a misspelled `start_comand`, which bluepill 0.1 ignores. Bluepill 0.2.0 fails the config on it. The warning names the attribute: remove it, or fix its name. `hook_timeout` and the hooks that 0.2.0 adds don't warn, though 0.1 ignores them.
220
+ - `pre_start_command`, which bluepill 0.2.0 runs like its other hooks: it kills one that runs longer than `hook_timeout`, 30 seconds by default, and starts the process even when the command can't run, such as a missing program. Bluepill 0.1 waits for it however long it takes. Set `hook_timeout`, which 0.1 ignores, for one that can take longer, such as a migration.
221
+ - A process with a `uid`, when bluepill runs as root. Bluepill 0.2.0 runs it with every group the user is in, as after logging in, and in the user's primary group when it sets no `gid`. Bluepill 0.1 gives it only its `gid` and `supplementary_groups`, and keeps it in root's groups when it sets no `gid`. Set `gid` and `supplementary_groups` to the groups it needs.
222
+ - `monitor_children` on a process with a `uid`, `gid` or `supplementary_groups`, when bluepill runs as root. Bluepill 0.2.0 runs the children's commands, such as their `stop_command`, as the parent's user and groups, where 0.1 runs them as root. Make sure they work as that user, or run one that needs root with `sudo`.
223
+ - A custom trigger that reads anything of the transition its `notify` gets but `from_name`, `to_name` and `loopback?`, such as `from`, `to` or `event`. Bluepill 0.2.0's transitions have only those three. The warning names the trigger and what it read. Bluepill's own `flapping` trigger only reads those three, so it gets its transitions as before.
224
+ - Bluepill 0.2.0 also changes some things that 0.1.7 can't warn about: it no longer loads ActiveSupport, so a pill file, check or trigger that uses `5.minutes.ago`, `blank?` or a unit such as `1.week` needs it required, a process's `state` is a Symbol, such as `:up`, the `state_machine` methods of a process, such as `state_name` and `can_start?`, are gone, and a custom check also gets a `logger:` option. Read 0.2.0's "Upgrading from 0.1" before upgrading.
225
+
226
+ ### Fixed
227
+
228
+ - The README's group example gave every process the same pid file, and its `stop_signals` example waited longer than its stop grace time, so bluepill refused to load either config.
229
+ - Bluepill timed its ticks, checks, grace times and flapping detection with the system clock, so a change to the clock, such as an NTP correction after a VM resumes, could put off ticks and checks for as long as the clock went back, or end grace times early when it went forward. It now times them with the monotonic clock, which only counts up, where Ruby has one (Ruby 2.1 and newer, and JRuby 9.0 and newer). On Ruby 1.9.3 and 2.0, and on JRuby 1.7, it still reads the system clock.
230
+ - An exception in a check, other than a failed `ps`, or in a trigger stopped the daemon, which exited with status 5 and left the processes unmonitored. Bluepill now logs it at `err`, with a line for each line of its backtrace, and keeps monitoring. The check adds nothing to its history and fires nothing, so it doesn't count toward `times`, and it runs again at its next interval. A tick that raises for any other reason is logged the same way, and the process ticks again next second.
231
+ - The CLI's check that the daemon runs the same version of bluepill never failed, since the daemon answered with its `Version` module, which Marshal loads as the CLI's own. The daemon now answers with its version as a String, and the CLI stops when it differs. A daemon from before 0.1.7 can't say which version it runs, so the CLI warns about it and goes on, as before. Run `bluepill load` again after upgrading to replace it.
232
+
233
+ ### Security
234
+
235
+ - `bluepill log` built a shell command with the log file's path and the pattern unquoted, so a path with a space broke the command, and a quote or `$(...)` in the path or the process name ran as shell code. It now passes them to `tail` and `grep` as the shell's arguments, so they are never parsed.
236
+ - The `http` check's `verify_ssl:` option. With `verify_ssl: true`, a check of an https URL verifies the server's certificate, so it fails against a server that isn't the one it names. It still doesn't verify it by default, as before, since that would fail the checks of servers with self-signed certificates. Bluepill 0.3.0 verifies it by default.
237
+
141
238
  ## [0.1.6] - 2026-11-02
142
239
 
143
240
  ### Security
144
241
 
145
- - The `http` check can verify an https server's certificate, with `verify_ssl: true`. Without it, the check passes against any server that answers, whatever its certificate.
146
242
  - The daemon now runs only the commands that the CLI sends over its socket: `start`, `stop`, `restart`, `unmonitor`, `status`, `version` and `log_file`. Before, it called whatever method a line on the socket named, so anyone who could write to the socket could call any of the daemon's public methods, such as `instance_eval` with code of their own. The socket takes the daemon's umask, so with the usual umask of 022 only the daemon's own user could connect. Any other command now gets a `NoMethodError` back.
147
243
 
148
244
  ## [0.1.5] - 2026-11-01
@@ -896,13 +992,15 @@ Initial release.
896
992
  - Added the `bluepill` CLI with the `load`, `status`, `start`, `stop`, `restart`, `unmonitor`, `quit` and `log` commands.
897
993
  - Added syslog logging.
898
994
 
899
- [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.2.9...HEAD
995
+ [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.3.0...HEAD
996
+ [0.3.0]: https://github.com/bluepill-rb/bluepill/compare/v0.2.9...v0.3.0
900
997
  [0.2.9]: https://github.com/bluepill-rb/bluepill/compare/v0.2.7...v0.2.9
901
998
  [0.2.7]: https://github.com/bluepill-rb/bluepill/compare/v0.2.5...v0.2.7
902
999
  [0.2.5]: https://github.com/bluepill-rb/bluepill/compare/v0.2.3...v0.2.5
903
1000
  [0.2.3]: https://github.com/bluepill-rb/bluepill/compare/v0.2.1...v0.2.3
904
1001
  [0.2.1]: https://github.com/bluepill-rb/bluepill/compare/v0.2.0...v0.2.1
905
1002
  [0.2.0]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...v0.2.0
1003
+ [0.1.7]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...v0.1.7
906
1004
  [0.1.6]: https://github.com/bluepill-rb/bluepill/compare/v0.1.5...v0.1.6
907
1005
  [0.1.5]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...v0.1.5
908
1006
  [0.1.4]: https://github.com/bluepill-rb/bluepill/compare/v0.1.3...v0.1.4
data/README.md CHANGED
@@ -60,10 +60,10 @@ end
60
60
 
61
61
  If you don't specify a stop command, a TERM signal will be sent by default. Similarly, the default restart action is to issue stop and then start.
62
62
 
63
- Bluepill only accepts the process attributes it knows, which are the ones this README describes. A misspelled or unsupported attribute fails when the config loads, and the error names the attribute, the process, and the closest attribute bluepill knows:
63
+ Bluepill only accepts the attributes it knows, which are the ones this README describes. A misspelled or unsupported attribute fails when the config loads, and the error names the attribute, the process, and the closest attribute bluepill knows. Bluepill prints the line of the pill file with the mistake before the error:
64
64
 
65
65
  ```
66
- Unknown attribute 'start_comand' for process 'process_name'. Did you mean 'start_command'?
66
+ app.pill.rb:5: Unknown attribute 'start_comand' for process 'process_name'. Did you mean 'start_command'?
67
67
  ```
68
68
 
69
69
  Now if we want to do something more meaningful, like actually monitor the process, we do:
@@ -78,7 +78,7 @@ Bluepill.application("app_name") do |app|
78
78
  end
79
79
  ```
80
80
 
81
- We added a line that checks every 10 seconds to make sure the cpu usage of this process is below 5 percent; 3 failed checks results in a restart. We can specify a two-element array for the _times_ option to say that it 3 out of 5 failed attempts results in a restart.
81
+ We added a line that checks every 10 seconds to make sure the cpu usage of this process is below 5 percent. Three failed checks in a row restart the process. We can specify a two-element array for the _times_ option to say that 3 out of 5 failed attempts result in a restart. A check restarts the process by default, and `fires:` picks other events instead, from `:start`, `:stop`, `:unmonitor` and `:restart`, such as `fires: :stop` or `fires: [:stop, :start]`.
82
82
 
83
83
  To watch memory usage, we just add one more line:
84
84
 
@@ -93,6 +93,8 @@ Bluepill.application("app_name") do |app|
93
93
  end
94
94
  ```
95
95
 
96
+ A process can have more than one check of a kind, such as two `:http` checks of different URLs.
97
+
96
98
  To watch the modification time of a file, e.g. a log file to ensure the process is actually working add one more line:
97
99
 
98
100
  ```ruby
@@ -213,6 +215,8 @@ To check for flapping:
213
215
  process.checks :flapping, times: 2, within: 30.seconds, retry_in: 7.seconds
214
216
  ```
215
217
 
218
+ To check that a URL answers, use `process.checks :http, url: "http://localhost:4567/", kind: :success, pattern: /html/, timeout: 3.seconds`. For an `https` URL, bluepill verifies the server's certificate. For a server with a self-signed certificate, add `verify_ssl: false`.
219
+
216
220
  To set the working directory to `cd` into when starting the command:
217
221
 
218
222
  ```ruby
@@ -327,6 +331,8 @@ process.monitor_children do |child_process|
327
331
  end
328
332
  ```
329
333
 
334
+ Bluepill runs this block once when the pill file loads, to check it, and again for each child it finds. `child_process.actual_pid` is the child's pid, and nil during that first run. The block must set a `stop_command`.
335
+
330
336
  Note `{{PID}}` will be substituted for the pid of process in both the stop and restart commands.
331
337
 
332
338
  ### Durations and sizes
@@ -337,7 +343,7 @@ If a pill file loads ActiveSupport, for example by loading a Rails app, ActiveSu
337
343
 
338
344
  ### Custom checks
339
345
 
340
- A check is a class in `Bluepill::ProcessConditions`, named after the check in camel case, so `checks :log_size` uses `LogSize`. Define it in the config file, before the application. It gets the check's options as keyword arguments, except `every`, `times`, `fires`, and `include_children`, which bluepill handles. `run` reads a value, and `check?` returns whether the value is fine:
346
+ A check is a subclass of `Bluepill::ProcessConditions::ProcessCondition`, in any module, named after the check in camel case, so `checks :log_size` uses a class named `LogSize`. Define it in the config file, before the application. It gets the check's options as keyword arguments, except `every`, `times`, `fires`, and `include_children`, which bluepill handles. Name the ones it needs, and pass the rest to `super`, which rejects any that no check takes. `run` reads a value, and `check?` returns whether the value is fine:
341
347
 
342
348
  ```ruby
343
349
  module Bluepill
@@ -370,9 +376,9 @@ Bluepill.application("app_name") do |app|
370
376
  end
371
377
  ```
372
378
 
373
- `logger` writes to bluepill's log, with the app's and the process's names, like the built-in checks. It's set once `super` returns, so call `super` first in `initialize`.
379
+ `logger` writes to bluepill's log, with the app's and the process's names, like the built-in checks. The check gets it through the `logger:` keyword, which `super` stores, so call `super` first in `initialize`: a check that doesn't call `super` has no logger.
374
380
 
375
- A check whose `initialize` takes the options as a Hash, `def initialize(options = {})`, and one written for bluepill 0.1, which defines `check` instead of `check?`, still work, but bluepill 0.3.0 needs keywords and `check?`, so `bluepill load` warns about them. The keywords and `check?` work in 0.2.9 too.
381
+ If `run`, `check?` or `format_value` raises, bluepill logs the error with its backtrace and skips the check until its next interval. It doesn't count toward `times`, and bluepill keeps monitoring. A check class must define both `run` and `check?`, or the pill file fails to load.
376
382
 
377
383
  ### Commands Don't Run in a Shell
378
384
 
@@ -506,7 +512,6 @@ JRuby allows you to run bluepill only in the foreground.
506
512
  This library aims to support and is [tested against][ci] the following Ruby
507
513
  implementations:
508
514
 
509
- * Ruby 2.7
510
515
  * Ruby 3.0 through 3.4
511
516
  * Ruby 4.0
512
517
  * JRuby 9.4 (only in the foreground)
data/bin/bluepill CHANGED
@@ -69,7 +69,7 @@ end
69
69
 
70
70
  APPLICATION_COMMANDS = %w[status start stop restart unmonitor quit log].freeze
71
71
 
72
- controller = Bluepill::Controller.new(options.slice(:base_dir, :log_file))
72
+ controller = Bluepill::Controller.new(**options.slice(:base_dir, :log_file))
73
73
 
74
74
  if controller.running_applications.include?(File.basename($PROGRAM_NAME)) && File.symlink?($PROGRAM_NAME)
75
75
  # bluepill was called as a symlink with the name of the target application
@@ -115,5 +115,11 @@ if options[:command] == "load"
115
115
  end
116
116
  else
117
117
  target = ARGV.shift
118
- controller.handle_command(options[:application], options[:command], target)
118
+ begin
119
+ controller.handle_command(options[:application], options[:command], target)
120
+ rescue Bluepill::Error => e
121
+ # The library raises what stops a command, with the status to exit with
122
+ $stderr.puts(e.message)
123
+ exit(e.exit_status)
124
+ end
119
125
  end
data/bluepill.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  "rubygems_mfa_required" => "true"
23
23
  }
24
24
 
25
- spec.required_ruby_version = ">= 2.7"
25
+ spec.required_ruby_version = ">= 3.0"
26
26
 
27
27
  # sig has the RBS signatures, and the standard libraries they use in manifest.yaml
28
28
  spec.executables = %w[bluepill bpsv]
@@ -12,20 +12,28 @@ module Bluepill
12
12
 
13
13
  # Start the daemon
14
14
  #
15
- # It stops a previous daemon for the application, clears journal locks
16
- # it left behind, daemonizes unless it runs in the foreground, opens the
17
- # socket, and runs until a signal stops it. Processes that are already
18
- # running, from their pid files, are monitored again, not restarted, so
19
- # bluepill can be upgraded without restarting them.
15
+ # It creates the pids directory, stops a previous daemon for the
16
+ # application, clears journal locks it left behind, daemonizes unless
17
+ # it runs in the foreground, traps signals, opens the socket, and runs
18
+ # until a signal stops it. Processes that are already running, from
19
+ # their pid files, are monitored again, not restarted, so bluepill can
20
+ # be upgraded without restarting them.
20
21
  #
21
22
  # @return [void]
22
23
  #
24
+ # @raise [StartupError] if it can't create the pids directory
25
+ # @raise [PreviousDaemonError] if it can't stop a previous daemon
26
+ # @raise [AlreadyRunningError] if a daemon answers on the socket
27
+ #
23
28
  # @api private
24
29
  def start_server
30
+ setup_pids_dir
25
31
  kill_previous_bluepill
26
32
  ProcessJournal.clear_all_atomic_fs_locks
27
33
  become_process_group_leader
28
- daemonize_and_trap_signals unless foreground?
34
+ # Trapped after daemonizing, which ignores HUP
35
+ Daemonize.daemonize unless foreground?
36
+ setup_signal_traps
29
37
  logger.reopen
30
38
 
31
39
  $0 = "bluepilld: #{name}"
@@ -45,17 +53,6 @@ module Bluepill
45
53
  # A session leader can't, and it already leads its process group
46
54
  end
47
55
 
48
- # Daemonize, and trap the signals again
49
- #
50
- # @return [void]
51
- #
52
- # @api private
53
- def daemonize_and_trap_signals
54
- Daemonize.daemonize
55
- # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
56
- setup_signal_traps
57
- end
58
-
59
56
  # Write the pid file, open the socket, and run the daemon
60
57
  #
61
58
  # @return [void]
@@ -123,38 +120,53 @@ module Bluepill
123
120
  end
124
121
  end
125
122
 
126
- # Create the logger and the pids directory
127
- #
128
- # The deprecation warnings go to the logger too.
123
+ # Create the logger, prefixed with the application's name
129
124
  #
130
- # Bluepill opens its log file and creates its pids dir as the user that
131
- # runs it. The uid and gid in the config only apply to its processes.
132
- # It exits with status 5 if it can't.
125
+ # Bluepill opens its log file as the user that runs it.
133
126
  #
134
127
  # @return [void]
135
128
  #
129
+ # @raise [StartupError] if it can't open the log file
130
+ #
136
131
  # @api private
137
- def setup_logger_and_pids_dir
138
- self.logger = ProcessJournal.logger = Deprecation.logger = Logger.new(log_file: log_file, log_level: @log_level, stdout: foreground?).prefix_with(name)
139
- setup_pids_dir
132
+ def setup_logger
133
+ self.logger = ProcessJournal.logger = Logger.new(log_file: log_file, log_level: @log_level, stdout: foreground?).prefix_with(name)
140
134
  rescue SystemCallError => e
141
- $stderr.puts("Failed to start bluepill: #{e}")
142
- message = "The user running bluepill must be able to write to its log_file and base_dir. " \
143
- "The uid and gid in the config only apply to the processes it monitors."
144
- $stderr.puts(message)
145
- exit(5)
135
+ raise unwritable(e)
146
136
  end
147
137
 
148
138
  # Create the pids directory, and let every user write to it
149
139
  #
140
+ # Bluepill creates it as the user that runs it.
141
+ #
150
142
  # @return [void]
151
143
  #
144
+ # @raise [StartupError] if it can't
145
+ #
152
146
  # @api private
153
147
  def setup_pids_dir
154
148
  FileUtils.mkdir_p(pids_dir)
155
149
  # we need everybody to be able to write to the pids_dir as processes managed by
156
150
  # bluepill will be writing to this dir after they've dropped privileges
157
151
  FileUtils.chmod(0o777, pids_dir)
152
+ rescue SystemCallError => e
153
+ raise unwritable(e)
154
+ end
155
+
156
+ # Return the error for a file that bluepill can't write to
157
+ #
158
+ # @param [SystemCallError] error what writing to it raised
159
+ #
160
+ # @return [StartupError] the error, which says the uid and gid in the
161
+ # config only apply to the processes bluepill monitors
162
+ #
163
+ # @api private
164
+ def unwritable(error)
165
+ StartupError.new(
166
+ "Failed to start bluepill: #{error}\n" \
167
+ "The user running bluepill must be able to write to its log_file and base_dir. " \
168
+ "The uid and gid in the config only apply to the processes it monitors."
169
+ )
158
170
  end
159
171
 
160
172
  # Write the daemon's pid to its pid file
@@ -162,9 +174,7 @@ module Bluepill
162
174
  # @return [void]
163
175
  #
164
176
  # @api private
165
- def write_pid_file
166
- File.write(pid_file, ::Process.pid.to_s)
167
- end
177
+ def write_pid_file = File.write(pid_file, ::Process.pid.to_s)
168
178
  end
169
179
  end
170
180
  end
@@ -93,7 +93,7 @@ module Bluepill
93
93
  def daemon_command(command)
94
94
  # The backtrace is caller's lines, with no locations, so Ruby 3.1's
95
95
  # error_highlight doesn't add a snippet of this file to the message
96
- DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
96
+ DAEMON_COMMANDS.detect { |known| known.name.eql?(command) } ||
97
97
  raise(NoMethodError, "Unknown command `#{command}`", caller)
98
98
  end
99
99
 
@@ -13,11 +13,13 @@ module Bluepill
13
13
 
14
14
  # Interrupt a daemon for this application that's still running
15
15
  #
16
- # It waits up to kill_timeout half seconds for the daemon to exit, and
17
- # exits with status 4 if it doesn't, or if signaling it fails.
16
+ # It waits up to kill_timeout half seconds for the daemon to exit.
18
17
  #
19
18
  # @return [void]
20
19
  #
20
+ # @raise [PreviousDaemonError] if signaling it fails, or it doesn't
21
+ # exit in time
22
+ #
21
23
  # @api private
22
24
  def kill_previous_bluepill
23
25
  previous_pid = interrupt_previous_bluepill
@@ -26,11 +28,11 @@ module Bluepill
26
28
 
27
29
  # Interrupt the previous daemon, if its pid file names a live one
28
30
  #
29
- # It exits with status 4 if signaling it fails, unless it's gone.
30
- #
31
31
  # @return [Integer, nil] the previous daemon's pid, or nil if it
32
32
  # interrupted none, such as when it has just exited
33
33
  #
34
+ # @raise [PreviousDaemonError] if signaling it fails
35
+ #
34
36
  # @api private
35
37
  def interrupt_previous_bluepill
36
38
  previous_pid = previous_bluepill_pid
@@ -41,11 +43,10 @@ module Bluepill
41
43
  ::Process.kill(2, previous_pid)
42
44
  previous_pid
43
45
  rescue Errno::ESRCH
44
- # It exited after previous_bluepill_pid saw it alive, so there's
45
- # nothing to wait for
46
+ # It exited after previous_bluepill_pid saw it alive
47
+ nil
46
48
  rescue => e
47
- $stderr.puts "Encountered error trying to kill previous bluepill:\n#{e.class}: #{e}"
48
- exit(4)
49
+ raise PreviousDaemonError, "Encountered error trying to kill previous bluepill:\n#{e.class}: #{e}"
49
50
  end
50
51
 
51
52
  # Return the pid of a previous daemon that's still running
@@ -61,12 +62,12 @@ module Bluepill
61
62
 
62
63
  # Wait up to kill_timeout half seconds for the previous daemon to exit
63
64
  #
64
- # It exits with status 4 if the daemon is still running after.
65
- #
66
65
  # @param [Integer] previous_pid the previous daemon's pid
67
66
  #
68
67
  # @return [void]
69
68
  #
69
+ # @raise [PreviousDaemonError] if it's still running after
70
+ #
70
71
  # @api private
71
72
  def wait_for_previous_bluepill(previous_pid)
72
73
  kill_timeout.times do |_i|
@@ -75,10 +76,7 @@ module Bluepill
75
76
  break unless System.pid_alive?(previous_pid)
76
77
  end
77
78
 
78
- return unless System.pid_alive?(previous_pid)
79
-
80
- $stderr.puts "Previous bluepilld[#{previous_pid}] didn't die"
81
- exit(4)
79
+ raise PreviousDaemonError, "Previous bluepilld[#{previous_pid}] didn't die" if System.pid_alive?(previous_pid)
82
80
  end
83
81
  end
84
82
  end
@@ -96,29 +96,37 @@ module Bluepill
96
96
 
97
97
  # Create the daemon for an application
98
98
  #
99
- # It sets up signal traps, the logger and the pids directory. It exits
100
- # with status 5 if it can't write to its log file or base directory.
99
+ # It opens the log, so the pill file's processes can log through it.
100
+ # Starting the daemon traps signals and creates the pids directory.
101
101
  #
102
102
  # @param [String] name the application's name
103
- # @param [Hash{Symbol => Object}] options :foreground, :log_file,
104
- # :log_level, :base_dir and :kill_timeout
103
+ # @param [Boolean] foreground whether to run in the foreground, rather
104
+ # than as a daemon
105
+ # @param [String, nil] log_file the file to log to, or nil for syslog
106
+ # @param [Symbol, String] log_level the least severe priority to log
107
+ # (debug, so everything, by default)
108
+ # @param [String, nil] base_dir the directory for the socket and pid
109
+ # files, or nil for the default
110
+ # @param [Numeric] kill_timeout how long to wait for an earlier daemon
111
+ # to exit, in half seconds
105
112
  #
106
113
  # @return [void]
107
114
  #
115
+ # @raise [StartupError] if it can't open its log file
116
+ #
108
117
  # @api private
109
- def initialize(name, options = {})
118
+ def initialize(name, foreground: false, log_file: nil, log_level: :debug, base_dir: nil, kill_timeout: 10)
110
119
  self.name = name
111
120
 
112
- @foreground = options[:foreground]
113
- self.log_file = options[:log_file]
114
- @log_level = options[:log_level]
115
- setup_base_dir(options[:base_dir])
116
- self.kill_timeout = options[:kill_timeout] || 10
121
+ @foreground = foreground
122
+ self.log_file = log_file
123
+ @log_level = log_level
124
+ setup_base_dir(base_dir)
125
+ self.kill_timeout = kill_timeout
117
126
 
118
127
  self.groups = {}
119
128
 
120
- setup_signal_traps
121
- setup_logger_and_pids_dir
129
+ setup_logger
122
130
 
123
131
  @mutex = Mutex.new
124
132
  end
@@ -128,9 +136,7 @@ module Bluepill
128
136
  # @return [Boolean]
129
137
  #
130
138
  # @api private
131
- def foreground?
132
- !!@foreground
133
- end
139
+ def foreground? = @foreground
134
140
 
135
141
  # Run a block while holding the daemon's lock
136
142
  #
@@ -142,27 +148,27 @@ module Bluepill
142
148
  # @return [Object] what the block returns
143
149
  #
144
150
  # @api private
145
- def synchronize(&block)
146
- @mutex.synchronize(&block)
147
- end
151
+ def synchronize(&block) = @mutex.synchronize(&block)
148
152
 
149
153
  # Start the daemon and monitor the application's processes
150
154
  #
151
- # It prints what went wrong, and exits with status 5, if the daemon
152
- # fails to start. Whatever stops it is also logged at crit, with its
153
- # backtrace, since a daemon's stderr is /dev/null.
155
+ # It returns when a signal stops the daemon. Whatever stops it before
156
+ # then is logged at crit, with its backtrace, since a daemon's stderr
157
+ # is /dev/null, and raised as a {Bluepill::Error}.
154
158
  #
155
159
  # @return [void]
156
160
  #
161
+ # @raise [Bluepill::Error] if the daemon fails to start, or stops
162
+ # without a signal, such as a {StartupError} for any other error
163
+ #
157
164
  # @api private
158
165
  def load
159
166
  start_server
160
167
  rescue => e
161
168
  log_crash(e)
162
- $stderr.puts("Failed to start bluepill:")
163
- $stderr.puts(format("%s `%s`", e.class.name, e.message))
164
- $stderr.puts(e.backtrace)
165
- exit(5)
169
+ raise if e.is_a?(Error)
170
+
171
+ raise StartupError, ["Failed to start bluepill:", format("%s `%s`", e.class.name, e.message), e.backtrace].join("\n")
166
172
  end
167
173
 
168
174
  # @!method start(group_name = nil, process_name = nil)
@@ -250,12 +256,10 @@ module Bluepill
250
256
  # It's a String, since Marshal sends a module as only its name, which
251
257
  # the CLI would load as its own Version.
252
258
  #
253
- # @return [String] the version, such as "0.2.9"
259
+ # @return [String] the version, such as "0.3.0"
254
260
  #
255
261
  # @api private
256
- def version
257
- Version.to_s
258
- end
262
+ def version = Version.to_s
259
263
 
260
264
  protected
261
265