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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -21
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +100 -20
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -259
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +129 -65
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -118
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -152
  20. data/lib/bluepill/dsl/process_proxy.rb +100 -51
  21. data/lib/bluepill/dsl.rb +0 -23
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +64 -667
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +48 -45
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -25
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +27 -35
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +47 -994
  74. metadata +39 -49
  75. data/lib/bluepill/clock.rb +0 -48
  76. data/lib/bluepill/deprecation.rb +0 -74
  77. data/lib/bluepill/trigger/custom.rb +0 -147
  78. data/lib/bluepill/util/log_error.rb +0 -29
@@ -1,27 +1,4 @@
1
1
  module Bluepill
2
- # One entry of a watch's history: the value it read, as its log shows it,
3
- # and whether the value was fine
4
- #
5
- # @!attribute value
6
- # Return the value as the history shows it
7
- #
8
- # @return [#to_s] the formatted value
9
- #
10
- # @api private
11
- #
12
- # @!attribute critical
13
- # Return whether the value was fine
14
- #
15
- # It's true for a value that passed the check, and false for one that
16
- # counts toward the watch firing.
17
- #
18
- # @return [Boolean] whether the value passed the check
19
- #
20
- # @api private
21
- #
22
- # @api private
23
- HistoryValue = Struct.new(:value, :critical)
24
-
25
2
  # Runs a process's check every so often, and fires its events when the
26
3
  # check fails often enough
27
4
  #
@@ -31,12 +8,35 @@ module Bluepill
31
8
  #
32
9
  # @api private
33
10
  class ConditionWatch
11
+ # One entry of a watch's history: the value it read, as its log shows it,
12
+ # and whether the value was fine
13
+ #
14
+ # @!attribute value
15
+ # Return the value as the history shows it
16
+ #
17
+ # @return [#to_s] the formatted value
18
+ #
19
+ # @api private
20
+ #
21
+ # @!attribute critical
22
+ # Return whether the value was fine
23
+ #
24
+ # It's true for a value that passed the check, and false for one that
25
+ # counts toward the watch firing.
26
+ #
27
+ # @return [Boolean] whether the value passed the check
28
+ #
29
+ # @api private
30
+ #
31
+ # @api private
32
+ HistoryValue = Struct.new(:value, :critical)
33
+
34
34
  # Return the logger the watch writes its history to
35
35
  #
36
36
  # @return [Logger] the process's logger
37
37
  #
38
38
  # @api private
39
- attr_accessor :logger
39
+ attr_reader :logger
40
40
 
41
41
  # Return the check's name, such as :mem_usage
42
42
  #
@@ -52,6 +52,11 @@ module Bluepill
52
52
  # @api private
53
53
  EMPTY_ARRAY = no_events.freeze
54
54
 
55
+ # The options the watch takes for itself, which the check doesn't get
56
+ #
57
+ # @api private
58
+ WATCH_KEYS = %i[logger fires every times include_children].freeze
59
+
55
60
  # Create a watch of a check
56
61
  #
57
62
  # @param [Symbol] name the check's name, such as :mem_usage
@@ -66,68 +71,137 @@ module Bluepill
66
71
  # @api private
67
72
  def initialize(name, options = {})
68
73
  @name = name
74
+ @logger = options[:logger]
75
+ @fires = Array(options.fetch(:fires, :restart))
76
+ @every = options[:every]
77
+ # times: 5 means 5 of the last 5, and times: [3, 5] 3 of the last 5
78
+ times = Array(options[:times] || 1) #: Array[Integer]
79
+ @required = times.first #: Integer
80
+ @window = times.last #: Integer
81
+ @include_children = options[:include_children] || false
82
+ clear_history!
83
+ create_condition(options)
84
+ end
69
85
 
70
- @logger = options.delete(:logger)
71
- @fires = options.key?(:fires) ? Array(options.delete(:fires)) : [:restart]
72
- @every = options.delete(:every)
73
- @times = Array(options.delete(:times) || 1) # handles times: 5
74
- @include_children = options.delete(:include_children) || false
86
+ # Create the check, with the options the watch doesn't take for itself
87
+ #
88
+ # The condition gets the logger while it's created, so its own
89
+ # initialize can log, and again after, in case that initialize doesn't
90
+ # call super.
91
+ #
92
+ # @param [Hash{Symbol => Object}] options the options the watch was
93
+ # created with, which stay as they are
94
+ #
95
+ # @return [void]
96
+ #
97
+ # @api private
98
+ def create_condition(options)
99
+ condition_options = options.reject { |key, _| WATCH_KEYS.include?(key) }
100
+ condition_options[:logger] = @logger
101
+ @process_condition = ProcessConditions[@name].new(condition_options)
102
+ self.logger = @logger
103
+ end
75
104
 
76
- clear_history!
105
+ # Set the logger the watch and its condition write to
106
+ #
107
+ # The process sets its watches' logger again when it gets a logger of
108
+ # its own, such as one with its group's name.
109
+ #
110
+ # @param [Logger] logger the process's logger
111
+ #
112
+ # @return [void]
113
+ #
114
+ # @api private
115
+ def logger=(logger)
116
+ @logger = logger
117
+ @process_condition.logger = logger
118
+ end
77
119
 
78
- @process_condition = ProcessConditions[@name].new(options)
120
+ # Return whether the check is due
121
+ #
122
+ # It's due on its first run, and then every `every` seconds.
123
+ #
124
+ # @param [Integer] tick_number the current time, in seconds
125
+ #
126
+ # @return [Boolean]
127
+ #
128
+ # @api private
129
+ def due?(tick_number)
130
+ last_ran_at = @last_ran_at
131
+ last_ran_at.nil? || (last_ran_at + @every) <= tick_number
79
132
  end
80
133
 
81
134
  # Run the check if it's due, and return the events to fire
82
135
  #
83
136
  # A check is due on its first run, and then every `every` seconds. A
84
- # run adds the value to the history and logs the history. A check that
85
- # raises is logged and skipped until its next run, so one broken check
86
- # doesn't stop the daemon.
137
+ # run adds the value to the history and logs the history.
87
138
  #
88
139
  # @param [Integer, nil] pid the process's pid
89
- # @param [Integer] tick_number the time, from Clock.now
140
+ # @param [Integer] tick_number the current time, in seconds
90
141
  #
91
142
  # @return [Array<Symbol>] the events to fire, empty unless the check
92
143
  # ran and failed often enough
93
144
  #
145
+ # @raise [StandardError] whatever the check raised, other than a
146
+ # System::PsError, after logging it
147
+ #
94
148
  # @api private
95
- def run(pid, tick_number = Clock.now)
149
+ def run(pid, tick_number = Integer(Time.now))
150
+ # The same test as due?, written out to save a call on every check.
151
+ # An if, not a guard clause: the guard ran 3% slower on the zombie
152
+ # benchmark, with the same instructions.
96
153
  last_ran_at = @last_ran_at
97
154
  if last_ran_at.nil? || (last_ran_at + @every) <= tick_number
98
155
  @last_ran_at = tick_number
156
+ return run_check(pid)
157
+ end
158
+ EMPTY_ARRAY
159
+ end
99
160
 
100
- # A rescue here, not in a method of its own, so a check that runs
101
- # makes no extra call
102
- begin
103
- value = @process_condition.run(pid, @include_children)
104
- @history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check(value))
105
- logger.info(to_s)
106
-
107
- return @fires if fired?
108
- rescue StandardError => e
109
- check_failed(e)
110
- end
161
+ # Run the check, add its value to the history, and log the history
162
+ #
163
+ # @param [Integer, nil] pid the process's pid
164
+ #
165
+ # @return [Array<Symbol>] the events to fire, empty unless enough of
166
+ # the last values failed for the watch to fire, or ps failed
167
+ #
168
+ # @raise [StandardError] whatever the check raised, other than a
169
+ # System::PsError, after logging it
170
+ #
171
+ # @api private
172
+ def run_check(pid)
173
+ begin
174
+ value = @process_condition.run(pid, @include_children)
175
+ rescue => e
176
+ return check_failed(e)
111
177
  end
178
+ @history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check?(value))
179
+ logger.info(to_s)
180
+ # Fire when enough of the values in the window failed
181
+ return @fires if @history.count { |v| !v.critical } >= @required
182
+
112
183
  EMPTY_ARRAY
113
184
  end
114
185
 
115
- # Log an error the check raised, and skip the check until its next run
186
+ # Log an error the check raised, and re-raise it unless ps failed
116
187
  #
117
- # A failed ps is a warning, since it says nothing about the check. Any
118
- # other error is logged with its backtrace. Either way, the check adds
119
- # nothing to its history and fires nothing this time.
188
+ # Call it from where the error was rescued, so raise re-raises it.
120
189
  #
121
190
  # @param [StandardError] error what the check raised
122
191
  #
123
- # @return [void]
192
+ # @return [Array<Symbol>] no events, when ps failed
193
+ #
194
+ # @raise [StandardError] the error, unless it's a System::PsError
124
195
  #
125
196
  # @api private
126
197
  def check_failed(error)
127
198
  if error.is_a?(System::PsError)
199
+ # No data this time, so leave the history alone and check again next interval
128
200
  logger.warning("#{@name}: #{error}")
201
+ EMPTY_ARRAY
129
202
  else
130
- Util.log_error(logger, "#{@name} check failed", error)
203
+ logger.err(error.backtrace)
204
+ raise
131
205
  end
132
206
  end
133
207
 
@@ -137,17 +211,7 @@ module Bluepill
137
211
  #
138
212
  # @api private
139
213
  def clear_history!
140
- @history = Util::RotationalArray.new(@times.last)
141
- end
142
-
143
- # Return whether enough of the last values failed for the watch to fire
144
- #
145
- # @return [Boolean] true when the failures reach the times the check
146
- # allows
147
- #
148
- # @api private
149
- def fired?
150
- @history.count { |v| !v.critical } >= @times.first
214
+ @history = Util::RotationalArray.new(@window)
151
215
  end
152
216
 
153
217
  # Return the check's name and history, as the log shows it
@@ -157,7 +221,7 @@ module Bluepill
157
221
  #
158
222
  # @api private
159
223
  def to_s
160
- data = @history.collect { |v| "#{v.value}#{'*' unless v.critical}" }.join(', ')
224
+ data = @history.collect { |v| "#{v.value}#{"*" unless v.critical}" }.join(", ")
161
225
  "#{@name}: [#{data}]\n"
162
226
  end
163
227
  end
@@ -0,0 +1,59 @@
1
+ module Bluepill
2
+ class Controller
3
+ # Find the daemons in the base directory, and clean up after dead ones
4
+ #
5
+ # Controller is the only class that includes it.
6
+ #
7
+ # @api private
8
+ module Directory
9
+ # Return the names of the applications with a daemon's socket
10
+ #
11
+ # @return [Array<String>]
12
+ #
13
+ # @api private
14
+ def running_applications
15
+ base_directory.socket_names
16
+ end
17
+
18
+ private
19
+
20
+ # Remove the socket and pid file of each daemon that's not running
21
+ #
22
+ # @return [void]
23
+ #
24
+ # @api private
25
+ def cleanup_bluepill_directory
26
+ running_applications.each do |app|
27
+ pid = pid_for(app)
28
+ next if pid && System.pid_alive?(pid)
29
+
30
+ System.delete_if_exists(base_directory.pid_file(app))
31
+ System.delete_if_exists(base_directory.socket_file(app))
32
+ end
33
+ end
34
+
35
+ # Return the pid in an application's pid file
36
+ #
37
+ # @param [String] app the application's name
38
+ #
39
+ # @return [Integer, false] the pid, or false without a valid one
40
+ #
41
+ # @api private
42
+ def pid_for(app)
43
+ # false, not nil, so quit says "bluepilld[false] not running"
44
+ PidFile.read(base_directory.pid_file(app)) || false
45
+ end
46
+
47
+ # Create the sockets and pids directories
48
+ #
49
+ # @return [void]
50
+ #
51
+ # @api private
52
+ def setup_dir_structure
53
+ [base_directory.sockets_dir, base_directory.pids_dir].each do |dir|
54
+ FileUtils.mkdir_p(dir)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,8 @@
1
- require 'fileutils'
2
- require 'bluepill/system'
1
+ require "fileutils"
2
+ require "bluepill/system"
3
+ require "bluepill/base_directory"
4
+ require "bluepill/pid_file"
5
+ require "bluepill/controller/directory"
3
6
 
4
7
  module Bluepill
5
8
  # Run the bluepill CLI's commands, and send them to the daemon
@@ -9,21 +12,10 @@ module Bluepill
9
12
  #
10
13
  # @api private
11
14
  class Controller
12
- # The shell command that tails a log file ($1) through grep -E ($2)
13
- #
14
- # The path and the pattern are the shell's arguments, so they're never
15
- # parsed as shell code, whatever characters they hold.
16
- TAIL_COMMAND = 'tail -n 100 -f "$1" | grep -E "$2"'.freeze
17
-
18
- # What the CLI says when the daemon runs another version of bluepill
19
- OUT_OF_DATE = 'The running version of your daemon seems to be out of date.'.freeze
15
+ include Directory
20
16
 
21
- # What the CLI says to a daemon from before 0.1.7, which can't say
22
- # which version it runs
23
- UNKNOWN_VERSION = 'The running daemon is older than bluepill 0.1.7, so the CLI cannot check its version. ' \
24
- 'Run bluepill load again to replace it.'.freeze
25
-
26
- attr_accessor :base_dir, :log_file, :sockets_dir, :pids_dir
17
+ attr_accessor :base_dir, :log_file
18
+ attr_reader :base_directory
27
19
 
28
20
  # @!attribute [rw] base_dir
29
21
  # The directory with the daemons' sockets and pid files
@@ -39,17 +31,10 @@ module Bluepill
39
31
  #
40
32
  # @api private
41
33
 
42
- # @!attribute [rw] sockets_dir
43
- # The directory with the daemons' sockets
44
- #
45
- # @return [String]
46
- #
47
- # @api private
48
-
49
- # @!attribute [rw] pids_dir
50
- # The directory with the daemons' pid files
34
+ # @!attribute [r] base_directory
35
+ # The paths in the base directory
51
36
  #
52
- # @return [String]
37
+ # @return [BaseDirectory]
53
38
  #
54
39
  # @api private
55
40
 
@@ -66,22 +51,12 @@ module Bluepill
66
51
  def initialize(options)
67
52
  self.log_file = options[:log_file]
68
53
  self.base_dir = options.fetch(:base_dir)
69
- self.sockets_dir = File.join(base_dir, 'socks')
70
- self.pids_dir = File.join(base_dir, 'pids')
54
+ @base_directory = BaseDirectory.new(base_dir)
71
55
 
72
56
  setup_dir_structure
73
57
  cleanup_bluepill_directory
74
58
  end
75
59
 
76
- # Return the names of the applications with a daemon's socket
77
- #
78
- # @return [Array<String>]
79
- #
80
- # @api private
81
- def running_applications
82
- Dir[File.join(sockets_dir, '*.sock')].collect { |x| File.basename(x, '.sock') }
83
- end
84
-
85
60
  # Run a CLI command and print what it did
86
61
  #
87
62
  # status prints the daemon's status, quit stops the daemon, log tails
@@ -100,35 +75,11 @@ module Bluepill
100
75
  when :status
101
76
  puts send_to_daemon(application, :status, *args)
102
77
  when :quit
103
- pid = pid_for(application)
104
- if pid && System.pid_alive?(pid)
105
- ::Process.kill('TERM', pid)
106
- puts "Killing bluepilld[#{pid}]"
107
- else
108
- puts "bluepilld[#{pid}] not running"
109
- end
78
+ quit_daemon(application)
110
79
  when :log
111
- log_file_location = send_to_daemon(application, :log_file).to_s
112
- log_file_location = log_file.to_s if log_file_location.rstrip.empty?
113
-
114
- requested_pattern = args.first
115
- puts "Tailing log for #{requested_pattern}..."
116
- # The path and the pattern are the shell's arguments, not its code
117
- Kernel.exec('sh', '-c', TAIL_COMMAND, 'sh', log_file_location, grep_pattern(application, requested_pattern))
118
- when *Application::PROCESS_COMMANDS
119
- # these need to be sent to the daemon and the results printed out
120
- affected = send_to_daemon(application, command, *args)
121
- if affected.empty?
122
- puts 'No processes effected'
123
- else
124
- puts "Sent #{command} to:"
125
- affected.each do |process|
126
- puts " #{process}"
127
- end
128
- end
80
+ tail_log(application, args.first)
129
81
  else
130
- $stderr.puts(format('Unknown command `%s` (or application `%s` has not been loaded yet)', command, command))
131
- exit(1)
82
+ send_process_command(application, command, *args)
132
83
  end
133
84
  end
134
85
 
@@ -148,19 +99,12 @@ module Bluepill
148
99
  def send_to_daemon(application, command, *args)
149
100
  verify_version!(application)
150
101
 
151
- command = [command, *args].join(':')
102
+ command = [command, *args].join(":")
152
103
  response = Socket.client_command(base_dir, application, command)
153
- if response.is_a?(Exception)
154
- $stderr.puts 'Received error from server:'
155
- $stderr.puts response.inspect
156
- # join flattens the backtrace and makes a missing one empty
157
- $stderr.puts [response.backtrace].join("\n")
158
- exit(8)
159
- else
160
- response
161
- end
104
+ exit_with_daemon_error(response) if response.is_a?(Exception)
105
+ response
162
106
  rescue Errno::ECONNREFUSED
163
- abort('Connection Refused: Server is not running')
107
+ abort("Connection Refused: Server is not running")
164
108
  end
165
109
 
166
110
  # Return the pattern that finds a process or group's lines in the log
@@ -172,64 +116,107 @@ module Bluepill
172
116
  #
173
117
  # @api private
174
118
  def grep_pattern(application, query = nil)
175
- pattern = [application, query].compact.join(':')
176
- ['\[.*', Regexp.escape(pattern), '.*'].join
119
+ pattern = [application, query].compact.join(":")
120
+ ['\[.*', Regexp.escape(pattern), ".*"].join
177
121
  end
178
122
 
179
- private
123
+ private
180
124
 
181
- # Remove the socket and pid file of each daemon that's not running
125
+ # Stop an application's daemon, if it's running
126
+ #
127
+ # @param [String] application the application's name
182
128
  #
183
129
  # @return [void]
184
130
  #
185
131
  # @api private
186
- def cleanup_bluepill_directory
187
- running_applications.each do |app|
188
- pid = pid_for(app)
189
- next if pid && System.pid_alive?(pid)
190
-
191
- pid_file = File.join(pids_dir, "#{app}.pid")
192
- sock_file = File.join(sockets_dir, "#{app}.sock")
193
- System.delete_if_exists(pid_file)
194
- System.delete_if_exists(sock_file)
132
+ def quit_daemon(application)
133
+ pid = pid_for(application)
134
+ if pid && System.pid_alive?(pid)
135
+ ::Process.kill("TERM", pid)
136
+ puts "Killing bluepilld[#{pid}]"
137
+ else
138
+ puts "bluepilld[#{pid}] not running"
195
139
  end
196
140
  end
197
141
 
198
- # Return the pid in an application's pid file
142
+ # Tail the daemon's log, showing only a process or group's lines
143
+ #
144
+ # It falls back to the controller's log_file when the daemon doesn't
145
+ # name one, and replaces the CLI with tail.
146
+ #
147
+ # @param [String] application the application's name
148
+ # @param [String, nil] requested_pattern the process or group
149
+ #
150
+ # @return [void]
151
+ #
152
+ # @api private
153
+ def tail_log(application, requested_pattern)
154
+ log_file_location = send_to_daemon(application, :log_file)
155
+ log_file_location = log_file if log_file_location.to_s.rstrip.empty?
156
+
157
+ pattern = grep_pattern(application, requested_pattern)
158
+
159
+ tail = "tail -n 100 -f #{log_file_location} | grep -E '#{pattern}'"
160
+ puts "Tailing log for #{requested_pattern}..."
161
+ Kernel.exec(tail)
162
+ end
163
+
164
+ # Send one of the PROCESS_COMMANDS to the daemon and print what it did
165
+ #
166
+ # It exits with status 1 for any other command.
199
167
  #
200
- # @param [String] app the application's name
168
+ # @param [String] application the application's name
169
+ # @param [String, Symbol] command the command
170
+ # @param [Array<String>] args the group or process, for example
201
171
  #
202
- # @return [Integer, false] the pid, or false without a valid one
172
+ # @return [void]
203
173
  #
204
174
  # @api private
205
- def pid_for(app)
206
- pid_file = File.join(pids_dir, "#{app}.pid")
207
- return false unless File.exist?(pid_file)
208
-
209
- # An empty or garbled pid file reads as 0, and signaling pid 0 would
210
- # hit the CLI's own process group
211
- pid = File.read(pid_file).to_i
212
- pid > 0 ? pid : false
175
+ def send_process_command(application, command, *args)
176
+ unless Application::PROCESS_COMMANDS.include?(command.to_sym)
177
+ $stderr.puts(format("Unknown command `%s` (or application `%s` has not been loaded yet)", command, command))
178
+ exit(1)
179
+ end
180
+
181
+ # these need to be sent to the daemon and the results printed out
182
+ print_affected(command, send_to_daemon(application, command, *args))
213
183
  end
214
184
 
215
- # Create the sockets and pids directories
185
+ # Print the processes that a command affected
186
+ #
187
+ # @param [String, Symbol] command the command
188
+ # @param [Array<String>] affected the processes, as group:process
216
189
  #
217
190
  # @return [void]
218
191
  #
219
192
  # @api private
220
- def setup_dir_structure
221
- [@sockets_dir, @pids_dir].each do |dir|
222
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
193
+ def print_affected(command, affected)
194
+ if affected.empty?
195
+ puts "No processes effected"
196
+ else
197
+ puts "Sent #{command} to:"
198
+ affected.each do |process|
199
+ puts " #{process}"
200
+ end
223
201
  end
224
202
  end
225
203
 
226
- # Abort unless an application's daemon runs this version of bluepill
204
+ # Print an error that the daemon raised, and exit with status 8
205
+ #
206
+ # @param [Exception] error the error
207
+ #
208
+ # @return [void]
227
209
  #
228
- # A daemon answers with its version as a String. One from before 0.1.7
229
- # answers with its Version module, which Marshal loads as the CLI's own,
230
- # so it can't say which version it runs. The CLI warns about it and goes
231
- # on, as it did before, so upgrading the gem doesn't stop the CLI from
232
- # working with the daemon that's running.
210
+ # @api private
211
+ def exit_with_daemon_error(error)
212
+ $stderr.puts "Received error from server:"
213
+ $stderr.puts error.inspect
214
+ # join flattens the backtrace and makes a missing one empty
215
+ $stderr.puts [error.backtrace].join("\n")
216
+ exit(8)
217
+ end
218
+
219
+ # Abort unless an application's daemon runs this version of bluepill
233
220
  #
234
221
  # @param [String] application the application's name
235
222
  #
@@ -237,16 +224,10 @@ module Bluepill
237
224
  #
238
225
  # @api private
239
226
  def verify_version!(application)
240
- version = Socket.client_command(base_dir, application, 'version')
241
- return if Version.to_s.eql?(version)
242
-
243
- if version.instance_of?(String)
244
- abort("#{OUT_OF_DATE}\nDaemon Version: #{version}, CLI Version: #{Version}")
245
- else
246
- $stderr.puts(UNKNOWN_VERSION)
247
- end
227
+ version = Socket.client_command(base_dir, application, "version")
228
+ abort("The running version of your daemon seems to be out of date.\nDaemon Version: #{version}, CLI Version: #{Version}") unless version.equal?(Version)
248
229
  rescue ArgumentError
249
- abort(OUT_OF_DATE)
230
+ abort("The running version of your daemon seems to be out of date.")
250
231
  end
251
232
  end
252
233
  end