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
@@ -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,28 @@ 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(&) = @mutex.synchronize(&)
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,
172
+ ["Failed to start bluepill:", format("%s `%s`", e.class.name, e.message), e.backtrace].join("\n")
166
173
  end
167
174
 
168
175
  # @!method start(group_name = nil, process_name = nil)
@@ -250,12 +257,10 @@ module Bluepill
250
257
  # It's a String, since Marshal sends a module as only its name, which
251
258
  # the CLI would load as its own Version.
252
259
  #
253
- # @return [String] the version, such as "0.2.9"
260
+ # @return [String] the version, such as "0.3.0"
254
261
  #
255
262
  # @api private
256
- def version
257
- Version.to_s
258
- end
263
+ def version = Version.to_s
259
264
 
260
265
  protected
261
266
 
@@ -267,7 +272,8 @@ module Bluepill
267
272
  #
268
273
  # @api private
269
274
  def log_crash(error)
270
- [format("Bluepill stopped: %s `%s`", error.class, error.message), *error.backtrace].each { |line| logger.crit(line) }
275
+ lines = [format("Bluepill stopped: %s `%s`", error.class, error.message), *error.backtrace]
276
+ lines.each { |line| logger.crit(line) }
271
277
  end
272
278
 
273
279
  # Send a command to a process, a group, or every process
@@ -4,8 +4,9 @@ module Bluepill
4
4
  # The paths in bluepill's base directory
5
5
  #
6
6
  # The daemons' sockets are in socks, and their pid files are in pids,
7
- # next to a directory for each application's processes. The CLI and the
8
- # daemon both find them here.
7
+ # next to a directory for each application's processes. The journals of
8
+ # the processes bluepill starts are in journals. The CLI and the daemon
9
+ # both find them here.
9
10
  #
10
11
  # @api private
11
12
  class BaseDirectory
@@ -28,27 +29,30 @@ module Bluepill
28
29
  # @return [void]
29
30
  #
30
31
  # @api private
31
- def initialize(path)
32
- @path = path
33
- end
32
+ def initialize(path) = @path = path
34
33
 
35
34
  # Return the directory with the daemons' sockets
36
35
  #
37
36
  # @return [String]
38
37
  #
39
38
  # @api private
40
- def sockets_dir
41
- File.join(path, "socks")
42
- end
39
+ def sockets_dir = File.join(path, "socks")
43
40
 
44
41
  # Return the directory with the daemons' pid files
45
42
  #
46
43
  # @return [String]
47
44
  #
48
45
  # @api private
49
- def pids_dir
50
- File.join(path, "pids")
51
- end
46
+ def pids_dir = File.join(path, "pids")
47
+
48
+ # Return the directory with the journals of the processes bluepill starts
49
+ #
50
+ # Each application's are in a directory of their own in it.
51
+ #
52
+ # @return [String]
53
+ #
54
+ # @api private
55
+ def journals_dir = File.join(path, "journals")
52
56
 
53
57
  # Return the socket that an application's daemon listens on
54
58
  #
@@ -57,9 +61,7 @@ module Bluepill
57
61
  # @return [String]
58
62
  #
59
63
  # @api private
60
- def socket_file(app)
61
- File.join(sockets_dir, "#{app}#{SOCKET_EXTENSION}")
62
- end
64
+ def socket_file(app) = File.join(sockets_dir, "#{app}#{SOCKET_EXTENSION}")
63
65
 
64
66
  # Return an application's daemon's pid file
65
67
  #
@@ -68,9 +70,7 @@ module Bluepill
68
70
  # @return [String]
69
71
  #
70
72
  # @api private
71
- def pid_file(app)
72
- File.join(pids_dir, "#{app}.pid")
73
- end
73
+ def pid_file(app) = File.join(pids_dir, "#{app}.pid")
74
74
 
75
75
  # Return the directory with the pid files of an application's processes
76
76
  #
@@ -79,9 +79,7 @@ module Bluepill
79
79
  # @return [String]
80
80
  #
81
81
  # @api private
82
- def process_pids_dir(app)
83
- File.join(pids_dir, app)
84
- end
82
+ def process_pids_dir(app) = File.join(pids_dir, app)
85
83
 
86
84
  # Return the names of the applications with a socket
87
85
  #
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "did_you_mean"
4
+
5
+ module Bluepill
6
+ # The checks and triggers a config can name, by their underscored class
7
+ # names
8
+ #
9
+ # Every subclass of ProcessConditions::ProcessCondition and of Trigger
10
+ # adds itself when it's defined, in whatever module, so `checks
11
+ # :log_size` finds a LogSize class anywhere. When two classes have the
12
+ # same name, the one defined last wins.
13
+ #
14
+ # @api private
15
+ module CheckRegistry
16
+ # Each class, newest first, with the class it builds on and the methods
17
+ # it must define
18
+ @entries = [] #: Array[[Class, Class, Array[Symbol]]]
19
+
20
+ # Add a check or trigger class
21
+ #
22
+ # @param [Class] klass the class
23
+ # @param [Class] base ProcessConditions::ProcessCondition or Trigger
24
+ # @param [Array<Symbol>] required the methods of the base that the class
25
+ # must define
26
+ #
27
+ # @return [void]
28
+ #
29
+ # @api private
30
+ def self.add(klass, base, required) = @entries.unshift([klass, base, required])
31
+
32
+ # Return the check or trigger class a config names
33
+ #
34
+ # @param [Symbol, String] name the underscored name, such as :mem_usage
35
+ #
36
+ # @return [Class] the ProcessConditions::ProcessCondition or Trigger
37
+ # subclass
38
+ #
39
+ # @raise [ConfigError] when no class has the name, or the class doesn't
40
+ # define the methods a check or trigger needs
41
+ #
42
+ # @api private
43
+ def self.fetch(name)
44
+ key = name.to_s
45
+ entry = @entries.detect { |klass, _, _| check_name(klass).eql?(key) }
46
+ raise ConfigError, unknown_message(key) unless entry
47
+
48
+ klass, base, required = entry
49
+ missing = missing_methods(klass, base, required)
50
+ return klass if missing.empty?
51
+
52
+ raise ConfigError, "#{klass}, the class of check '#{key}', must define #{missing.join(" and ")}"
53
+ end
54
+
55
+ # Return the names a config can use, for suggestions
56
+ #
57
+ # @return [Array<String>] the name of every class that defines the
58
+ # methods it needs
59
+ #
60
+ # @api private
61
+ def self.names
62
+ @entries.filter_map { |klass, base, required| check_name(klass) if missing_methods(klass, base, required).empty? }
63
+ end
64
+
65
+ # Return the name a config uses for a class
66
+ #
67
+ # @param [Class] klass the class
68
+ #
69
+ # @return [String, nil] its underscored name without its modules, such
70
+ # as "mem_usage", or nil for a class without a name
71
+ #
72
+ # @api private
73
+ def self.check_name(klass)
74
+ class_name = klass.name
75
+ Util.underscore(class_name.rpartition("::").last) if class_name
76
+ end
77
+
78
+ # Return the methods a class must define and doesn't
79
+ #
80
+ # @param [Class] klass the class
81
+ # @param [Class] base the class it builds on
82
+ # @param [Array<Symbol>] required the methods it must define
83
+ #
84
+ # @return [Array<Symbol>] the methods it leaves to its base
85
+ #
86
+ # @api private
87
+ def self.missing_methods(klass, base, required)
88
+ required.select { |method| klass.instance_method(method).owner.equal?(base) }
89
+ end
90
+
91
+ # Return the message for a name no class has, with a suggestion
92
+ #
93
+ # @param [String] name the name
94
+ #
95
+ # @return [String]
96
+ #
97
+ # @api private
98
+ def self.unknown_message(name)
99
+ suggestion = DidYouMean::SpellChecker.new(dictionary: names).correct(name).first
100
+ message = "Unknown check '#{name}'"
101
+ suggestion ? "#{message}. Did you mean '#{suggestion}'?" : message
102
+ end
103
+ end
104
+ end
@@ -18,8 +18,6 @@ module Bluepill
18
18
  # @return [Integer]
19
19
  #
20
20
  # @api private
21
- def self.now
22
- ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :second)
23
- end
21
+ def self.now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :second)
24
22
  end
25
23
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bluepill/check_registry"
3
4
  require "bluepill/util/log_error"
4
5
 
5
6
  module Bluepill
@@ -22,18 +23,17 @@ module Bluepill
22
23
  #
23
24
  # @api private
24
25
  #
25
- # @!attribute critical
26
- # Return whether the value was fine
26
+ # @!attribute passed
27
+ # Return whether the value passed the check
27
28
  #
28
- # It's true for a value that passed the check, and false for one that
29
- # counts toward the watch firing.
29
+ # It's false for a value that counts toward the watch firing.
30
30
  #
31
31
  # @return [Boolean] whether the value passed the check
32
32
  #
33
33
  # @api private
34
34
  #
35
35
  # @api private
36
- HistoryValue = Struct.new(:value, :critical)
36
+ HistoryValue = Struct.new(:value, :passed)
37
37
 
38
38
  # Return the logger the watch writes its history to
39
39
  #
@@ -56,77 +56,71 @@ module Bluepill
56
56
  # @api private
57
57
  EMPTY_ARRAY = no_events.freeze
58
58
 
59
- # The options the watch takes for itself, which the check doesn't get
60
- #
61
- # @api private
62
- WATCH_KEYS = %i[logger fires every times include_children].freeze
63
-
64
59
  # Create a watch of a check
65
60
  #
66
61
  # @param [Symbol] name the check's name, such as :mem_usage
67
- # @param [Hash{Symbol => Object}] options the check's options. The
68
- # watch takes :logger, :fires (the events to fire, [:restart] by
69
- # default), :every (seconds between runs), :times (how many of the
70
- # last values must fail, as a count or [count, of]), and
71
- # :include_children. The check gets the rest.
62
+ # @param [Logger, nil] logger the logger to write to
63
+ # @param [Symbol, Array<Symbol>, nil] fires the events to fire when the
64
+ # check fails
65
+ # @param [Integer, nil] every the seconds between runs
66
+ # @param [Integer, Array<Integer>] times how many of the last values must
67
+ # fail, as a count or as [count, of]
68
+ # @param [Boolean] include_children whether to count the process's
69
+ # children too
70
+ # @param [Hash{Symbol => Object}] condition_options the check's own
71
+ # options, such as below: 100.megabytes
72
72
  #
73
73
  # @return [void]
74
74
  #
75
+ # @raise [ConfigError] for an event a check can't fire
76
+ #
75
77
  # @api private
76
- def initialize(name, options = {})
78
+ def initialize(name, logger: nil, fires: :restart, every: nil, times: 1, include_children: false,
79
+ **condition_options)
77
80
  @name = name
78
- @logger = options[:logger]
79
- @fires = Array(options.fetch(:fires, :restart))
80
- @every = options[:every]
81
+ @logger = logger
82
+ @fires = events_to_fire(Array(fires))
83
+ @every = every
81
84
  # times: 5 means 5 of the last 5, and times: [3, 5] 3 of the last 5
82
- times = Array(options[:times] || 1) #: Array[Integer]
85
+ times = Array(times) #: Array[Integer]
83
86
  @required = times.first #: Integer
84
87
  @window = times.last #: Integer
85
- @include_children = options[:include_children] || false
88
+ @include_children = include_children
86
89
  clear_history!
87
- create_condition(options)
90
+ create_condition(condition_options)
88
91
  end
89
92
 
90
- # Create the check, with the options the watch doesn't take for itself
91
- #
92
- # It first warns about the options that bluepill 0.3.0 rejects.
93
+ # Return the events a check fires, if each is one a process takes
93
94
  #
94
- # The condition gets the logger while it's created, so its own
95
- # initialize can log, and again after, in case that initialize doesn't
96
- # call super. It gets its options as keyword arguments, which an
97
- # initialize that takes an options Hash gets as the Hash, so a custom
98
- # check can take them either way, as bluepill 0.3.0's need keywords.
95
+ # @param [Array<Object>] events what fires: names
99
96
  #
100
- # @param [Hash{Symbol => Object}] options the options the watch was
101
- # created with, which stay as they are
97
+ # @return [Array<Symbol>] the events
102
98
  #
103
- # @return [void]
99
+ # @raise [ConfigError] for an event that isn't one of
100
+ # Process::StateMachine::TRANSITIONS
104
101
  #
105
102
  # @api private
106
- def create_condition(options)
107
- Deprecation.check_watch(@name, options, @fires)
108
- condition_options = options.reject { |key, _| WATCH_KEYS.include?(key) }
109
- condition_options[:logger] = @logger
110
- condition = ProcessConditions[@name] #: untyped
111
- Deprecation.check_condition_class(condition)
112
- @process_condition = condition.new(**condition_options)
113
- Deprecation.check_logger(@process_condition, @logger)
114
- self.logger = @logger
103
+ def events_to_fire(events)
104
+ known = Process::StateMachine::TRANSITIONS.keys
105
+ unknown = events - known
106
+ return events if unknown.empty?
107
+
108
+ raise ConfigError, "#{@name} can't fire #{unknown.collect(&:inspect).join(", ")}: " \
109
+ "a check fires #{known.collect(&:inspect).join(", ")}"
115
110
  end
116
111
 
117
- # Set the logger the watch and its condition write to
112
+ # Create the check, with the options the watch doesn't take for itself
118
113
  #
119
- # The process sets its watches' logger again when it gets a logger of
120
- # its own, such as one with its group's name.
114
+ # The condition gets the watch's logger as its logger: option, so its
115
+ # own initialize can log once it has called super.
121
116
  #
122
- # @param [Logger] logger the process's logger
117
+ # @param [Hash{Symbol => Object}] options the check's own options
123
118
  #
124
119
  # @return [void]
125
120
  #
126
121
  # @api private
127
- def logger=(logger)
128
- @logger = logger
129
- @process_condition.logger = logger
122
+ def create_condition(options)
123
+ @process_condition = CheckRegistry.fetch(@name).new(**options, logger: @logger)
130
124
  end
131
125
 
132
126
  # Return whether the check is due
@@ -183,7 +177,7 @@ module Bluepill
183
177
  @history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check?(value))
184
178
  logger.info(to_s)
185
179
  # Fire when enough of the values in the window failed
186
- return @fires if @history.count { |v| !v.critical } >= @required
180
+ return @fires if @history.count { |v| !v.passed } >= @required
187
181
 
188
182
  EMPTY_ARRAY
189
183
  rescue => e
@@ -215,9 +209,7 @@ module Bluepill
215
209
  # @return [Util::RotationalArray<HistoryValue>] the new, empty history
216
210
  #
217
211
  # @api private
218
- def clear_history!
219
- @history = Util::RotationalArray.new(@window)
220
- end
212
+ def clear_history! = @history = Util::RotationalArray.new(@window)
221
213
 
222
214
  # Return the check's name and history, as the log shows it
223
215
  #
@@ -226,7 +218,7 @@ module Bluepill
226
218
  #
227
219
  # @api private
228
220
  def to_s
229
- data = @history.collect { |v| "#{v.value}#{"*" unless v.critical}" }.join(", ")
221
+ data = @history.collect { |v| "#{v.value}#{"*" unless v.passed}" }.join(", ")
230
222
  "#{@name}: [#{data}]\n"
231
223
  end
232
224
  end
@@ -13,9 +13,7 @@ module Bluepill
13
13
  # @return [Array<String>]
14
14
  #
15
15
  # @api private
16
- def running_applications
17
- base_directory.socket_names
18
- end
16
+ def running_applications = base_directory.socket_names
19
17
 
20
18
  private
21
19
 
@@ -54,14 +54,17 @@ module Bluepill
54
54
  # It creates the sockets and pids directories, and removes the socket
55
55
  # and pid file of each daemon that's no longer running.
56
56
  #
57
- # @param [Hash{Symbol => Object}] options :base_dir, and :log_file
57
+ # @param [String] base_dir the directory with the daemons' sockets and
58
+ # pid files
59
+ # @param [String, nil] log_file the log file to tail when a daemon names
60
+ # none
58
61
  #
59
62
  # @return [void]
60
63
  #
61
64
  # @api private
62
- def initialize(options)
63
- self.log_file = options[:log_file]
64
- self.base_dir = options.fetch(:base_dir)
65
+ def initialize(base_dir:, log_file: nil)
66
+ self.log_file = log_file
67
+ self.base_dir = base_dir
65
68
  @base_directory = BaseDirectory.new(base_dir)
66
69
 
67
70
  setup_dir_structure
@@ -72,7 +75,7 @@ module Bluepill
72
75
  #
73
76
  # status prints the daemon's status, quit stops the daemon, log tails
74
77
  # the log for a process or group, and the PROCESS_COMMANDS go to the
75
- # daemon. It exits with status 1 for an unknown command.
78
+ # daemon.
76
79
  #
77
80
  # @param [String] application the application's name
78
81
  # @param [String, Symbol] command the command
@@ -80,6 +83,10 @@ module Bluepill
80
83
  #
81
84
  # @return [void]
82
85
  #
86
+ # @raise [UnknownCommandError] for an unknown command
87
+ # @raise [Bluepill::Error] if the daemon can't be reached, runs another
88
+ # version, or answers with an error
89
+ #
83
90
  # @api private
84
91
  def handle_command(application, command, *args)
85
92
  case command.to_sym
@@ -96,9 +103,7 @@ module Bluepill
96
103
 
97
104
  # Send a command to an application's daemon, and return its answer
98
105
  #
99
- # It first checks that the daemon runs this version of bluepill. It
100
- # prints an error that the daemon raised and exits with status 8, and
101
- # aborts if the daemon isn't running.
106
+ # It first checks that the daemon runs this version of bluepill.
102
107
  #
103
108
  # @param [String] application the application's name
104
109
  # @param [String, Symbol] command the command
@@ -106,16 +111,22 @@ module Bluepill
106
111
  #
107
112
  # @return [Object] what the daemon answered
108
113
  #
114
+ # @raise [ConnectionError] if the daemon isn't running, or doesn't
115
+ # answer
116
+ # @raise [VersionMismatchError] if it runs another version
117
+ # @raise [DaemonError] if it answers with an error that it raised
118
+ #
109
119
  # @api private
110
120
  def send_to_daemon(application, command, *args)
111
121
  verify_version!(application)
112
122
 
113
123
  command = [command, *args].join(":")
114
124
  response = Socket.client_command(base_dir, application, command)
115
- exit_with_daemon_error(response) if response.is_a?(Exception)
125
+ raise DaemonError, response if response.is_a?(Exception)
126
+
116
127
  response
117
128
  rescue Errno::ECONNREFUSED
118
- abort("Connection Refused: Server is not running")
129
+ raise ConnectionError, "Connection Refused: Server is not running"
119
130
  end
120
131
 
121
132
  # Return the pattern that finds a process or group's lines in the log
@@ -171,19 +182,19 @@ module Bluepill
171
182
 
172
183
  # Send one of the PROCESS_COMMANDS to the daemon and print what it did
173
184
  #
174
- # It exits with status 1 for any other command.
175
- #
176
185
  # @param [String] application the application's name
177
186
  # @param [String, Symbol] command the command
178
187
  # @param [Array<String>] args the group or process, for example
179
188
  #
180
189
  # @return [void]
181
190
  #
191
+ # @raise [UnknownCommandError] for any other command
192
+ #
182
193
  # @api private
183
194
  def send_process_command(application, command, *args)
184
195
  unless Application::PROCESS_COMMANDS.include?(command.to_sym)
185
- $stderr.puts(format("Unknown command `%s` (or application `%s` has not been loaded yet)", command, command))
186
- exit(1)
196
+ raise UnknownCommandError,
197
+ format("Unknown command `%s` (or application `%s` has not been loaded yet)", command, command)
187
198
  end
188
199
 
189
200
  # these need to be sent to the daemon and the results printed out
@@ -209,40 +220,27 @@ module Bluepill
209
220
  end
210
221
  end
211
222
 
212
- # Print an error that the daemon raised, and exit with status 8
213
- #
214
- # @param [Exception] error the error
215
- #
216
- # @return [void]
217
- #
218
- # @api private
219
- def exit_with_daemon_error(error)
220
- $stderr.puts "Received error from server:"
221
- $stderr.puts error.inspect
222
- # join flattens the backtrace and makes a missing one empty
223
- $stderr.puts [error.backtrace].join("\n")
224
- exit(8)
225
- end
226
-
227
- # Abort unless an application's daemon runs this version of bluepill
223
+ # Check that an application's daemon runs this version of bluepill
228
224
  #
229
- # A daemon older than 0.2.9 answers with its Version module, which
230
- # Marshal loads as the CLI's own, so it can't say which version it runs,
231
- # and the CLI talks to it as before, with a warning, since bluepill
232
- # 0.3.0's CLI refuses it.
225
+ # A daemon older than 0.3.0 answers with its Version module, which
226
+ # Marshal loads as the CLI's own, so it can't say which version it runs.
233
227
  #
234
228
  # @param [String] application the application's name
235
229
  #
236
230
  # @return [void]
237
231
  #
232
+ # @raise [VersionMismatchError] if it runs another version
233
+ #
238
234
  # @api private
239
235
  def verify_version!(application)
240
236
  version = Socket.client_command(base_dir, application, "version")
241
- return Deprecation.check_daemon_version(version) if version.equal?(Version) || Version.to_s.eql?(version)
237
+ return if Version.to_s.eql?(version)
238
+
239
+ raise VersionMismatchError, OUT_OF_DATE unless version.instance_of?(String)
242
240
 
243
- abort("#{OUT_OF_DATE}\nDaemon Version: #{version}, CLI Version: #{Version}")
241
+ raise VersionMismatchError, "#{OUT_OF_DATE}\nDaemon Version: #{version}, CLI Version: #{Version}"
244
242
  rescue ArgumentError
245
- abort(OUT_OF_DATE)
243
+ raise VersionMismatchError, OUT_OF_DATE
246
244
  end
247
245
  end
248
246
  end