bluepill 0.2.7 → 0.3.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 +144 -1
- data/README.md +17 -10
- data/bin/bluepill +8 -2
- data/bluepill.gemspec +1 -1
- data/lib/bluepill/application/lifecycle.rb +43 -31
- data/lib/bluepill/application/listener.rb +1 -1
- data/lib/bluepill/application/previous_daemon.rb +14 -14
- data/lib/bluepill/application.rb +48 -28
- data/lib/bluepill/base_directory.rb +18 -20
- data/lib/bluepill/check_registry.rb +104 -0
- data/lib/bluepill/clock.rb +1 -3
- data/lib/bluepill/condition_watch.rb +61 -67
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +46 -38
- data/lib/bluepill/dsl/app_proxy.rb +69 -58
- data/lib/bluepill/dsl/attributes.rb +135 -0
- data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
- data/lib/bluepill/dsl/process_factory.rb +86 -70
- data/lib/bluepill/dsl/process_proxy.rb +61 -158
- data/lib/bluepill/dsl.rb +15 -6
- data/lib/bluepill/errors.rb +190 -0
- data/lib/bluepill/group.rb +14 -37
- data/lib/bluepill/logger.rb +55 -97
- data/lib/bluepill/process/attributes.rb +66 -32
- data/lib/bluepill/process/children.rb +4 -6
- data/lib/bluepill/process/pid.rb +41 -35
- data/lib/bluepill/process/state_machine.rb +15 -59
- data/lib/bluepill/process/stop_signals.rb +120 -0
- data/lib/bluepill/process/transition.rb +1 -3
- data/lib/bluepill/process/transition_callbacks.rb +2 -19
- data/lib/bluepill/process/triggers.rb +90 -0
- data/lib/bluepill/process/user_commands.rb +1 -1
- data/lib/bluepill/process/watches.rb +9 -23
- data/lib/bluepill/process.rb +70 -55
- data/lib/bluepill/process_commands/execution.rb +14 -22
- data/lib/bluepill/process_commands/hooks.rb +1 -31
- data/lib/bluepill/process_commands/stop_signals.rb +12 -17
- data/lib/bluepill/process_commands.rb +10 -8
- data/lib/bluepill/process_conditions/always_true.rb +2 -6
- data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
- data/lib/bluepill/process_conditions/file_time.rb +8 -9
- data/lib/bluepill/process_conditions/http.rb +28 -43
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +30 -32
- data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
- data/lib/bluepill/process_conditions.rb +5 -14
- data/lib/bluepill/process_journal/cleanup.rb +5 -53
- data/lib/bluepill/process_journal/config.rb +73 -0
- data/lib/bluepill/process_journal/entries.rb +128 -0
- data/lib/bluepill/process_journal/journal.rb +69 -0
- data/lib/bluepill/process_journal/kind.rb +60 -0
- data/lib/bluepill/process_journal/lock.rb +9 -4
- data/lib/bluepill/process_journal.rb +13 -324
- data/lib/bluepill/process_statistics.rb +3 -7
- data/lib/bluepill/socket.rb +19 -17
- data/lib/bluepill/system/command_options.rb +135 -0
- data/lib/bluepill/system/command_result.rb +73 -0
- data/lib/bluepill/system/command_setup.rb +3 -4
- data/lib/bluepill/system/commands.rb +21 -18
- data/lib/bluepill/system/daemon.rb +19 -14
- data/lib/bluepill/system/files.rb +35 -0
- data/lib/bluepill/system/ps.rb +20 -17
- data/lib/bluepill/system/ps_table.rb +7 -9
- data/lib/bluepill/system/usage.rb +155 -0
- data/lib/bluepill/system.rb +7 -192
- data/lib/bluepill/trigger.rb +22 -32
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/util/log_error.rb +28 -0
- data/lib/bluepill/version.rb +12 -22
- data/lib/bluepill.rb +1 -0
- data/sig/bluepill/application.rbs +17 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +43 -30
- data/sig/bluepill/dsl.rbs +98 -55
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +42 -304
- data/sig/bluepill/process_commands.rbs +235 -0
- data/sig/bluepill/stop_signals.rbs +21 -0
- data/sig/bluepill/system.rbs +154 -99
- data/sig/bluepill/version.rbs +17 -0
- data/sig/bluepill.rbs +4 -17
- data/sig/manifest.yaml +1 -0
- metadata +22 -3
- data/lib/bluepill/dsl/process_factory/children.rb +0 -73
data/lib/bluepill/controller.rb
CHANGED
|
@@ -16,6 +16,15 @@ module Bluepill
|
|
|
16
16
|
class Controller
|
|
17
17
|
include Directory
|
|
18
18
|
|
|
19
|
+
# The shell command that tails a log file ($1) through grep -E ($2)
|
|
20
|
+
#
|
|
21
|
+
# The path and the pattern are the shell's arguments, so they're never
|
|
22
|
+
# parsed as shell code, whatever characters they hold.
|
|
23
|
+
TAIL_COMMAND = 'tail -n 100 -f "$1" | grep -E "$2"'
|
|
24
|
+
|
|
25
|
+
# What the CLI says when the daemon runs another version of bluepill
|
|
26
|
+
OUT_OF_DATE = "The running version of your daemon seems to be out of date."
|
|
27
|
+
|
|
19
28
|
attr_accessor :base_dir, :log_file
|
|
20
29
|
attr_reader :base_directory
|
|
21
30
|
|
|
@@ -45,14 +54,17 @@ module Bluepill
|
|
|
45
54
|
# It creates the sockets and pids directories, and removes the socket
|
|
46
55
|
# and pid file of each daemon that's no longer running.
|
|
47
56
|
#
|
|
48
|
-
# @param [
|
|
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
|
|
49
61
|
#
|
|
50
62
|
# @return [void]
|
|
51
63
|
#
|
|
52
64
|
# @api private
|
|
53
|
-
def initialize(
|
|
54
|
-
self.log_file =
|
|
55
|
-
self.base_dir =
|
|
65
|
+
def initialize(base_dir:, log_file: nil)
|
|
66
|
+
self.log_file = log_file
|
|
67
|
+
self.base_dir = base_dir
|
|
56
68
|
@base_directory = BaseDirectory.new(base_dir)
|
|
57
69
|
|
|
58
70
|
setup_dir_structure
|
|
@@ -63,7 +75,7 @@ module Bluepill
|
|
|
63
75
|
#
|
|
64
76
|
# status prints the daemon's status, quit stops the daemon, log tails
|
|
65
77
|
# the log for a process or group, and the PROCESS_COMMANDS go to the
|
|
66
|
-
# daemon.
|
|
78
|
+
# daemon.
|
|
67
79
|
#
|
|
68
80
|
# @param [String] application the application's name
|
|
69
81
|
# @param [String, Symbol] command the command
|
|
@@ -71,6 +83,10 @@ module Bluepill
|
|
|
71
83
|
#
|
|
72
84
|
# @return [void]
|
|
73
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
|
+
#
|
|
74
90
|
# @api private
|
|
75
91
|
def handle_command(application, command, *args)
|
|
76
92
|
case command.to_sym
|
|
@@ -87,9 +103,7 @@ module Bluepill
|
|
|
87
103
|
|
|
88
104
|
# Send a command to an application's daemon, and return its answer
|
|
89
105
|
#
|
|
90
|
-
# It first checks that the daemon runs this version of bluepill.
|
|
91
|
-
# prints an error that the daemon raised and exits with status 8, and
|
|
92
|
-
# aborts if the daemon isn't running.
|
|
106
|
+
# It first checks that the daemon runs this version of bluepill.
|
|
93
107
|
#
|
|
94
108
|
# @param [String] application the application's name
|
|
95
109
|
# @param [String, Symbol] command the command
|
|
@@ -97,16 +111,22 @@ module Bluepill
|
|
|
97
111
|
#
|
|
98
112
|
# @return [Object] what the daemon answered
|
|
99
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
|
+
#
|
|
100
119
|
# @api private
|
|
101
120
|
def send_to_daemon(application, command, *args)
|
|
102
121
|
verify_version!(application)
|
|
103
122
|
|
|
104
123
|
command = [command, *args].join(":")
|
|
105
124
|
response = Socket.client_command(base_dir, application, command)
|
|
106
|
-
|
|
125
|
+
raise DaemonError, response if response.is_a?(Exception)
|
|
126
|
+
|
|
107
127
|
response
|
|
108
128
|
rescue Errno::ECONNREFUSED
|
|
109
|
-
|
|
129
|
+
raise ConnectionError, "Connection Refused: Server is not running"
|
|
110
130
|
end
|
|
111
131
|
|
|
112
132
|
# Return the pattern that finds a process or group's lines in the log
|
|
@@ -144,7 +164,7 @@ module Bluepill
|
|
|
144
164
|
# Tail the daemon's log, showing only a process or group's lines
|
|
145
165
|
#
|
|
146
166
|
# It falls back to the controller's log_file when the daemon doesn't
|
|
147
|
-
# name one, and replaces the CLI with tail.
|
|
167
|
+
# name one, and replaces the CLI with tail piped through grep.
|
|
148
168
|
#
|
|
149
169
|
# @param [String] application the application's name
|
|
150
170
|
# @param [String, nil] requested_pattern the process or group
|
|
@@ -153,31 +173,27 @@ module Bluepill
|
|
|
153
173
|
#
|
|
154
174
|
# @api private
|
|
155
175
|
def tail_log(application, requested_pattern)
|
|
156
|
-
log_file_location = send_to_daemon(application, :log_file)
|
|
157
|
-
log_file_location = log_file if log_file_location.
|
|
158
|
-
|
|
159
|
-
pattern = grep_pattern(application, requested_pattern)
|
|
176
|
+
log_file_location = send_to_daemon(application, :log_file).to_s
|
|
177
|
+
log_file_location = log_file.to_s if log_file_location.rstrip.empty?
|
|
160
178
|
|
|
161
|
-
tail = "tail -n 100 -f #{log_file_location} | grep -E '#{pattern}'"
|
|
162
179
|
puts "Tailing log for #{requested_pattern}..."
|
|
163
|
-
Kernel.exec(
|
|
180
|
+
Kernel.exec("sh", "-c", TAIL_COMMAND, "sh", log_file_location, grep_pattern(application, requested_pattern))
|
|
164
181
|
end
|
|
165
182
|
|
|
166
183
|
# Send one of the PROCESS_COMMANDS to the daemon and print what it did
|
|
167
184
|
#
|
|
168
|
-
# It exits with status 1 for any other command.
|
|
169
|
-
#
|
|
170
185
|
# @param [String] application the application's name
|
|
171
186
|
# @param [String, Symbol] command the command
|
|
172
187
|
# @param [Array<String>] args the group or process, for example
|
|
173
188
|
#
|
|
174
189
|
# @return [void]
|
|
175
190
|
#
|
|
191
|
+
# @raise [UnknownCommandError] for any other command
|
|
192
|
+
#
|
|
176
193
|
# @api private
|
|
177
194
|
def send_process_command(application, command, *args)
|
|
178
195
|
unless Application::PROCESS_COMMANDS.include?(command.to_sym)
|
|
179
|
-
|
|
180
|
-
exit(1)
|
|
196
|
+
raise UnknownCommandError, format("Unknown command `%s` (or application `%s` has not been loaded yet)", command, command)
|
|
181
197
|
end
|
|
182
198
|
|
|
183
199
|
# these need to be sent to the daemon and the results printed out
|
|
@@ -203,33 +219,25 @@ module Bluepill
|
|
|
203
219
|
end
|
|
204
220
|
end
|
|
205
221
|
|
|
206
|
-
#
|
|
222
|
+
# Check that an application's daemon runs this version of bluepill
|
|
207
223
|
#
|
|
208
|
-
#
|
|
209
|
-
#
|
|
210
|
-
# @return [void]
|
|
211
|
-
#
|
|
212
|
-
# @api private
|
|
213
|
-
def exit_with_daemon_error(error)
|
|
214
|
-
$stderr.puts "Received error from server:"
|
|
215
|
-
$stderr.puts error.inspect
|
|
216
|
-
# join flattens the backtrace and makes a missing one empty
|
|
217
|
-
$stderr.puts [error.backtrace].join("\n")
|
|
218
|
-
exit(8)
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
# Abort unless an application's daemon runs this version of bluepill
|
|
224
|
+
# A daemon older than 0.3.0 answers with its Version module, which
|
|
225
|
+
# Marshal loads as the CLI's own, so it can't say which version it runs.
|
|
222
226
|
#
|
|
223
227
|
# @param [String] application the application's name
|
|
224
228
|
#
|
|
225
229
|
# @return [void]
|
|
226
230
|
#
|
|
231
|
+
# @raise [VersionMismatchError] if it runs another version
|
|
232
|
+
#
|
|
227
233
|
# @api private
|
|
228
234
|
def verify_version!(application)
|
|
229
235
|
version = Socket.client_command(base_dir, application, "version")
|
|
230
|
-
|
|
236
|
+
return if Version.to_s.eql?(version)
|
|
237
|
+
|
|
238
|
+
raise VersionMismatchError, version.instance_of?(String) ? "#{OUT_OF_DATE}\nDaemon Version: #{version}, CLI Version: #{Version}" : OUT_OF_DATE
|
|
231
239
|
rescue ArgumentError
|
|
232
|
-
|
|
240
|
+
raise VersionMismatchError, OUT_OF_DATE
|
|
233
241
|
end
|
|
234
242
|
end
|
|
235
243
|
end
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "bluepill/dsl/attributes"
|
|
4
|
+
require "bluepill/process/attributes"
|
|
5
|
+
|
|
3
6
|
module Bluepill
|
|
4
7
|
# The self of the block given to {Bluepill.application}
|
|
5
8
|
#
|
|
@@ -14,86 +17,88 @@ module Bluepill
|
|
|
14
17
|
#
|
|
15
18
|
# @api public
|
|
16
19
|
class AppProxy
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
include DSL::Attributes
|
|
21
|
+
|
|
22
|
+
# Every process of the application gets these, unless it sets its own
|
|
23
|
+
attribute(*Process::Attributes::APP_ATTRIBUTES)
|
|
20
24
|
|
|
21
|
-
#
|
|
25
|
+
# @!attribute [rw] working_dir
|
|
26
|
+
# The directory that the application's processes start in
|
|
22
27
|
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
28
|
+
# @example
|
|
29
|
+
# app.working_dir = '/srv/my_app'
|
|
25
30
|
#
|
|
26
|
-
#
|
|
31
|
+
# @return [String, nil] the directory, or nil for the default
|
|
27
32
|
#
|
|
28
|
-
#
|
|
29
|
-
attr_accessor :working_dir
|
|
33
|
+
# @api public
|
|
30
34
|
|
|
31
|
-
#
|
|
35
|
+
# @!attribute [rw] uid
|
|
36
|
+
# The user that the application's processes run as
|
|
32
37
|
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
38
|
+
# @example
|
|
39
|
+
# app.uid = 'deploy'
|
|
35
40
|
#
|
|
36
|
-
#
|
|
41
|
+
# @return [String, nil] the user's name, or nil for bluepill's own user
|
|
37
42
|
#
|
|
38
|
-
#
|
|
39
|
-
attr_accessor :uid
|
|
43
|
+
# @api public
|
|
40
44
|
|
|
41
|
-
#
|
|
45
|
+
# @!attribute [rw] gid
|
|
46
|
+
# The group that the application's processes run as
|
|
42
47
|
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
48
|
+
# @example
|
|
49
|
+
# app.gid = 'deploy'
|
|
45
50
|
#
|
|
46
|
-
#
|
|
51
|
+
# @return [String, nil] the group's name, or nil for bluepill's own
|
|
52
|
+
# group
|
|
47
53
|
#
|
|
48
|
-
#
|
|
49
|
-
attr_accessor :gid
|
|
54
|
+
# @api public
|
|
50
55
|
|
|
51
|
-
#
|
|
56
|
+
# @!attribute [rw] supplementary_groups
|
|
57
|
+
# More groups that the application's processes run with
|
|
52
58
|
#
|
|
53
|
-
#
|
|
54
|
-
# are added to those.
|
|
59
|
+
# The processes already get every group that their uid belongs to.
|
|
60
|
+
# These are added to those.
|
|
55
61
|
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
62
|
+
# @example
|
|
63
|
+
# app.supplementary_groups = ['rvm', 'ssl-cert']
|
|
58
64
|
#
|
|
59
|
-
#
|
|
65
|
+
# @return [Array<String>, nil] the groups' names
|
|
60
66
|
#
|
|
61
|
-
#
|
|
62
|
-
attr_accessor :supplementary_groups
|
|
67
|
+
# @api public
|
|
63
68
|
|
|
64
|
-
#
|
|
69
|
+
# @!attribute [rw] environment
|
|
70
|
+
# The environment variables that the application's processes get
|
|
65
71
|
#
|
|
66
|
-
#
|
|
67
|
-
#
|
|
72
|
+
# @example
|
|
73
|
+
# app.environment = {'RAILS_ENV' => 'production'}
|
|
68
74
|
#
|
|
69
|
-
#
|
|
75
|
+
# @return [Hash{#to_s => #to_s}, nil] the variables by name
|
|
70
76
|
#
|
|
71
|
-
#
|
|
72
|
-
attr_accessor :environment
|
|
77
|
+
# @api public
|
|
73
78
|
|
|
74
|
-
#
|
|
79
|
+
# @!attribute [rw] auto_start
|
|
80
|
+
# Whether the application's processes start when bluepill loads
|
|
75
81
|
#
|
|
76
|
-
#
|
|
77
|
-
#
|
|
82
|
+
# @example
|
|
83
|
+
# app.auto_start = false
|
|
78
84
|
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
85
|
+
# @return [Boolean, nil] false to leave the processes unmonitored
|
|
86
|
+
# until they're started
|
|
81
87
|
#
|
|
82
|
-
#
|
|
83
|
-
attr_accessor :auto_start
|
|
88
|
+
# @api public
|
|
84
89
|
|
|
85
|
-
#
|
|
90
|
+
# @!attribute [rw] notify_command
|
|
91
|
+
# The command the application's processes run on each change of state
|
|
86
92
|
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
93
|
+
# A process can set its own. See Process#notify_command for the
|
|
94
|
+
# environment variables it gets.
|
|
89
95
|
#
|
|
90
|
-
#
|
|
91
|
-
#
|
|
96
|
+
# @example
|
|
97
|
+
# app.notify_command = '/usr/local/bin/alert'
|
|
92
98
|
#
|
|
93
|
-
#
|
|
99
|
+
# @return [String, nil] the command, without a shell
|
|
94
100
|
#
|
|
95
|
-
#
|
|
96
|
-
attr_accessor :notify_command
|
|
101
|
+
# @api public
|
|
97
102
|
|
|
98
103
|
# Return the application that the config describes
|
|
99
104
|
#
|
|
@@ -110,16 +115,18 @@ module Bluepill
|
|
|
110
115
|
# @return [void]
|
|
111
116
|
#
|
|
112
117
|
# @api private
|
|
113
|
-
def initialize(app_name, options)
|
|
114
|
-
@
|
|
118
|
+
def initialize(app_name, **options)
|
|
119
|
+
@name = app_name.to_s
|
|
120
|
+
@app = Application.new(@name, **options)
|
|
121
|
+
@attributes = {}
|
|
115
122
|
@registry = ProcessFactory::Registry.new
|
|
116
123
|
end
|
|
117
124
|
|
|
118
125
|
# Define a process of the application
|
|
119
126
|
#
|
|
120
127
|
# The block runs with self set to a {ProcessProxy}, and also gets the
|
|
121
|
-
# proxy as an argument. The process starts with the application
|
|
122
|
-
# attributes, which the block can override.
|
|
128
|
+
# proxy as an argument. The process starts with the application
|
|
129
|
+
# attributes that are set, which the block can override.
|
|
123
130
|
#
|
|
124
131
|
# @example
|
|
125
132
|
# app.process('web') do |process|
|
|
@@ -138,12 +145,16 @@ module Bluepill
|
|
|
138
145
|
#
|
|
139
146
|
# @api public
|
|
140
147
|
def process(process_name, &process_block)
|
|
141
|
-
attributes = {} #: Hash[Symbol, untyped]
|
|
142
|
-
APP_ATTRIBUTES.each { |a| attributes[a] = public_send(a) }
|
|
143
|
-
|
|
144
148
|
process_factory = ProcessFactory.new(attributes, process_block, @registry)
|
|
145
|
-
process, group = process_factory.create_process(process_name, @app.pids_dir)
|
|
149
|
+
process, group = process_factory.create_process(process_name, @app.pids_dir, logger: @app.logger)
|
|
146
150
|
@app.add_process(process, group)
|
|
147
151
|
end
|
|
152
|
+
|
|
153
|
+
# Name the application, for an unknown attribute's error
|
|
154
|
+
#
|
|
155
|
+
# @return [String] the application's name
|
|
156
|
+
#
|
|
157
|
+
# @api private
|
|
158
|
+
def attribute_owner = "application '#{@name}'"
|
|
148
159
|
end
|
|
149
160
|
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "did_you_mean"
|
|
4
|
+
|
|
5
|
+
module Bluepill
|
|
6
|
+
# The parts that the pill file's application and process blocks share
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
module DSL
|
|
10
|
+
# The attributes that a pill file sets with `name = value`
|
|
11
|
+
#
|
|
12
|
+
# A class that includes it names its attributes with
|
|
13
|
+
# {ClassMethods#attribute}, which defines a writer and a reader for
|
|
14
|
+
# each. A reader returns nil for an attribute that isn't set. A block
|
|
15
|
+
# that {#evaluate} runs fails with a {ConfigError} that names the
|
|
16
|
+
# closest attribute for any other call on the proxy, so a misspelled
|
|
17
|
+
# attribute fails when the pill file loads instead of being ignored.
|
|
18
|
+
# The class sets @attributes to a Hash, and defines attribute_owner,
|
|
19
|
+
# which names it in the error.
|
|
20
|
+
#
|
|
21
|
+
# @api private
|
|
22
|
+
module Attributes
|
|
23
|
+
# Give a class that includes the module its attribute macro
|
|
24
|
+
#
|
|
25
|
+
# @param [Class] base the class
|
|
26
|
+
#
|
|
27
|
+
# @return [void]
|
|
28
|
+
#
|
|
29
|
+
# @api private
|
|
30
|
+
def self.included(base)
|
|
31
|
+
base.extend(ClassMethods)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The attribute macro of a class that includes {Attributes}
|
|
35
|
+
#
|
|
36
|
+
# @api private
|
|
37
|
+
module ClassMethods
|
|
38
|
+
# Return the attributes that a pill file can set
|
|
39
|
+
#
|
|
40
|
+
# @return [Array<Symbol>] the attributes' names
|
|
41
|
+
#
|
|
42
|
+
# @api private
|
|
43
|
+
attr_reader :attribute_names
|
|
44
|
+
|
|
45
|
+
# Define a writer and a reader for each attribute
|
|
46
|
+
#
|
|
47
|
+
# @param [Array<Symbol>] names the attributes' names
|
|
48
|
+
#
|
|
49
|
+
# @return [void]
|
|
50
|
+
#
|
|
51
|
+
# @api private
|
|
52
|
+
def attribute(*names)
|
|
53
|
+
@attribute_names = names.freeze
|
|
54
|
+
names.each do |name|
|
|
55
|
+
define_method(name) do
|
|
56
|
+
# @type self: Attributes
|
|
57
|
+
attributes[name]
|
|
58
|
+
end
|
|
59
|
+
define_method(:"#{name}=") do |value|
|
|
60
|
+
# @type self: Attributes
|
|
61
|
+
attributes[name] = value
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Return the attributes that the pill file set, by name
|
|
68
|
+
#
|
|
69
|
+
# @return [Hash{Symbol => Object}] the attributes
|
|
70
|
+
#
|
|
71
|
+
# @api private
|
|
72
|
+
attr_reader :attributes
|
|
73
|
+
|
|
74
|
+
# Run a block of the pill file with self set to the proxy
|
|
75
|
+
#
|
|
76
|
+
# instance_eval also passes the proxy to a block that takes an
|
|
77
|
+
# argument. A call of a method the proxy doesn't have, such as a
|
|
78
|
+
# misspelled attribute, fails with a ConfigError instead, with the
|
|
79
|
+
# backtrace of the call, so it still points at the pill file's line.
|
|
80
|
+
# Any other NameError, such as one raised on another object, passes
|
|
81
|
+
# through as it is.
|
|
82
|
+
#
|
|
83
|
+
# @yield runs with self set to the proxy
|
|
84
|
+
# @yieldparam [Attributes] proxy the proxy
|
|
85
|
+
# @yieldreturn [void]
|
|
86
|
+
#
|
|
87
|
+
# @return [Object] what the block returns
|
|
88
|
+
#
|
|
89
|
+
# @raise [ConfigError] for a method the proxy doesn't have
|
|
90
|
+
#
|
|
91
|
+
# @api private
|
|
92
|
+
def evaluate(&block)
|
|
93
|
+
instance_eval(&block)
|
|
94
|
+
rescue NameError => e
|
|
95
|
+
raise unless e.receiver.equal?(self)
|
|
96
|
+
|
|
97
|
+
raise ConfigError, unknown_attribute_message(e.name), e.backtrace
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# Return the message for a call of an attribute that isn't there
|
|
103
|
+
#
|
|
104
|
+
# It names the attribute and what it was called on, and the closest
|
|
105
|
+
# attribute there is, when one is close enough.
|
|
106
|
+
#
|
|
107
|
+
# @param [Symbol] name the method's name
|
|
108
|
+
#
|
|
109
|
+
# @return [String] the message
|
|
110
|
+
#
|
|
111
|
+
# @api private
|
|
112
|
+
def unknown_attribute_message(name)
|
|
113
|
+
attribute = name.to_s.delete_suffix("=")
|
|
114
|
+
message = "Unknown attribute '#{attribute}' for #{attribute_owner}"
|
|
115
|
+
suggestion = suggest(attribute)
|
|
116
|
+
suggestion ? "#{message}. Did you mean '#{suggestion}'?" : message
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Return the attribute that's closest to a misspelled one
|
|
120
|
+
#
|
|
121
|
+
# Ruby's did_you_mean picks it, as it picks a method for a misspelled
|
|
122
|
+
# one.
|
|
123
|
+
#
|
|
124
|
+
# @param [String] attribute the misspelled attribute
|
|
125
|
+
#
|
|
126
|
+
# @return [String, nil] the closest attribute, or nil
|
|
127
|
+
#
|
|
128
|
+
# @api private
|
|
129
|
+
def suggest(attribute)
|
|
130
|
+
names = self.class.attribute_names.collect(&:to_s)
|
|
131
|
+
DidYouMean::SpellChecker.new(dictionary: names).correct(attribute).first
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
class ProcessFactory
|
|
5
|
+
# Create the processes of the children that a process finds
|
|
6
|
+
#
|
|
7
|
+
# A process that monitors its children gets one, with the block that
|
|
8
|
+
# configures each child. Process asks it for a child's process when it
|
|
9
|
+
# finds a new child, long after the config loaded, so the factory
|
|
10
|
+
# checks the block when it's made: it runs the block once, as for a
|
|
11
|
+
# child whose pid isn't known yet, so actual_pid is nil, and fails on a
|
|
12
|
+
# mistake. It runs the block again for each child it creates.
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
class ChildFactory
|
|
16
|
+
# Create a factory of a process's children, and check their config
|
|
17
|
+
#
|
|
18
|
+
# @param [String] parent_name the name of the process whose children
|
|
19
|
+
# it creates
|
|
20
|
+
# @param [Hash{Symbol => Object}] parent_attributes the process's
|
|
21
|
+
# attributes, of which the children get the CHILD_ATTRIBUTES
|
|
22
|
+
# @param [Proc] child_process_block the block that configures each
|
|
23
|
+
# child
|
|
24
|
+
#
|
|
25
|
+
# @return [void]
|
|
26
|
+
#
|
|
27
|
+
# @raise [ConfigError] for a mistake in the children's config
|
|
28
|
+
#
|
|
29
|
+
# @api private
|
|
30
|
+
def initialize(parent_name, parent_attributes, child_process_block)
|
|
31
|
+
# An attribute the parent leaves unset keeps the Process default
|
|
32
|
+
@attributes = parent_attributes.slice(*Process::Attributes::CHILD_ATTRIBUTES)
|
|
33
|
+
@child_process_block = child_process_block
|
|
34
|
+
validate!(parent_name)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Create the process of a child that bluepill found
|
|
38
|
+
#
|
|
39
|
+
# The child gets its parent's grace times, the parent's user and
|
|
40
|
+
# groups, so its commands run as the parent's do, and its state from
|
|
41
|
+
# whether it's running.
|
|
42
|
+
#
|
|
43
|
+
# @param [String] name the child's name
|
|
44
|
+
# @param [Integer] pid the child's pid
|
|
45
|
+
# @param [Logger] logger the child's logger
|
|
46
|
+
#
|
|
47
|
+
# @return [Process] the child's process
|
|
48
|
+
#
|
|
49
|
+
# @api private
|
|
50
|
+
def create_child_process(name, pid, logger)
|
|
51
|
+
child = ProcessProxy.new(name, @attributes, @child_process_block, actual_pid: pid)
|
|
52
|
+
process = child.to_process(logger: logger)
|
|
53
|
+
process.determine_initial_state
|
|
54
|
+
process
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
# Run the block for a child whose pid isn't known, and check it
|
|
60
|
+
#
|
|
61
|
+
# The block must set a stop_command. Making the child's process also
|
|
62
|
+
# makes its checks, which fail on a check or an option that bluepill
|
|
63
|
+
# doesn't have.
|
|
64
|
+
#
|
|
65
|
+
# @param [String] parent_name the name of the children's parent
|
|
66
|
+
#
|
|
67
|
+
# @return [void]
|
|
68
|
+
#
|
|
69
|
+
# @raise [ConfigError] for a mistake in the children's config
|
|
70
|
+
#
|
|
71
|
+
# @api private
|
|
72
|
+
def validate!(parent_name)
|
|
73
|
+
child = ProcessProxy.new("<child of #{parent_name}>", @attributes, @child_process_block)
|
|
74
|
+
raise ConfigError, "Config Error: You must specify a stop_command for the children of '#{parent_name}'" unless child.stop_command
|
|
75
|
+
|
|
76
|
+
child.to_process
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
3
5
|
module Bluepill
|
|
4
6
|
class ProcessFactory
|
|
5
7
|
# The process names and pid files that an application's config has used
|
|
@@ -17,9 +19,8 @@ module Bluepill
|
|
|
17
19
|
#
|
|
18
20
|
# @api private
|
|
19
21
|
def initialize
|
|
20
|
-
|
|
21
|
-
@
|
|
22
|
-
@pid_files = {} #: Hash[String, nil]
|
|
22
|
+
@process_keys = Set.new #: Set[[String?, String]]
|
|
23
|
+
@pid_files = Set.new #: Set[String]
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
# Add a process's name in its group, unless it's there already
|
|
@@ -34,9 +35,7 @@ module Bluepill
|
|
|
34
35
|
# @return [Boolean] true for a new name, false for one already added
|
|
35
36
|
#
|
|
36
37
|
# @api private
|
|
37
|
-
def add_process?(group, name)
|
|
38
|
-
add?(@process_keys, [group, name].collect { |part| part && part.to_s })
|
|
39
|
-
end
|
|
38
|
+
def add_process?(group, name) = !@process_keys.add?([group&.to_s, name.to_s]).nil?
|
|
40
39
|
|
|
41
40
|
# Add a pid file, unless it's there already
|
|
42
41
|
#
|
|
@@ -46,26 +45,7 @@ module Bluepill
|
|
|
46
45
|
# added
|
|
47
46
|
#
|
|
48
47
|
# @api private
|
|
49
|
-
def add_pid_file?(pid_file)
|
|
50
|
-
add?(@pid_files, pid_file)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
private
|
|
54
|
-
|
|
55
|
-
# Add a key to a hash, unless it's there already
|
|
56
|
-
#
|
|
57
|
-
# @param [Hash{Object => nil}] keys the keys added so far
|
|
58
|
-
# @param [Object] key the key to add
|
|
59
|
-
#
|
|
60
|
-
# @return [Boolean] true for a new key, false for one already added
|
|
61
|
-
#
|
|
62
|
-
# @api private
|
|
63
|
-
def add?(keys, key)
|
|
64
|
-
return false if keys.key?(key)
|
|
65
|
-
|
|
66
|
-
keys[key] = nil
|
|
67
|
-
true
|
|
68
|
-
end
|
|
48
|
+
def add_pid_file?(pid_file) = !@pid_files.add?(pid_file).nil?
|
|
69
49
|
end
|
|
70
50
|
end
|
|
71
51
|
end
|