bluepill 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -21
- data/CONTRIBUTING.md +2 -2
- data/README.md +100 -20
- data/bin/bluepill +25 -30
- data/bluepill.gemspec +21 -28
- data/lib/bluepill/application/lifecycle.rb +166 -0
- data/lib/bluepill/application/listener.rb +124 -0
- data/lib/bluepill/application/previous_daemon.rb +81 -0
- data/lib/bluepill/application.rb +59 -259
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +129 -65
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -118
- data/lib/bluepill/daemonize.rb +234 -0
- data/lib/bluepill/dsl/app_proxy.rb +30 -6
- data/lib/bluepill/dsl/process_factory/children.rb +71 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
- data/lib/bluepill/dsl/process_factory.rb +81 -152
- data/lib/bluepill/dsl/process_proxy.rb +100 -51
- data/lib/bluepill/dsl.rb +0 -23
- data/lib/bluepill/group.rb +187 -78
- data/lib/bluepill/logger.rb +139 -48
- data/lib/bluepill/pid_file.rb +23 -0
- data/lib/bluepill/process/attributes.rb +379 -0
- data/lib/bluepill/process/children.rb +70 -0
- data/lib/bluepill/process/pid.rb +154 -0
- data/lib/bluepill/process/state_machine.rb +196 -0
- data/lib/bluepill/process/transition.rb +45 -0
- data/lib/bluepill/process/transition_callbacks.rb +101 -0
- data/lib/bluepill/process/user_commands.rb +71 -0
- data/lib/bluepill/process/watches.rb +141 -0
- data/lib/bluepill/process.rb +64 -667
- data/lib/bluepill/process_commands/execution.rb +89 -0
- data/lib/bluepill/process_commands/hooks.rb +186 -0
- data/lib/bluepill/process_commands/stop_signals.rb +114 -0
- data/lib/bluepill/process_commands.rb +106 -174
- data/lib/bluepill/process_conditions/always_true.rb +1 -1
- data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
- data/lib/bluepill/process_conditions/file_time.rb +4 -5
- data/lib/bluepill/process_conditions/http.rb +48 -45
- data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
- data/lib/bluepill/process_conditions/process_condition.rb +34 -3
- data/lib/bluepill/process_conditions/running_time.rb +1 -25
- data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
- data/lib/bluepill/process_conditions.rb +3 -2
- data/lib/bluepill/process_journal/cleanup.rb +200 -0
- data/lib/bluepill/process_journal/lock.rb +106 -0
- data/lib/bluepill/process_journal.rb +126 -233
- data/lib/bluepill/process_statistics.rb +2 -2
- data/lib/bluepill/socket.rb +60 -20
- data/lib/bluepill/system/command_setup.rb +98 -0
- data/lib/bluepill/system/commands.rb +219 -248
- data/lib/bluepill/system/daemon.rb +161 -0
- data/lib/bluepill/system/ps.rb +85 -0
- data/lib/bluepill/system/ps_table.rb +163 -0
- data/lib/bluepill/system.rb +45 -126
- data/lib/bluepill/trigger.rb +52 -25
- data/lib/bluepill/triggers/flapping.rb +8 -16
- data/lib/bluepill/units.rb +126 -0
- data/lib/bluepill/util/inflections.rb +34 -0
- data/lib/bluepill/util/levenshtein.rb +53 -0
- data/lib/bluepill/util/rotational_array.rb +1 -1
- data/lib/bluepill/version.rb +13 -12
- data/lib/bluepill.rb +27 -35
- data/sig/bluepill/application.rbs +257 -0
- data/sig/bluepill/checks.rbs +203 -0
- data/sig/bluepill/dsl.rbs +126 -0
- data/sig/bluepill/logger.rbs +104 -0
- data/sig/bluepill/process.rbs +542 -0
- data/sig/bluepill/system.rbs +316 -0
- data/sig/bluepill.rbs +47 -994
- metadata +39 -49
- data/lib/bluepill/clock.rb +0 -48
- data/lib/bluepill/deprecation.rb +0 -74
- data/lib/bluepill/trigger/custom.rb +0 -147
- data/lib/bluepill/util/log_error.rb +0 -29
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 479806cb2fec4bce8b5dda9efcc35f2fd155aa3520d25916b3fa8ce3ee8b9670
|
|
4
|
+
data.tar.gz: 4a7681b538732c743423ca2f8921c097950bddd313b56f92c97ecff9e7b4435a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e1fc693888a977f958df3f854d9a19c38ee87ad36832329afdd58489d268d9749e57c74af7566138ef8edb4f3b29618d261757c87ae15fca4039baa6191ab30f
|
|
7
|
+
data.tar.gz: b186296ac77abe6a1c8fdc5f2bdfba9d4e598f576b77956283be641150df041c7b939075c9c78615445c23f8b90848f1b1d747668b8ac5563eb48ae630f2238d
|
data/CHANGELOG.md
CHANGED
|
@@ -7,34 +7,53 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
## [0.
|
|
10
|
+
## [0.2.0] - 2026-11-02
|
|
11
|
+
|
|
12
|
+
### Upgrading from 0.1
|
|
13
|
+
|
|
14
|
+
Most pill files work with 0.2.0 unchanged. Check these before you upgrade:
|
|
15
|
+
|
|
16
|
+
- **Bluepill needs Ruby 2.0 or newer.** On Ruby 1.9.3 or JRuby 1.7, stay on 0.1.6, which has every fix that doesn't change behavior, or upgrade Ruby first.
|
|
17
|
+
- **A setting bluepill doesn't know now fails the config.** 0.1 quietly ignored a misspelled or unsupported setting in a `process` block, such as `start_comand`. Now `bluepill load` stops with the setting's name and the closest one it knows. The config fails before bluepill touches anything, so a running daemon and its processes keep going, and loading the new config is a safe way to find out.
|
|
18
|
+
- **Bluepill no longer loads ActiveSupport for you.** `10.seconds`, `2.minutes`, and `100.megabytes` still work, as plain numbers of seconds and bytes. A pill file that uses anything else from ActiveSupport, such as `5.minutes.ago`, `blank?`, or `present?`, needs the `activesupport` gem and a `require "active_support/all"` (or just the parts it uses) at the top.
|
|
19
|
+
- **A process with a `uid` gets that user's groups.** It now runs with every group the user belongs to, as after logging in. Without a `gid`, it used to keep root's groups, and now it gets the user's primary group instead. Set `gid` or `supplementary_groups` for a process that needs a group the user isn't in.
|
|
20
|
+
- **With `monitor_children`, the children's commands run as the parent's user.** A child's `stop_command` used to run as root. One that needs root has to get it some other way, such as `sudo`.
|
|
21
|
+
- **`pre_start_command` has a time limit.** It now runs like the other hooks, so bluepill kills it after `hook_timeout` (30 seconds by default) and starts the process anyway. Raise `hook_timeout` for a setup step that takes longer, such as a migration. A `pre_start_command` that can't run, such as a missing program, is now logged, and no longer stops the start.
|
|
22
|
+
- **A process's `state` is a Symbol.** A custom trigger or check that compares `process.state` with a String, such as `"up"`, needs to compare it with a Symbol, such as `:up`. What `bluepill status`, the log and `notify_command` show is unchanged.
|
|
23
|
+
- **The daemon only answers the CLI's commands.** A script that talks to the daemon's socket itself can still send `start`, `stop`, `restart`, `unmonitor`, `status`, `version` and `log_file`, but any other line now gets a `NoMethodError` back instead of calling that method on the daemon.
|
|
24
|
+
- **A custom trigger's `notify` gets a smaller transition.** It now has `from_name`, `to_name`, and `loopback?`, which is all bluepill's own triggers use. The `from`, `to`, and `event` that state_machine's transitions also had are gone.
|
|
25
|
+
|
|
26
|
+
To upgrade, install 0.2.0 and run `bluepill load` with the same pill file. It replaces the running 0.1 daemon and keeps monitoring the processes that are already running, without restarting them. Quitting the 0.1 daemon first works too, since `bluepill quit` leaves the processes running in both versions. Since 0.1.5, each app's journals live in `journals/<app>`, and 0.2.0 still reads and clears the ones that 0.1.4 and earlier wrote. If you go back to 0.1.4 or earlier, it won't see the journals that 0.1.5 and 0.2.0 write.
|
|
11
27
|
|
|
12
28
|
### Added
|
|
13
29
|
|
|
14
|
-
-
|
|
30
|
+
- Custom checks have a `logger`, which writes to bluepill's log with the app's and the process's names, and the README shows how to write one ([#7](https://github.com/bluepill-rb/bluepill/issues/7)).
|
|
31
|
+
- A `log_level` option for `Bluepill.application`, which drops messages less severe than it, such as `log_level: :warning` ([#47](https://github.com/bluepill-rb/bluepill/issues/47)).
|
|
32
|
+
- `supplementary_groups` can be set for the whole app, like `uid` and `gid` ([#28](https://github.com/bluepill-rb/bluepill/issues/28)).
|
|
33
|
+
- `post_start_command`, `notify_command`, `pre_stop_command`, and `post_stop_command` hooks. A hook that runs longer than `hook_timeout` is killed, with anything it started. `notify_command` gets the app, group, process, old and new state, reason, and pid in environment variables, and an app can set it for all its processes ([#60](https://github.com/bluepill-rb/bluepill/issues/60)).
|
|
34
|
+
- Documented the durations and sizes, such as `10.seconds` and `100.megabytes`, that bluepill adds to `Numeric`, and a spec that fails if bluepill changes any other of Ruby's own classes.
|
|
15
35
|
|
|
16
|
-
###
|
|
36
|
+
### Changed
|
|
17
37
|
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
38
|
+
- **Breaking:** an attribute in a `process` block that bluepill doesn't know, such as a misspelled `start_comand`, now fails when the config loads, naming the attribute and the process and suggesting the closest one. Before, bluepill accepted it and quietly ignored it ([#40](https://github.com/bluepill-rb/bluepill/issues/40)).
|
|
39
|
+
- **Breaking:** a process's `state` is now a Symbol, such as `:up`, where it was a String, such as `"up"`. A custom trigger that compares `process.state` with a String needs to compare it with a Symbol instead. `bluepill status`, the log, and `notify_command`'s `BLUEPILL_FROM` and `BLUEPILL_TO` show the states as before, and a tick that leaves a process's state alone no longer makes a new transition and string to do it.
|
|
40
|
+
- **Breaking:** `pre_start_command` now runs like the other hooks. Bluepill waits for it for up to `hook_timeout` (30 seconds by default), kills it if it runs longer, and logs it if it fails to run, then starts the process either way. Before, it had no time limit, and one that couldn't run, such as a missing program, stopped the start. A setup step that takes longer needs a bigger `hook_timeout`.
|
|
41
|
+
- **Breaking:** a process with a `uid` now runs with every group the user belongs to, as it would after logging in, and with the user's primary group when it sets no `gid`. Before, it got only its `gid` and `supplementary_groups`, and without a `gid` it kept the root group and root's other groups ([#28](https://github.com/bluepill-rb/bluepill/issues/28)).
|
|
42
|
+
- **Breaking:** the commands of a process's children with `monitor_children`, such as their `stop_command`, now run as the parent's `uid`, `gid` and `supplementary_groups`. Before, they ran as root ([#28](https://github.com/bluepill-rb/bluepill/issues/28)).
|
|
43
|
+
- A check's method that says whether a value is fine is now `check?`. A custom check that defines `check`, as in bluepill 0.1, still works.
|
|
44
|
+
- Each tick does much less work. Compared with 0.1.4 on the benchmarks in `duel/`, a tick of a process that's up takes 94% fewer instructions and makes 2 objects instead of 166, finding a process's children among 300 others takes over 99% fewer, checking the memory of a process and its workers 98% fewer, and parsing `ps` about a third fewer.
|
|
45
|
+
- A process's logger works out its prefixes once, and writes straight to the log, so a log line takes 1 allocation instead of 10, and each check's run takes about 10% less time.
|
|
26
46
|
|
|
27
|
-
###
|
|
47
|
+
### Removed
|
|
28
48
|
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
- The
|
|
49
|
+
- **Breaking:** support for Ruby 1.9.3 and JRuby 1.7. Bluepill 0.2 needs Ruby 2.0 or newer, or JRuby 9.0 or newer. Bluepill 0.1.6 still runs on both.
|
|
50
|
+
- The `state_machine` dependency, whose last release was in 2013. A process's states and events are now a small transition table in `Bluepill::Process`, which moves between the same states, in the same order of callbacks. With all three dependencies gone, bluepill has no runtime dependencies. This isn't a breaking change ([#61](https://github.com/bluepill-rb/bluepill/issues/61)).
|
|
51
|
+
- The `activesupport` dependency, which loaded about 300 files into the daemon for six small methods. `10.seconds`, `2.minutes`, `100.megabytes` and the rest of the durations and sizes that configs use now come from bluepill, as plain numbers of seconds and bytes. Bluepill only adds them where they aren't defined already, so a config that loads ActiveSupport keeps its versions, which bluepill still accepts. This isn't a breaking change ([#61](https://github.com/bluepill-rb/bluepill/issues/61)).
|
|
52
|
+
- The `blue-daemons` dependency. Bluepill now has its own copy of the few functions it used from it, as `Bluepill::Daemonize`, so an app that also uses the `daemons` gem no longer gets whichever of the two loads first. This isn't a breaking change ([#61](https://github.com/bluepill-rb/bluepill/issues/61)).
|
|
33
53
|
|
|
34
|
-
###
|
|
54
|
+
### Fixed
|
|
35
55
|
|
|
36
|
-
-
|
|
37
|
-
- 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.
|
|
56
|
+
- 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.
|
|
38
57
|
|
|
39
58
|
## [0.1.6] - 2026-11-02
|
|
40
59
|
|
|
@@ -793,8 +812,8 @@ Initial release.
|
|
|
793
812
|
- Added the `bluepill` CLI with the `load`, `status`, `start`, `stop`, `restart`, `unmonitor`, `quit` and `log` commands.
|
|
794
813
|
- Added syslog logging.
|
|
795
814
|
|
|
796
|
-
[unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.
|
|
797
|
-
[0.
|
|
815
|
+
[unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.2.0...HEAD
|
|
816
|
+
[0.2.0]: https://github.com/bluepill-rb/bluepill/compare/v0.1.6...v0.2.0
|
|
798
817
|
[0.1.6]: https://github.com/bluepill-rb/bluepill/compare/v0.1.5...v0.1.6
|
|
799
818
|
[0.1.5]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...v0.1.5
|
|
800
819
|
[0.1.4]: https://github.com/bluepill-rb/bluepill/compare/v0.1.3...v0.1.4
|
data/CONTRIBUTING.md
CHANGED
|
@@ -8,11 +8,11 @@ As more OS options are added, this should also simplify the steps necessary to r
|
|
|
8
8
|
Type checking
|
|
9
9
|
-------------
|
|
10
10
|
|
|
11
|
-
The RBS signatures in `sig
|
|
11
|
+
The RBS signatures in `sig/` ship with the gem, and CI checks `lib` against them with [Steep](https://github.com/soutaro/steep) in strict mode. Steep needs Ruby 3.3 or newer. To run the check locally:
|
|
12
12
|
|
|
13
13
|
bundle exec rake steep
|
|
14
14
|
|
|
15
|
-
This installs the signatures of the dependencies into `.gem_rbs_collection`, validates the signatures and runs `steep check`. `sig-vendor` has stubs for the dependencies that publish no signatures. Update `sig/bluepill.rbs`
|
|
15
|
+
This installs the signatures of the dependencies into `.gem_rbs_collection`, validates the signatures and runs `steep check`. `sig-vendor` has stubs for the dependencies that publish no signatures. Update the signatures when you change a method in `lib`: `sig/bluepill.rbs` has the shared types, and `sig/bluepill/` has a file for each part of the gem.
|
|
16
16
|
|
|
17
17
|
Mutation testing
|
|
18
18
|
----------------
|
data/README.md
CHANGED
|
@@ -25,6 +25,8 @@ In order to take advantage of logging with syslog, you also need to setup your s
|
|
|
25
25
|
|
|
26
26
|
local6.* /var/log/bluepill.log
|
|
27
27
|
|
|
28
|
+
Since Ruby 3.4, syslog is a separate gem, so install it too (`sudo gem install syslog`), or have bluepill write to a log file instead (see [Logging](#logging)).
|
|
29
|
+
|
|
28
30
|
You'll also want to add _/var/log/bluepill.log_ to _/etc/logrotate.d/syslog_ so that it gets rotated.
|
|
29
31
|
|
|
30
32
|
Lastly, create the _/var/run/bluepill_ directory for bluepill to store its pid and sock files.
|
|
@@ -58,6 +60,12 @@ end
|
|
|
58
60
|
|
|
59
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.
|
|
60
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:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
Unknown attribute 'start_comand' for process 'process_name'. Did you mean 'start_command'?
|
|
67
|
+
```
|
|
68
|
+
|
|
61
69
|
Now if we want to do something more meaningful, like actually monitor the process, we do:
|
|
62
70
|
|
|
63
71
|
```ruby
|
|
@@ -111,20 +119,6 @@ Bluepill.application("app_name") do |app|
|
|
|
111
119
|
end
|
|
112
120
|
```
|
|
113
121
|
|
|
114
|
-
To restart a process when a URL stops answering:
|
|
115
|
-
|
|
116
|
-
```ruby
|
|
117
|
-
Bluepill.application("app_name") do |app|
|
|
118
|
-
app.process("process_name") do |process|
|
|
119
|
-
process.start_command = "/usr/bin/some_start_command"
|
|
120
|
-
process.pid_file = "/tmp/some_pid_file.pid"
|
|
121
|
-
process.checks :http, every: 30.seconds, url: "https://localhost:4443/status", kind: :success, timeout: 5.seconds, verify_ssl: true
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
Bluepill 0.1 doesn't verify an https URL's certificate unless the check sets `verify_ssl: true`. Bluepill 0.3.0 verifies it by default, so set `verify_ssl: true` now, or `verify_ssl: false` for a server with a self-signed certificate.
|
|
127
|
-
|
|
128
122
|
We can tell bluepill to give a process some grace time to start/stop/restart before resuming monitoring:
|
|
129
123
|
|
|
130
124
|
```ruby
|
|
@@ -170,7 +164,7 @@ Bluepill.application("app_name") do |app|
|
|
|
170
164
|
end
|
|
171
165
|
```
|
|
172
166
|
|
|
173
|
-
|
|
167
|
+
A process with a `uid` gets every group that the user belongs to, the same as logging in as the user, and the user's primary group unless `gid` names another. To add more groups to those:
|
|
174
168
|
|
|
175
169
|
```ruby
|
|
176
170
|
Bluepill.application("app_name") do |app|
|
|
@@ -186,12 +180,13 @@ Bluepill.application("app_name") do |app|
|
|
|
186
180
|
end
|
|
187
181
|
```
|
|
188
182
|
|
|
189
|
-
You can also set an app-wide uid
|
|
183
|
+
You can also set an app-wide uid, gid and supplementary groups:
|
|
190
184
|
|
|
191
185
|
```ruby
|
|
192
186
|
Bluepill.application("app_name") do |app|
|
|
193
187
|
app.uid = "deploy"
|
|
194
188
|
app.gid = "deploy"
|
|
189
|
+
app.supplementary_groups = ['rvm']
|
|
195
190
|
app.process("process_name") do |process|
|
|
196
191
|
process.start_command = "/usr/bin/some_start_command"
|
|
197
192
|
process.pid_file = "/tmp/some_pid_file.pid"
|
|
@@ -199,6 +194,8 @@ Bluepill.application("app_name") do |app|
|
|
|
199
194
|
end
|
|
200
195
|
```
|
|
201
196
|
|
|
197
|
+
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
|
+
|
|
202
199
|
To track resources of child processes, use `:include_children`:
|
|
203
200
|
```ruby
|
|
204
201
|
Bluepill.application("app_name") do |app|
|
|
@@ -254,7 +251,39 @@ Bluepill.application("app_name") do |app|
|
|
|
254
251
|
end
|
|
255
252
|
```
|
|
256
253
|
|
|
257
|
-
Bluepill waits for the `pre_start_command` to finish, and logs a
|
|
254
|
+
Bluepill waits for the `pre_start_command` to finish, for up to `hook_timeout` (30 seconds by default), and then starts the process either way. It logs a `pre_start_command` that exits with a non-zero status, fails to run, or runs too long, and kills one that runs too long, with anything it started. It runs in the same `working_dir`, with the same `environment`, `uid`, and `gid`, as the process.
|
|
255
|
+
|
|
256
|
+
Bluepill has four more hooks, named after systemd's `ExecStartPost`, `ExecStop`, and `ExecStopPost`:
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
Bluepill.application("app_name") do |app|
|
|
260
|
+
# Runs on every change of state, for every process in the app
|
|
261
|
+
app.notify_command = "/usr/local/bin/alert-on-call"
|
|
262
|
+
|
|
263
|
+
app.process("process_name") do |process|
|
|
264
|
+
process.start_command = "/usr/bin/some_start_command"
|
|
265
|
+
process.pid_file = "/tmp/some_pid_file.pid"
|
|
266
|
+
|
|
267
|
+
# Runs when the process comes up after a start or a restart
|
|
268
|
+
process.post_start_command = "renice 20 {{PID}}"
|
|
269
|
+
# Runs before the stop, which waits for it
|
|
270
|
+
process.pre_stop_command = "/usr/bin/leave-load-balancer"
|
|
271
|
+
# Runs after the process has stopped and bluepill has cleaned up
|
|
272
|
+
process.post_stop_command = "/usr/bin/remove-socket {{PID}}"
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
- `post_start_command` runs once the process reaches `up` after a start or a restart, not as soon as `start_command` returns, so a server that daemonizes itself has written its pid file by then.
|
|
278
|
+
- `notify_command` runs on every change of state, such as `up` to `down`, or `down` to `starting`. It gets the details in environment variables: `BLUEPILL_APP`, `BLUEPILL_GROUP`, `BLUEPILL_PROCESS`, `BLUEPILL_FROM` and `BLUEPILL_TO` (the old and new state), `BLUEPILL_REASON` (such as the check that failed, `Flapping`, or `user initiated`), and `BLUEPILL_PID`. Use it to send an alert when bluepill restarts a process or gives up on one. Set it on the app for every process, or on one process.
|
|
279
|
+
- `pre_stop_command` runs before the stop signals or `stop_command`, for example to take a server out of a load balancer or to drain a queue. The stop waits for it to finish, and then goes ahead either way.
|
|
280
|
+
- `post_stop_command` runs after the process has stopped and bluepill has cleaned up, for example to remove a socket or a lock file.
|
|
281
|
+
|
|
282
|
+
`{{PID}}` in a hook is replaced with the process's pid, or, in `post_stop_command`, with the pid of the process that just stopped. Like `pre_start_command`, the hooks run in the process's `working_dir`, with its `environment`, `uid`, and `gid`, and without a shell. `post_start_command`, `notify_command`, and `post_stop_command` run in the background, so a slow hook never holds up monitoring. A hook that runs longer than `hook_timeout` (30 seconds by default) is killed, with anything it started. Bluepill logs a hook that fails or times out, but a hook never changes the process's state or stops bluepill from starting or stopping it:
|
|
283
|
+
|
|
284
|
+
```ruby
|
|
285
|
+
process.hook_timeout = 10.seconds
|
|
286
|
+
```
|
|
258
287
|
|
|
259
288
|
By default, bluepill will send a SIGTERM to your process when stopping.
|
|
260
289
|
To change the stop command:
|
|
@@ -300,9 +329,52 @@ end
|
|
|
300
329
|
|
|
301
330
|
Note `{{PID}}` will be substituted for the pid of process in both the stop and restart commands.
|
|
302
331
|
|
|
332
|
+
### Durations and sizes
|
|
333
|
+
|
|
334
|
+
`10.seconds` and `100.megabytes` are plain numbers of seconds and bytes, so `every: 10.seconds` and `every: 10` mean the same thing. To make them work, bluepill adds `seconds`, `minutes`, `hours`, `days`, `kilobytes`, `megabytes`, and `gigabytes`, and their singular forms such as `1.minute`, to Ruby's `Numeric`. It's the only change bluepill makes to Ruby's own classes, and it applies to all the code that a pill file loads, not just the pill file.
|
|
335
|
+
|
|
336
|
+
If a pill file loads ActiveSupport, for example by loading a Rails app, ActiveSupport's versions of these methods win, whichever loads first. Bluepill accepts ActiveSupport's durations too.
|
|
337
|
+
|
|
338
|
+
### Custom checks
|
|
339
|
+
|
|
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, except `every`, `times`, `fires`, and `include_children`, which bluepill handles. `run` reads a value, and `check?` returns whether the value is fine:
|
|
341
|
+
|
|
342
|
+
```ruby
|
|
343
|
+
module Bluepill
|
|
344
|
+
module ProcessConditions
|
|
345
|
+
class LogSize < ProcessCondition
|
|
346
|
+
def initialize(options = {})
|
|
347
|
+
super
|
|
348
|
+
@path = options.fetch(:path)
|
|
349
|
+
@below = options.fetch(:below)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def run(_pid, _include_children)
|
|
353
|
+
File.size?(@path).to_i
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def check?(size)
|
|
357
|
+
logger.warning("#{@path} is #{size} bytes") unless size < @below
|
|
358
|
+
size < @below
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
Bluepill.application("app_name") do |app|
|
|
365
|
+
app.process("process_name") do |process|
|
|
366
|
+
process.start_command = "/usr/bin/some_start_command"
|
|
367
|
+
process.pid_file = "/tmp/some_pid_file.pid"
|
|
368
|
+
process.checks :log_size, every: 1.minute, path: "/var/log/app.log", below: 1.gigabyte
|
|
369
|
+
end
|
|
370
|
+
end
|
|
371
|
+
```
|
|
372
|
+
|
|
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`. A check written for bluepill 0.1, which defines `check` instead of `check?`, still works.
|
|
374
|
+
|
|
303
375
|
### Commands Don't Run in a Shell
|
|
304
376
|
|
|
305
|
-
Bluepill splits each command (`start_command`, `
|
|
377
|
+
Bluepill splits each command (`start_command`, `stop_command`, `restart_command`, and the hooks, such as `pre_start_command`) into words and runs it directly, without a shell. So shell syntax like `cd ... &&`, `VAR=value`, `|`, `>`, `2>&1`, and `*` globs doesn't work.
|
|
306
378
|
|
|
307
379
|
Use the config options instead:
|
|
308
380
|
|
|
@@ -395,6 +467,16 @@ end
|
|
|
395
467
|
|
|
396
468
|
Keep in mind that you still need to set up log rotation (described in the installation section) to keep the log file from growing huge.
|
|
397
469
|
|
|
470
|
+
Bluepill logs everything by default, including a line for every check it runs. To log less, set the :log\_level option to the least severe priority you want to keep. Bluepill drops anything less severe, whether it logs to syslog, a log file, or stdout in foreground mode:
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
Bluepill.application("app_name", log_level: :warning) do |app|
|
|
474
|
+
# ...
|
|
475
|
+
end
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
The levels, from most to least severe, are `emerg`, `alert`, `crit`, `err`, `warning`, `notice`, `info`, and `debug`, the default. Bluepill logs each check's history at `info`, and process state changes at `info` and `warning`.
|
|
479
|
+
|
|
398
480
|
### Foreground mode
|
|
399
481
|
|
|
400
482
|
You can run bluepill in the foreground:
|
|
@@ -422,11 +504,9 @@ JRuby allows you to run bluepill only in the foreground.
|
|
|
422
504
|
This library aims to support and is [tested against][ci] the following Ruby
|
|
423
505
|
implementations:
|
|
424
506
|
|
|
425
|
-
* Ruby 1.9.3
|
|
426
507
|
* Ruby 2.0 through 2.7
|
|
427
508
|
* Ruby 3.0 through 3.4
|
|
428
509
|
* Ruby 4.0
|
|
429
|
-
* JRuby 1.7 (only in the foreground)
|
|
430
510
|
* JRuby 9.0 through 9.4 (only in the foreground)
|
|
431
511
|
* JRuby 10.0 and 10.1 (only in the foreground)
|
|
432
512
|
|
data/bin/bluepill
CHANGED
|
@@ -1,54 +1,48 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
|
|
5
|
-
begin
|
|
6
|
-
require 'rbconfig'
|
|
7
|
-
rescue LoadError
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
RbConfig = Config unless Object.const_defined?(:RbConfig)
|
|
2
|
+
require "optparse"
|
|
3
|
+
require "rbconfig"
|
|
4
|
+
require "bluepill"
|
|
11
5
|
|
|
12
6
|
# Default options
|
|
13
7
|
options = {
|
|
14
|
-
log_file:
|
|
15
|
-
base_dir: ENV[
|
|
8
|
+
log_file: "/var/log/bluepill.log",
|
|
9
|
+
base_dir: ENV["BLUEPILL_BASE_DIR"] || (::Process.euid.zero? ? "/var/run/bluepill" : File.join(ENV.fetch("HOME", nil), ".bluepill")),
|
|
16
10
|
privileged: true,
|
|
17
11
|
timeout: 10,
|
|
18
|
-
attempts: 1
|
|
12
|
+
attempts: 1
|
|
19
13
|
}
|
|
20
14
|
|
|
21
15
|
OptionParser.new do |opts|
|
|
22
|
-
opts.banner =
|
|
23
|
-
opts.on(
|
|
16
|
+
opts.banner = "Usage: bluepill [app] cmd [options]"
|
|
17
|
+
opts.on("-l", "--logfile LOGFILE", "Path to logfile, defaults to #{options[:log_file]}") do |file|
|
|
24
18
|
options[:log_file] = file
|
|
25
19
|
end
|
|
26
20
|
|
|
27
|
-
opts.on(
|
|
21
|
+
opts.on("-c", "--base-dir DIR", "Directory to store bluepill socket and pid files, defaults to #{options[:base_dir]}") do |base_dir|
|
|
28
22
|
options[:base_dir] = base_dir
|
|
29
23
|
end
|
|
30
24
|
|
|
31
|
-
opts.on(
|
|
25
|
+
opts.on("-v", "--version") do
|
|
32
26
|
puts "bluepill, version #{Bluepill::Version}"
|
|
33
27
|
exit
|
|
34
28
|
end
|
|
35
29
|
|
|
36
|
-
opts.on(
|
|
30
|
+
opts.on("--[no-]privileged", "Allow/disallow to run #{$PROGRAM_NAME} as non-privileged process. disallowed by default") do |v|
|
|
37
31
|
options[:privileged] = v
|
|
38
32
|
end
|
|
39
33
|
|
|
40
|
-
opts.on(
|
|
34
|
+
opts.on("-t", "--timeout Seconds", Integer, "Timeout for commands sent to the daemon, in seconds. Defaults to 10.") do |timeout|
|
|
41
35
|
options[:timeout] = timeout
|
|
42
36
|
end
|
|
43
37
|
|
|
44
|
-
opts.on(
|
|
38
|
+
opts.on("--attempts Count", Integer, "Attempts for commands sent to the daemon, in seconds. Defaults to 1.") do |attempts|
|
|
45
39
|
options[:attempts] = attempts
|
|
46
40
|
end
|
|
47
41
|
|
|
48
42
|
help = proc do
|
|
49
43
|
puts opts
|
|
50
44
|
puts
|
|
51
|
-
puts
|
|
45
|
+
puts "Commands:"
|
|
52
46
|
puts " load CONFIG_FILE\t\tLoads new instance of bluepill using the specified config file"
|
|
53
47
|
puts " status\t\t\tLists the status of the proceses for the specified app"
|
|
54
48
|
puts " start [TARGET]\t\tIssues the start command for the target process or group, defaults to all processes"
|
|
@@ -58,23 +52,24 @@ OptionParser.new do |opts|
|
|
|
58
52
|
puts " log [TARGET]\t\tShow the log for the specified process or group, defaults to all for app"
|
|
59
53
|
puts " quit\t\t\tStop bluepill"
|
|
60
54
|
puts
|
|
61
|
-
puts
|
|
55
|
+
puts "See http://github.com/bluepill-rb/bluepill#readme"
|
|
62
56
|
exit
|
|
63
57
|
end
|
|
64
58
|
|
|
65
|
-
opts.on_tail(
|
|
59
|
+
opts.on_tail("-h", "--help", "Show this message", &help)
|
|
66
60
|
help.call if ARGV.empty?
|
|
67
61
|
end.parse!
|
|
68
62
|
|
|
69
63
|
# Check for root
|
|
70
64
|
if options[:privileged] && ::Process.euid != 0
|
|
71
|
-
$stderr.puts
|
|
65
|
+
$stderr.puts "You must run bluepill as root or use --no-privileged option."
|
|
72
66
|
exit(3)
|
|
73
67
|
end
|
|
74
68
|
|
|
75
69
|
APPLICATION_COMMANDS = %w[status start stop restart unmonitor quit log].freeze
|
|
76
70
|
|
|
77
|
-
|
|
71
|
+
# Hash#slice needs Ruby 2.5
|
|
72
|
+
controller = Bluepill::Controller.new(options.select { |key, _| %i[base_dir log_file].include?(key) })
|
|
78
73
|
|
|
79
74
|
if controller.running_applications.include?(File.basename($PROGRAM_NAME)) && File.symlink?($PROGRAM_NAME)
|
|
80
75
|
# bluepill was called as a symlink with the name of the target application
|
|
@@ -92,7 +87,7 @@ elsif APPLICATION_COMMANDS.include?(ARGV.first)
|
|
|
92
87
|
controller.running_applications.each_with_index do |app, index|
|
|
93
88
|
$stderr.puts " #{index + 1}. #{app}"
|
|
94
89
|
end
|
|
95
|
-
$stderr.puts
|
|
90
|
+
$stderr.puts "Usage: bluepill [app] cmd [options]"
|
|
96
91
|
exit(1)
|
|
97
92
|
else
|
|
98
93
|
# There are none running AND they aren't trying to start one
|
|
@@ -103,17 +98,17 @@ end
|
|
|
103
98
|
|
|
104
99
|
options[:command] = ARGV.shift
|
|
105
100
|
|
|
106
|
-
if options[:command] ==
|
|
101
|
+
if options[:command] == "load"
|
|
107
102
|
file = ARGV.shift
|
|
108
103
|
if File.exist?(file)
|
|
109
104
|
# Restart the ruby interpreter for the config file so that anything loaded here
|
|
110
105
|
# does not stay in memory for the daemon
|
|
111
|
-
ruby = File.join(RbConfig::CONFIG[
|
|
112
|
-
load_path = File.expand_path("
|
|
106
|
+
ruby = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])
|
|
107
|
+
load_path = File.expand_path("../lib", __dir__)
|
|
113
108
|
file_path = File.expand_path(file)
|
|
114
|
-
ENV[
|
|
109
|
+
ENV["BLUEPILL_BASE_DIR"] = options[:base_dir]
|
|
115
110
|
|
|
116
|
-
exec(ruby, "-I#{load_path}",
|
|
111
|
+
exec(ruby, "-I#{load_path}", "-rbluepill", file_path)
|
|
117
112
|
|
|
118
113
|
else
|
|
119
114
|
$stderr.puts "Can't find file: #{file}"
|
data/bluepill.gemspec
CHANGED
|
@@ -1,37 +1,30 @@
|
|
|
1
|
-
lib = File.expand_path(
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
-
require
|
|
3
|
+
require "bluepill/version"
|
|
4
4
|
|
|
5
5
|
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name
|
|
7
|
-
spec.version
|
|
8
|
-
spec.platform
|
|
9
|
-
spec.authors
|
|
10
|
-
spec.email
|
|
11
|
-
spec.homepage
|
|
12
|
-
spec.summary
|
|
6
|
+
spec.name = "bluepill"
|
|
7
|
+
spec.version = Bluepill::Version
|
|
8
|
+
spec.platform = Gem::Platform::RUBY
|
|
9
|
+
spec.authors = ["Arya Asemanfar", "Erik Berlin", "Rohith Ravi", "Gary Tsang"]
|
|
10
|
+
spec.email = ["entombedvirus@gmail.com"]
|
|
11
|
+
spec.homepage = "https://github.com/bluepill-rb/bluepill"
|
|
12
|
+
spec.summary = "A process monitor written in Ruby with stability and minimalism in mind."
|
|
13
13
|
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."
|
|
14
|
-
spec.license
|
|
14
|
+
spec.license = "MIT"
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
'rubygems_mfa_required' => 'true',
|
|
23
|
-
}
|
|
24
|
-
end
|
|
16
|
+
spec.metadata = {
|
|
17
|
+
"source_code_uri" => "https://github.com/bluepill-rb/bluepill",
|
|
18
|
+
"changelog_uri" => "https://github.com/bluepill-rb/bluepill/blob/master/CHANGELOG.md",
|
|
19
|
+
"bug_tracker_uri" => "https://github.com/bluepill-rb/bluepill/issues",
|
|
20
|
+
"rubygems_mfa_required" => "true"
|
|
21
|
+
}
|
|
25
22
|
|
|
26
|
-
spec.
|
|
27
|
-
spec.add_dependency 'blue-daemons', '~> 1.1'
|
|
28
|
-
spec.add_dependency 'state_machine', '~> 1.1'
|
|
29
|
-
|
|
30
|
-
spec.required_ruby_version = '>= 1.9.3'
|
|
23
|
+
spec.required_ruby_version = ">= 2.0"
|
|
31
24
|
|
|
32
25
|
# sig has the RBS signatures, and the standard libraries they use in manifest.yaml
|
|
33
|
-
spec.executables
|
|
34
|
-
spec.files
|
|
35
|
-
spec.executables.collect { |f| "bin/#{f}" } + Dir[
|
|
36
|
-
spec.require_paths
|
|
26
|
+
spec.executables = %w[bluepill bpsv]
|
|
27
|
+
spec.files = %w[CHANGELOG.md CONTRIBUTING.md DESIGN.md LICENSE README.md bluepill.gemspec sig/manifest.yaml] +
|
|
28
|
+
spec.executables.collect { |f| "bin/#{f}" } + Dir["lib/**/*.rb"] + Dir["sig/**/*.rbs"]
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
37
30
|
end
|