bluepill 0.1.5 → 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 +57 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +105 -8
- 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 -231
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/condition_watch.rb +134 -56
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -94
- 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 -92
- data/lib/bluepill/dsl/process_proxy.rb +109 -21
- 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 +56 -653
- 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 +104 -172
- 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 +52 -24
- 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 -26
- data/lib/bluepill/triggers/flapping.rb +7 -15
- 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 +33 -36
- 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 -869
- metadata +39 -45
|
@@ -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
|
-
|
|
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,16 +71,64 @@ 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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -94,46 +147,71 @@ module Bluepill
|
|
|
94
147
|
#
|
|
95
148
|
# @api private
|
|
96
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.
|
|
97
153
|
last_ran_at = @last_ran_at
|
|
98
154
|
if last_ran_at.nil? || (last_ran_at + @every) <= tick_number
|
|
99
155
|
@last_ran_at = tick_number
|
|
156
|
+
return run_check(pid)
|
|
157
|
+
end
|
|
158
|
+
EMPTY_ARRAY
|
|
159
|
+
end
|
|
100
160
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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)
|
|
116
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
|
+
|
|
117
183
|
EMPTY_ARRAY
|
|
118
184
|
end
|
|
119
185
|
|
|
120
|
-
#
|
|
186
|
+
# Log an error the check raised, and re-raise it unless ps failed
|
|
121
187
|
#
|
|
122
|
-
#
|
|
188
|
+
# Call it from where the error was rescued, so raise re-raises it.
|
|
189
|
+
#
|
|
190
|
+
# @param [StandardError] error what the check raised
|
|
191
|
+
#
|
|
192
|
+
# @return [Array<Symbol>] no events, when ps failed
|
|
193
|
+
#
|
|
194
|
+
# @raise [StandardError] the error, unless it's a System::PsError
|
|
123
195
|
#
|
|
124
196
|
# @api private
|
|
125
|
-
def
|
|
126
|
-
|
|
197
|
+
def check_failed(error)
|
|
198
|
+
if error.is_a?(System::PsError)
|
|
199
|
+
# No data this time, so leave the history alone and check again next interval
|
|
200
|
+
logger.warning("#{@name}: #{error}")
|
|
201
|
+
EMPTY_ARRAY
|
|
202
|
+
else
|
|
203
|
+
logger.err(error.backtrace)
|
|
204
|
+
raise
|
|
205
|
+
end
|
|
127
206
|
end
|
|
128
207
|
|
|
129
|
-
#
|
|
208
|
+
# Forget the values the check read, as when the process changes state
|
|
130
209
|
#
|
|
131
|
-
# @return [
|
|
132
|
-
# allows
|
|
210
|
+
# @return [Util::RotationalArray<HistoryValue>] the new, empty history
|
|
133
211
|
#
|
|
134
212
|
# @api private
|
|
135
|
-
def
|
|
136
|
-
@history
|
|
213
|
+
def clear_history!
|
|
214
|
+
@history = Util::RotationalArray.new(@window)
|
|
137
215
|
end
|
|
138
216
|
|
|
139
217
|
# Return the check's name and history, as the log shows it
|
|
@@ -143,7 +221,7 @@ module Bluepill
|
|
|
143
221
|
#
|
|
144
222
|
# @api private
|
|
145
223
|
def to_s
|
|
146
|
-
data = @history.collect { |v| "#{v.value}#{
|
|
224
|
+
data = @history.collect { |v| "#{v.value}#{"*" unless v.critical}" }.join(", ")
|
|
147
225
|
"#{@name}: [#{data}]\n"
|
|
148
226
|
end
|
|
149
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
|
data/lib/bluepill/controller.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
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,7 +12,10 @@ module Bluepill
|
|
|
9
12
|
#
|
|
10
13
|
# @api private
|
|
11
14
|
class Controller
|
|
12
|
-
|
|
15
|
+
include Directory
|
|
16
|
+
|
|
17
|
+
attr_accessor :base_dir, :log_file
|
|
18
|
+
attr_reader :base_directory
|
|
13
19
|
|
|
14
20
|
# @!attribute [rw] base_dir
|
|
15
21
|
# The directory with the daemons' sockets and pid files
|
|
@@ -25,17 +31,10 @@ module Bluepill
|
|
|
25
31
|
#
|
|
26
32
|
# @api private
|
|
27
33
|
|
|
28
|
-
# @!attribute [
|
|
29
|
-
# The
|
|
30
|
-
#
|
|
31
|
-
# @return [String]
|
|
34
|
+
# @!attribute [r] base_directory
|
|
35
|
+
# The paths in the base directory
|
|
32
36
|
#
|
|
33
|
-
# @
|
|
34
|
-
|
|
35
|
-
# @!attribute [rw] pids_dir
|
|
36
|
-
# The directory with the daemons' pid files
|
|
37
|
-
#
|
|
38
|
-
# @return [String]
|
|
37
|
+
# @return [BaseDirectory]
|
|
39
38
|
#
|
|
40
39
|
# @api private
|
|
41
40
|
|
|
@@ -52,22 +51,12 @@ module Bluepill
|
|
|
52
51
|
def initialize(options)
|
|
53
52
|
self.log_file = options[:log_file]
|
|
54
53
|
self.base_dir = options.fetch(:base_dir)
|
|
55
|
-
|
|
56
|
-
self.pids_dir = File.join(base_dir, 'pids')
|
|
54
|
+
@base_directory = BaseDirectory.new(base_dir)
|
|
57
55
|
|
|
58
56
|
setup_dir_structure
|
|
59
57
|
cleanup_bluepill_directory
|
|
60
58
|
end
|
|
61
59
|
|
|
62
|
-
# Return the names of the applications with a daemon's socket
|
|
63
|
-
#
|
|
64
|
-
# @return [Array<String>]
|
|
65
|
-
#
|
|
66
|
-
# @api private
|
|
67
|
-
def running_applications
|
|
68
|
-
Dir[File.join(sockets_dir, '*.sock')].collect { |x| File.basename(x, '.sock') }
|
|
69
|
-
end
|
|
70
|
-
|
|
71
60
|
# Run a CLI command and print what it did
|
|
72
61
|
#
|
|
73
62
|
# status prints the daemon's status, quit stops the daemon, log tails
|
|
@@ -86,37 +75,11 @@ module Bluepill
|
|
|
86
75
|
when :status
|
|
87
76
|
puts send_to_daemon(application, :status, *args)
|
|
88
77
|
when :quit
|
|
89
|
-
|
|
90
|
-
if pid && System.pid_alive?(pid)
|
|
91
|
-
::Process.kill('TERM', pid)
|
|
92
|
-
puts "Killing bluepilld[#{pid}]"
|
|
93
|
-
else
|
|
94
|
-
puts "bluepilld[#{pid}] not running"
|
|
95
|
-
end
|
|
78
|
+
quit_daemon(application)
|
|
96
79
|
when :log
|
|
97
|
-
|
|
98
|
-
log_file_location = log_file if log_file_location.to_s.rstrip.empty?
|
|
99
|
-
|
|
100
|
-
requested_pattern = args.first
|
|
101
|
-
pattern = grep_pattern(application, requested_pattern)
|
|
102
|
-
|
|
103
|
-
tail = "tail -n 100 -f #{log_file_location} | grep -E '#{pattern}'"
|
|
104
|
-
puts "Tailing log for #{requested_pattern}..."
|
|
105
|
-
Kernel.exec(tail)
|
|
106
|
-
when *Application::PROCESS_COMMANDS
|
|
107
|
-
# these need to be sent to the daemon and the results printed out
|
|
108
|
-
affected = send_to_daemon(application, command, *args)
|
|
109
|
-
if affected.empty?
|
|
110
|
-
puts 'No processes effected'
|
|
111
|
-
else
|
|
112
|
-
puts "Sent #{command} to:"
|
|
113
|
-
affected.each do |process|
|
|
114
|
-
puts " #{process}"
|
|
115
|
-
end
|
|
116
|
-
end
|
|
80
|
+
tail_log(application, args.first)
|
|
117
81
|
else
|
|
118
|
-
|
|
119
|
-
exit(1)
|
|
82
|
+
send_process_command(application, command, *args)
|
|
120
83
|
end
|
|
121
84
|
end
|
|
122
85
|
|
|
@@ -136,19 +99,12 @@ module Bluepill
|
|
|
136
99
|
def send_to_daemon(application, command, *args)
|
|
137
100
|
verify_version!(application)
|
|
138
101
|
|
|
139
|
-
command = [command, *args].join(
|
|
102
|
+
command = [command, *args].join(":")
|
|
140
103
|
response = Socket.client_command(base_dir, application, command)
|
|
141
|
-
if response.is_a?(Exception)
|
|
142
|
-
|
|
143
|
-
$stderr.puts response.inspect
|
|
144
|
-
# join flattens the backtrace and makes a missing one empty
|
|
145
|
-
$stderr.puts [response.backtrace].join("\n")
|
|
146
|
-
exit(8)
|
|
147
|
-
else
|
|
148
|
-
response
|
|
149
|
-
end
|
|
104
|
+
exit_with_daemon_error(response) if response.is_a?(Exception)
|
|
105
|
+
response
|
|
150
106
|
rescue Errno::ECONNREFUSED
|
|
151
|
-
abort(
|
|
107
|
+
abort("Connection Refused: Server is not running")
|
|
152
108
|
end
|
|
153
109
|
|
|
154
110
|
# Return the pattern that finds a process or group's lines in the log
|
|
@@ -160,57 +116,106 @@ module Bluepill
|
|
|
160
116
|
#
|
|
161
117
|
# @api private
|
|
162
118
|
def grep_pattern(application, query = nil)
|
|
163
|
-
pattern = [application, query].compact.join(
|
|
164
|
-
['\[.*', Regexp.escape(pattern),
|
|
119
|
+
pattern = [application, query].compact.join(":")
|
|
120
|
+
['\[.*', Regexp.escape(pattern), ".*"].join
|
|
165
121
|
end
|
|
166
122
|
|
|
167
|
-
|
|
123
|
+
private
|
|
168
124
|
|
|
169
|
-
#
|
|
125
|
+
# Stop an application's daemon, if it's running
|
|
126
|
+
#
|
|
127
|
+
# @param [String] application the application's name
|
|
170
128
|
#
|
|
171
129
|
# @return [void]
|
|
172
130
|
#
|
|
173
131
|
# @api private
|
|
174
|
-
def
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
System.delete_if_exists(pid_file)
|
|
182
|
-
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"
|
|
183
139
|
end
|
|
184
140
|
end
|
|
185
141
|
|
|
186
|
-
#
|
|
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
|
|
187
165
|
#
|
|
188
|
-
#
|
|
166
|
+
# It exits with status 1 for any other command.
|
|
189
167
|
#
|
|
190
|
-
# @
|
|
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
|
|
171
|
+
#
|
|
172
|
+
# @return [void]
|
|
191
173
|
#
|
|
192
174
|
# @api private
|
|
193
|
-
def
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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))
|
|
201
183
|
end
|
|
202
184
|
|
|
203
|
-
#
|
|
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
|
|
204
189
|
#
|
|
205
190
|
# @return [void]
|
|
206
191
|
#
|
|
207
192
|
# @api private
|
|
208
|
-
def
|
|
209
|
-
|
|
210
|
-
|
|
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
|
|
211
201
|
end
|
|
212
202
|
end
|
|
213
203
|
|
|
204
|
+
# Print an error that the daemon raised, and exit with status 8
|
|
205
|
+
#
|
|
206
|
+
# @param [Exception] error the error
|
|
207
|
+
#
|
|
208
|
+
# @return [void]
|
|
209
|
+
#
|
|
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
|
+
|
|
214
219
|
# Abort unless an application's daemon runs this version of bluepill
|
|
215
220
|
#
|
|
216
221
|
# @param [String] application the application's name
|
|
@@ -219,10 +224,10 @@ module Bluepill
|
|
|
219
224
|
#
|
|
220
225
|
# @api private
|
|
221
226
|
def verify_version!(application)
|
|
222
|
-
version = Socket.client_command(base_dir, application,
|
|
227
|
+
version = Socket.client_command(base_dir, application, "version")
|
|
223
228
|
abort("The running version of your daemon seems to be out of date.\nDaemon Version: #{version}, CLI Version: #{Version}") unless version.equal?(Version)
|
|
224
229
|
rescue ArgumentError
|
|
225
|
-
abort(
|
|
230
|
+
abort("The running version of your daemon seems to be out of date.")
|
|
226
231
|
end
|
|
227
232
|
end
|
|
228
233
|
end
|