bluepill 0.2.9 → 0.3.2

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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -2
  3. data/README.md +16 -13
  4. data/bin/bluepill +21 -8
  5. data/bluepill.gemspec +7 -2
  6. data/lib/bluepill/application/lifecycle.rb +48 -35
  7. data/lib/bluepill/application/listener.rb +2 -4
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +36 -30
  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 +46 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +35 -37
  16. data/lib/bluepill/daemonize.rb +52 -0
  17. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  18. data/lib/bluepill/dsl/attributes.rb +135 -0
  19. data/lib/bluepill/dsl/process_factory/child_factory.rb +83 -0
  20. data/lib/bluepill/dsl/process_factory/registry.rb +4 -26
  21. data/lib/bluepill/dsl/process_factory.rb +85 -84
  22. data/lib/bluepill/dsl/process_proxy.rb +80 -160
  23. data/lib/bluepill/dsl.rb +16 -15
  24. data/lib/bluepill/errors.rb +190 -0
  25. data/lib/bluepill/group.rb +14 -37
  26. data/lib/bluepill/logger.rb +65 -99
  27. data/lib/bluepill/process/attributes.rb +66 -32
  28. data/lib/bluepill/process/children.rb +6 -7
  29. data/lib/bluepill/process/pid.rb +43 -35
  30. data/lib/bluepill/process/state_machine.rb +15 -59
  31. data/lib/bluepill/process/stop_signals.rb +123 -0
  32. data/lib/bluepill/process/transition.rb +1 -3
  33. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  34. data/lib/bluepill/process/triggers.rb +90 -0
  35. data/lib/bluepill/process/user_commands.rb +1 -1
  36. data/lib/bluepill/process/watches.rb +9 -23
  37. data/lib/bluepill/process.rb +55 -52
  38. data/lib/bluepill/process_commands/execution.rb +16 -24
  39. data/lib/bluepill/process_commands/hooks.rb +1 -31
  40. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  41. data/lib/bluepill/process_commands.rb +10 -8
  42. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  43. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  44. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  45. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  46. data/lib/bluepill/process_conditions/http.rb +29 -67
  47. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  48. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  49. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  50. data/lib/bluepill/process_conditions.rb +5 -14
  51. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  52. data/lib/bluepill/process_journal/config.rb +73 -0
  53. data/lib/bluepill/process_journal/entries.rb +128 -0
  54. data/lib/bluepill/process_journal/journal.rb +69 -0
  55. data/lib/bluepill/process_journal/kind.rb +60 -0
  56. data/lib/bluepill/process_journal/lock.rb +9 -4
  57. data/lib/bluepill/process_journal.rb +13 -324
  58. data/lib/bluepill/process_statistics.rb +3 -7
  59. data/lib/bluepill/socket.rb +19 -17
  60. data/lib/bluepill/system/command_options.rb +136 -0
  61. data/lib/bluepill/system/command_result.rb +73 -0
  62. data/lib/bluepill/system/command_setup.rb +3 -4
  63. data/lib/bluepill/system/command_spawn.rb +244 -0
  64. data/lib/bluepill/system/commands.rb +27 -40
  65. data/lib/bluepill/system/daemon.rb +97 -15
  66. data/lib/bluepill/system/files.rb +35 -0
  67. data/lib/bluepill/system/ps.rb +20 -17
  68. data/lib/bluepill/system/ps_table.rb +7 -9
  69. data/lib/bluepill/system/usage.rb +175 -0
  70. data/lib/bluepill/system.rb +7 -192
  71. data/lib/bluepill/trigger.rb +18 -69
  72. data/lib/bluepill/triggers/flapping.rb +12 -8
  73. data/lib/bluepill/units.rb +7 -21
  74. data/lib/bluepill/version.rb +16 -26
  75. data/lib/bluepill.rb +1 -1
  76. data/sig/bluepill/application.rbs +11 -12
  77. data/sig/bluepill/attributes.rbs +48 -0
  78. data/sig/bluepill/checks.rbs +40 -40
  79. data/sig/bluepill/dsl.rbs +100 -59
  80. data/sig/bluepill/errors.rbs +48 -0
  81. data/sig/bluepill/logger.rbs +15 -31
  82. data/sig/bluepill/process.rbs +39 -305
  83. data/sig/bluepill/process_commands.rbs +235 -0
  84. data/sig/bluepill/stop_signals.rbs +21 -0
  85. data/sig/bluepill/system.rbs +191 -101
  86. data/sig/bluepill/version.rbs +17 -0
  87. data/sig/bluepill.rbs +2 -17
  88. data/sig/manifest.yaml +1 -0
  89. metadata +22 -8
  90. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  91. data/lib/bluepill/deprecation/dsl.rb +0 -141
  92. data/lib/bluepill/deprecation/options.rb +0 -140
  93. data/lib/bluepill/deprecation.rb +0 -92
  94. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  95. 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: '0208aabbcc97f59a0e1186f9d01d58b96a3193b378a33341e38914f5817e2dde'
4
+ data.tar.gz: a985b9962e81fcce8a800e3154a971fcf8864cf43905f31abc91fed6cfdf0e2d
5
5
  SHA512:
6
- metadata.gz: 0b8fcfe797993aaa1f5663e96b0a68028e1979181c2d2d5d36dc81ebaa44f8549c7c84d83f9598392d1f36740c8ffa1e89b710927045427ed3a945951e03135c
7
- data.tar.gz: 40be778dcf5a90336923599c6bc9d9cb8485ee986d8da46ea0169c4264023383e3416a3296d133106391a9bdcc709e4c42cd498208c130ca9e486ab5335cddf6
6
+ metadata.gz: e260492f6536962844cf956db8493aa730a28c53a08a18dbca4a1ac53d53e54f9c1b9e2e6edfad165205a0b4021844878099ccb25a486b8fa7a9fdd2d0125ef8
7
+ data.tar.gz: 848b4037e5cf18f6b4ad5db27fd5dbe22e443101619af3c6e4a8e944bff85189ebafcb6353615bb7df73c40a65dda1c6294ab5ca0849f23e5c19d2a3237f2126
data/CHANGELOG.md CHANGED
@@ -7,6 +7,84 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.2] - 2026-11-02
11
+
12
+ ### Removed
13
+
14
+ - **Breaking:** support for Ruby 3.0 and 3.1, and for JRuby 9.4, which runs Ruby 3.1 code. Bluepill 0.3.2 needs Ruby 3.2 or newer, or JRuby 10.0 or newer, which needs Java 21. Bluepill 0.3.0 still runs on them.
15
+
16
+ ### Fixed
17
+
18
+ - Bluepill runs on JRuby, in the foreground and as a daemon. It forked to start every process and command, which the JVM can't do, so on JRuby it started none. On JRuby it now spawns them, and starts a new bluepill to be the daemon. A process that bluepill starts there runs in a process group of its own, as a forked one's new session does, and stays bluepill's child, which collects its exit status. Spawned commands can't switch to another user, so on JRuby a config that sets `uid`, `gid` or `supplementary_groups` fails to load when bluepill runs as root. CRuby runs bluepill as before.
19
+
20
+ ## [0.3.0] - 2026-11-02
21
+
22
+ ### Upgrading from 0.2
23
+
24
+ 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:
25
+
26
+ - **Bluepill needs Ruby 3.0 or newer, or JRuby 9.4 or newer.** On Ruby 2.7, stay on 0.2.9.
27
+ - **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?`.
28
+ - **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.
29
+ - **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`.
30
+ - **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.
31
+ - **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.
32
+ - **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.
33
+ - **Set attributes with `=`.** `process.daemonize!` and `process.start_command "cmd"` no longer work. Write `process.daemonize = true` and `process.start_command = "cmd"`.
34
+ - **`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`.
35
+ - **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.
36
+ - **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.
37
+ - **A custom check that subclasses `MemUsage` gets bytes.** Its `run`, `check?` and `format_value` see the memory usage in bytes, where they saw kilobytes.
38
+ - **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.
39
+ - **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.
40
+
41
+ ### Added
42
+
43
+ - `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`.
44
+ - The `http` check's `verify_ssl:` option. Set it to false for a server with a self-signed certificate.
45
+ - A custom check or trigger can be defined in any module, since bluepill finds it by its class name.
46
+ - The daemon logs what stops it at `crit`, with the backtrace, since its stderr is `/dev/null`.
47
+
48
+ ### Changed
49
+
50
+ - **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.
51
+ - **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").
52
+ - 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`.
53
+ - A config error's message starts with the pill file's line, as in `app.pill.rb:5: Unknown attribute ...`.
54
+ - **Breaking:** a misspelled application attribute, such as `app.uidd = "deploy"`, raises `Bluepill::ConfigError` with a suggestion, as a misspelled process attribute does.
55
+ - **Breaking:** an unknown check name raises `Bluepill::ConfigError` with a did-you-mean suggestion, where it raised `NameError`.
56
+ - **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.
57
+ - **Breaking:** `on_start_timeout` defaults to `:start`, and a value that isn't one of `:start`, `:stop`, `:unmonitor` or `:restart` fails the config.
58
+ - **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.
59
+ - **Breaking:** the `http` check verifies TLS certificates.
60
+ - **Breaking:** reading an attribute that isn't set returns nil instead of raising `NameError`.
61
+ - **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.
62
+ - **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).
63
+ - 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.
64
+ - The daemon traps signals and creates its pids directory when it starts, not while the pill file loads.
65
+ - **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.
66
+ - **Breaking:** each process reaches its pid and process group journals through `ProcessJournal.for(name)`, and a journal lock held too long raises `ProcessJournal::LockTimeoutError`.
67
+ - **Breaking:** the ps table is kept in `System`'s own state, and `System.store` is gone.
68
+ - Checking a process's CPU takes 8% fewer instructions, and the `mem_usage` check 1.4% fewer.
69
+ - 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.
70
+ - A process's `stop_signals` is parsed and checked once, when the pill file loads, into a `Bluepill::Process::StopSignals`.
71
+ - **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.
72
+
73
+ ### Removed
74
+
75
+ - **Breaking:** a custom check that defines `check`, as in bluepill 0.1, no longer works. Name the method `check?`, as bluepill 0.2 does.
76
+ - **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.
77
+ - **Breaking:** the undocumented `name!` and `name value` forms of setting a process attribute.
78
+ - **Breaking:** setting `monitor_children` or `child_process_factory` in a process block. Use `monitor_children` with a block.
79
+ - **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.
80
+ - **Breaking:** `Process#start`, `#stop`, `#restart` and `#unmonitor` (use `dispatch!`), `Trigger.[]` and `ProcessConditions.[]` (use `CheckRegistry.fetch`), `Group#process_logger`, and `ConditionWatch::HistoryValue#critical` (now `passed`).
81
+
82
+ ### Fixed
83
+
84
+ - Two checks of the same kind, such as two `:http` checks of different URLs, both run. Before, only the last one did.
85
+ - 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.
86
+ - `bluepill log` quotes the log file's path and the pattern, so a path with spaces or shell characters works.
87
+
10
88
  ## [0.2.9] - 2026-11-02
11
89
 
12
90
  ### Added
@@ -138,11 +216,39 @@ To upgrade, install 0.2.0 and run `bluepill load` with the same pill file. It re
138
216
 
139
217
  - 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
218
 
219
+ ## [0.1.7] - 2026-11-02
220
+
221
+ ### Added
222
+
223
+ - 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.
224
+
225
+ ### Deprecated
226
+
227
+ - 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.
228
+ - 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.
229
+ - 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.
230
+ - `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.
231
+ - 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.
232
+ - `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`.
233
+ - 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.
234
+ - 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.
235
+
236
+ ### Fixed
237
+
238
+ - 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.
239
+ - 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.
240
+ - 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.
241
+ - 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.
242
+
243
+ ### Security
244
+
245
+ - `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.
246
+ - 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.
247
+
141
248
  ## [0.1.6] - 2026-11-02
142
249
 
143
250
  ### Security
144
251
 
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
252
  - 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
253
 
148
254
  ## [0.1.5] - 2026-11-01
@@ -896,13 +1002,16 @@ Initial release.
896
1002
  - Added the `bluepill` CLI with the `load`, `status`, `start`, `stop`, `restart`, `unmonitor`, `quit` and `log` commands.
897
1003
  - Added syslog logging.
898
1004
 
899
- [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.2.9...HEAD
1005
+ [unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.3.2...HEAD
1006
+ [0.3.2]: https://github.com/bluepill-rb/bluepill/compare/v0.3.0...v0.3.2
1007
+ [0.3.0]: https://github.com/bluepill-rb/bluepill/compare/v0.2.9...v0.3.0
900
1008
  [0.2.9]: https://github.com/bluepill-rb/bluepill/compare/v0.2.7...v0.2.9
901
1009
  [0.2.7]: https://github.com/bluepill-rb/bluepill/compare/v0.2.5...v0.2.7
902
1010
  [0.2.5]: https://github.com/bluepill-rb/bluepill/compare/v0.2.3...v0.2.5
903
1011
  [0.2.3]: https://github.com/bluepill-rb/bluepill/compare/v0.2.1...v0.2.3
904
1012
  [0.2.1]: https://github.com/bluepill-rb/bluepill/compare/v0.2.0...v0.2.1
905
1013
  [0.2.0]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...v0.2.0
1014
+ [0.1.7]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...v0.1.7
906
1015
  [0.1.6]: https://github.com/bluepill-rb/bluepill/compare/v0.1.5...v0.1.6
907
1016
  [0.1.5]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...v0.1.5
908
1017
  [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
@@ -196,6 +198,8 @@ end
196
198
 
197
199
  These only apply when bluepill runs as root. Every command a process runs, such as its `start_command`, `stop_command` and `pre_start_command`, runs as its user and groups, and so do the commands of its children with `monitor_children`.
198
200
 
201
+ On JRuby, bluepill can't run commands as another user or group, since the JVM can't fork, so a config that sets `uid`, `gid` or `supplementary_groups` fails to load when bluepill runs as root. Run it on CRuby to use them.
202
+
199
203
  To track resources of child processes, use `:include_children`:
200
204
  ```ruby
201
205
  Bluepill.application("app_name") do |app|
@@ -213,6 +217,8 @@ To check for flapping:
213
217
  process.checks :flapping, times: 2, within: 30.seconds, retry_in: 7.seconds
214
218
  ```
215
219
 
220
+ 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`.
221
+
216
222
  To set the working directory to `cd` into when starting the command:
217
223
 
218
224
  ```ruby
@@ -327,6 +333,8 @@ process.monitor_children do |child_process|
327
333
  end
328
334
  ```
329
335
 
336
+ 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`.
337
+
330
338
  Note `{{PID}}` will be substituted for the pid of process in both the stop and restart commands.
331
339
 
332
340
  ### Durations and sizes
@@ -337,7 +345,7 @@ If a pill file loads ActiveSupport, for example by loading a Rails app, ActiveSu
337
345
 
338
346
  ### Custom checks
339
347
 
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:
348
+ 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
349
 
342
350
  ```ruby
343
351
  module Bluepill
@@ -370,9 +378,9 @@ Bluepill.application("app_name") do |app|
370
378
  end
371
379
  ```
372
380
 
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`.
381
+ `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
382
 
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.
383
+ 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
384
 
377
385
  ### Commands Don't Run in a Shell
378
386
 
@@ -491,9 +499,6 @@ end
491
499
 
492
500
  Note that You must define only one application per config when using foreground mode.
493
501
 
494
-
495
- JRuby allows you to run bluepill only in the foreground.
496
-
497
502
  ## Links
498
503
 
499
504
  * Code: [http://github.com/bluepill-rb/bluepill](http://github.com/bluepill-rb/bluepill)
@@ -506,11 +511,9 @@ JRuby allows you to run bluepill only in the foreground.
506
511
  This library aims to support and is [tested against][ci] the following Ruby
507
512
  implementations:
508
513
 
509
- * Ruby 2.7
510
- * Ruby 3.0 through 3.4
514
+ * Ruby 3.2 through 3.4
511
515
  * Ruby 4.0
512
- * JRuby 9.4 (only in the foreground)
513
- * JRuby 10.0 and 10.1 (only in the foreground)
516
+ * JRuby 10.0 and 10.1, without `uid`, `gid` and `supplementary_groups`
514
517
 
515
518
  If something doesn't work on one of these interpreters, please open an issue.
516
519
 
data/bin/bluepill CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
+
3
4
  require "optparse"
4
5
  require "rbconfig"
5
6
  require "bluepill"
@@ -7,7 +8,8 @@ require "bluepill"
7
8
  # Default options
8
9
  options = {
9
10
  log_file: "/var/log/bluepill.log",
10
- base_dir: ENV["BLUEPILL_BASE_DIR"] || (::Process.euid.zero? ? "/var/run/bluepill" : File.join(ENV.fetch("HOME", nil), ".bluepill")),
11
+ base_dir: ENV["BLUEPILL_BASE_DIR"] ||
12
+ (::Process.euid.zero? ? "/var/run/bluepill" : File.join(ENV.fetch("HOME", nil), ".bluepill")),
11
13
  privileged: true,
12
14
  timeout: 10,
13
15
  attempts: 1
@@ -19,7 +21,8 @@ OptionParser.new do |opts|
19
21
  options[:log_file] = file
20
22
  end
21
23
 
22
- opts.on("-c", "--base-dir DIR", "Directory to store bluepill socket and pid files, defaults to #{options[:base_dir]}") do |base_dir|
24
+ opts.on("-c", "--base-dir DIR",
25
+ "Directory to store bluepill socket and pid files, defaults to #{options[:base_dir]}") do |base_dir|
23
26
  options[:base_dir] = base_dir
24
27
  end
25
28
 
@@ -28,15 +31,18 @@ OptionParser.new do |opts|
28
31
  exit
29
32
  end
30
33
 
31
- opts.on("--[no-]privileged", "Allow/disallow to run #{$PROGRAM_NAME} as non-privileged process. disallowed by default") do |v|
34
+ opts.on("--[no-]privileged",
35
+ "Allow/disallow to run #{$PROGRAM_NAME} as non-privileged process. disallowed by default") do |v|
32
36
  options[:privileged] = v
33
37
  end
34
38
 
35
- opts.on("-t", "--timeout Seconds", Integer, "Timeout for commands sent to the daemon, in seconds. Defaults to 10.") do |timeout|
39
+ opts.on("-t", "--timeout Seconds", Integer,
40
+ "Timeout for commands sent to the daemon, in seconds. Defaults to 10.") do |timeout|
36
41
  options[:timeout] = timeout
37
42
  end
38
43
 
39
- opts.on("--attempts Count", Integer, "Attempts for commands sent to the daemon, in seconds. Defaults to 1.") do |attempts|
44
+ opts.on("--attempts Count", Integer,
45
+ "Attempts for commands sent to the daemon, in seconds. Defaults to 1.") do |attempts|
40
46
  options[:attempts] = attempts
41
47
  end
42
48
 
@@ -69,7 +75,7 @@ end
69
75
 
70
76
  APPLICATION_COMMANDS = %w[status start stop restart unmonitor quit log].freeze
71
77
 
72
- controller = Bluepill::Controller.new(options.slice(:base_dir, :log_file))
78
+ controller = Bluepill::Controller.new(**options.slice(:base_dir, :log_file))
73
79
 
74
80
  if controller.running_applications.include?(File.basename($PROGRAM_NAME)) && File.symlink?($PROGRAM_NAME)
75
81
  # bluepill was called as a symlink with the name of the target application
@@ -91,7 +97,8 @@ elsif APPLICATION_COMMANDS.include?(ARGV.first)
91
97
  exit(1)
92
98
  else
93
99
  # There are none running AND they aren't trying to start one
94
- $stderr.puts "Error: There are no running bluepill daemons.\nTo start a bluepill daemon, use: bluepill load <config file>"
100
+ $stderr.puts "Error: There are no running bluepill daemons.\n" \
101
+ "To start a bluepill daemon, use: bluepill load <config file>"
95
102
  exit(2)
96
103
  end
97
104
  end
@@ -115,5 +122,11 @@ if options[:command] == "load"
115
122
  end
116
123
  else
117
124
  target = ARGV.shift
118
- controller.handle_command(options[:application], options[:command], target)
125
+ begin
126
+ controller.handle_command(options[:application], options[:command], target)
127
+ rescue Bluepill::Error => e
128
+ # The library raises what stops a command, with the status to exit with
129
+ $stderr.puts(e.message)
130
+ exit(e.exit_status)
131
+ end
119
132
  end
data/bluepill.gemspec CHANGED
@@ -12,7 +12,12 @@ Gem::Specification.new do |spec|
12
12
  spec.email = ["entombedvirus@gmail.com"]
13
13
  spec.homepage = "https://github.com/bluepill-rb/bluepill"
14
14
  spec.summary = "A process monitor written in Ruby with stability and minimalism in mind."
15
- spec.description = "Bluepill keeps your daemons up while taking up as little resources as possible. After all you probably want the resources of your server to be used by whatever daemons you are running rather than the thing that's supposed to make sure they are brought back up, should they die or misbehave."
15
+ spec.description = <<~DESCRIPTION.tr("\n", " ").strip
16
+ Bluepill keeps your daemons up while taking up as little resources as possible.
17
+ After all you probably want the resources of your server to be used by whatever
18
+ daemons you are running rather than the thing that's supposed to make sure they
19
+ are brought back up, should they die or misbehave.
20
+ DESCRIPTION
16
21
  spec.license = "MIT"
17
22
 
18
23
  spec.metadata = {
@@ -22,7 +27,7 @@ Gem::Specification.new do |spec|
22
27
  "rubygems_mfa_required" => "true"
23
28
  }
24
29
 
25
- spec.required_ruby_version = ">= 2.7"
30
+ spec.required_ruby_version = ">= 3.2"
26
31
 
27
32
  # sig has the RBS signatures, and the standard libraries they use in manifest.yaml
28
33
  spec.executables = %w[bluepill bpsv]
@@ -12,20 +12,30 @@ 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, or in
17
+ # the foreground leads a process group of its own, traps signals,
18
+ # opens the socket, and runs
19
+ # until a signal stops it. Processes that are already running, from
20
+ # their pid files, are monitored again, not restarted, so bluepill can
21
+ # be upgraded without restarting them.
20
22
  #
21
23
  # @return [void]
22
24
  #
25
+ # @raise [StartupError] if it can't create the pids directory
26
+ # @raise [PreviousDaemonError] if it can't stop a previous daemon
27
+ # @raise [AlreadyRunningError] if a daemon answers on the socket
28
+ #
23
29
  # @api private
24
30
  def start_server
31
+ setup_pids_dir
25
32
  kill_previous_bluepill
26
33
  ProcessJournal.clear_all_atomic_fs_locks
27
- become_process_group_leader
28
- daemonize_and_trap_signals unless foreground?
34
+ # Daemonizing starts a new session, which has a process group of its
35
+ # own, and a process that leads a process group already can't
36
+ foreground? ? become_process_group_leader : Daemonize.daemonize
37
+ # Trapped after daemonizing, which ignores HUP
38
+ setup_signal_traps
29
39
  logger.reopen
30
40
 
31
41
  $0 = "bluepilld: #{name}"
@@ -45,17 +55,6 @@ module Bluepill
45
55
  # A session leader can't, and it already leads its process group
46
56
  end
47
57
 
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
58
  # Write the pid file, open the socket, and run the daemon
60
59
  #
61
60
  # @return [void]
@@ -123,38 +122,54 @@ module Bluepill
123
122
  end
124
123
  end
125
124
 
126
- # Create the logger and the pids directory
127
- #
128
- # The deprecation warnings go to the logger too.
125
+ # Create the logger, prefixed with the application's name
129
126
  #
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.
127
+ # Bluepill opens its log file as the user that runs it.
133
128
  #
134
129
  # @return [void]
135
130
  #
131
+ # @raise [StartupError] if it can't open the log file
132
+ #
136
133
  # @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
134
+ def setup_logger
135
+ logger = Logger.new(log_file:, log_level: @log_level, stdout: foreground?).prefix_with(name)
136
+ self.logger = ProcessJournal.logger = logger
140
137
  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)
138
+ raise unwritable(e)
146
139
  end
147
140
 
148
141
  # Create the pids directory, and let every user write to it
149
142
  #
143
+ # Bluepill creates it as the user that runs it.
144
+ #
150
145
  # @return [void]
151
146
  #
147
+ # @raise [StartupError] if it can't
148
+ #
152
149
  # @api private
153
150
  def setup_pids_dir
154
151
  FileUtils.mkdir_p(pids_dir)
155
152
  # we need everybody to be able to write to the pids_dir as processes managed by
156
153
  # bluepill will be writing to this dir after they've dropped privileges
157
154
  FileUtils.chmod(0o777, pids_dir)
155
+ rescue SystemCallError => e
156
+ raise unwritable(e)
157
+ end
158
+
159
+ # Return the error for a file that bluepill can't write to
160
+ #
161
+ # @param [SystemCallError] error what writing to it raised
162
+ #
163
+ # @return [StartupError] the error, which says the uid and gid in the
164
+ # config only apply to the processes bluepill monitors
165
+ #
166
+ # @api private
167
+ def unwritable(error)
168
+ StartupError.new(
169
+ "Failed to start bluepill: #{error}\n" \
170
+ "The user running bluepill must be able to write to its log_file and base_dir. " \
171
+ "The uid and gid in the config only apply to the processes it monitors."
172
+ )
158
173
  end
159
174
 
160
175
  # Write the daemon's pid to its pid file
@@ -162,9 +177,7 @@ module Bluepill
162
177
  # @return [void]
163
178
  #
164
179
  # @api private
165
- def write_pid_file
166
- File.write(pid_file, ::Process.pid.to_s)
167
- end
180
+ def write_pid_file = File.write(pid_file, ::Process.pid.to_s)
168
181
  end
169
182
  end
170
183
  end
@@ -91,10 +91,8 @@ module Bluepill
91
91
  #
92
92
  # @api private
93
93
  def daemon_command(command)
94
- # The backtrace is caller's lines, with no locations, so Ruby 3.1's
95
- # error_highlight doesn't add a snippet of this file to the message
96
- DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
97
- raise(NoMethodError, "Unknown command `#{command}`", caller)
94
+ DAEMON_COMMANDS.detect { |known| known.name.eql?(command) } ||
95
+ raise(NoMethodError, "Unknown command `#{command}`")
98
96
  end
99
97
 
100
98
  # Log an error that answering a client raised, with its backtrace
@@ -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