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,4 +1,6 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "bluepill/system"
|
|
2
|
+
require "bluepill/process_journal/cleanup"
|
|
3
|
+
require "bluepill/process_journal/lock"
|
|
2
4
|
|
|
3
5
|
module Bluepill
|
|
4
6
|
# Remember the pids and process groups of the processes bluepill starts
|
|
@@ -23,6 +25,54 @@ module Bluepill
|
|
|
23
25
|
#
|
|
24
26
|
# @api private
|
|
25
27
|
module ProcessJournal
|
|
28
|
+
extend Cleanup
|
|
29
|
+
extend Lock
|
|
30
|
+
|
|
31
|
+
# What a journal holds: pids, or process group ids
|
|
32
|
+
#
|
|
33
|
+
# @!attribute name
|
|
34
|
+
# Return what the journal's log calls an id
|
|
35
|
+
#
|
|
36
|
+
# @return [String] pid or pgid
|
|
37
|
+
#
|
|
38
|
+
# @api private
|
|
39
|
+
#
|
|
40
|
+
# @!attribute label
|
|
41
|
+
# Return what the cleanup's log calls an id's process
|
|
42
|
+
#
|
|
43
|
+
# @return [String] process or process group
|
|
44
|
+
#
|
|
45
|
+
# @api private
|
|
46
|
+
#
|
|
47
|
+
# @!attribute file_tag
|
|
48
|
+
# Return what the journal's filename says it holds
|
|
49
|
+
#
|
|
50
|
+
# @return [String] pids or pgids
|
|
51
|
+
#
|
|
52
|
+
# @api private
|
|
53
|
+
#
|
|
54
|
+
# @!attribute sign
|
|
55
|
+
# Return what an id is multiplied by to signal it
|
|
56
|
+
#
|
|
57
|
+
# A negative id signals the whole process group.
|
|
58
|
+
#
|
|
59
|
+
# @return [Integer] 1 for pids, or -1 for process groups
|
|
60
|
+
#
|
|
61
|
+
# @api private
|
|
62
|
+
#
|
|
63
|
+
# @api private
|
|
64
|
+
Kind = Struct.new(:name, :label, :file_tag, :sign)
|
|
65
|
+
|
|
66
|
+
# A pid journal's kind
|
|
67
|
+
#
|
|
68
|
+
# @api private
|
|
69
|
+
PID = Kind.new("pid".freeze, "process".freeze, "pids".freeze, 1).freeze
|
|
70
|
+
|
|
71
|
+
# A process group journal's kind
|
|
72
|
+
#
|
|
73
|
+
# @api private
|
|
74
|
+
PGID = Kind.new("pgid".freeze, "process group".freeze, "pgids".freeze, -1).freeze
|
|
75
|
+
|
|
26
76
|
class << self
|
|
27
77
|
# Return the logger the journal writes to
|
|
28
78
|
#
|
|
@@ -71,10 +121,10 @@ module Bluepill
|
|
|
71
121
|
#
|
|
72
122
|
# @api private
|
|
73
123
|
def setup(base_dir, app_name)
|
|
74
|
-
@legacy_journal_dir ||= File.join(base_dir,
|
|
124
|
+
@legacy_journal_dir ||= File.join(base_dir, "journals")
|
|
75
125
|
@journal_base_dir ||= File.join(@legacy_journal_dir, app_name.to_s)
|
|
76
126
|
[@legacy_journal_dir, @journal_base_dir].each do |dir|
|
|
77
|
-
FileUtils.mkdir_p(dir)
|
|
127
|
+
FileUtils.mkdir_p(dir)
|
|
78
128
|
FileUtils.chmod(0o777, dir)
|
|
79
129
|
end
|
|
80
130
|
end
|
|
@@ -90,7 +140,7 @@ module Bluepill
|
|
|
90
140
|
#
|
|
91
141
|
# @api private
|
|
92
142
|
def self.skip_pid?(pid)
|
|
93
|
-
|
|
143
|
+
skip_id?(pid)
|
|
94
144
|
end
|
|
95
145
|
|
|
96
146
|
# Return whether a process group id can't be journaled or signaled
|
|
@@ -101,63 +151,33 @@ module Bluepill
|
|
|
101
151
|
#
|
|
102
152
|
# @api private
|
|
103
153
|
def self.skip_pgid?(pgid)
|
|
104
|
-
|
|
154
|
+
skip_id?(pgid)
|
|
105
155
|
end
|
|
106
156
|
|
|
107
|
-
#
|
|
157
|
+
# Return whether an id can't be journaled or signaled
|
|
108
158
|
#
|
|
109
|
-
#
|
|
110
|
-
# filesystems and f.flock(File::LOCK_SH) is not available on all
|
|
111
|
-
# platforms. It waits up to 10 seconds for another process's lock.
|
|
159
|
+
# @param [Integer, nil] id the pid or process group id to check
|
|
112
160
|
#
|
|
113
|
-
# @
|
|
114
|
-
#
|
|
115
|
-
# @yield while holding the lock
|
|
116
|
-
# @yieldreturn [Object] what the method returns
|
|
117
|
-
#
|
|
118
|
-
# @return [Object] what the block returns
|
|
119
|
-
#
|
|
120
|
-
# @raise [RuntimeError] if the lock is still held after 10 tries
|
|
161
|
+
# @return [Boolean] true unless id is an Integer above 1
|
|
121
162
|
#
|
|
122
163
|
# @api private
|
|
123
|
-
def self.
|
|
124
|
-
|
|
125
|
-
times = 0
|
|
126
|
-
begin
|
|
127
|
-
Dir.mkdir lock, 0o700
|
|
128
|
-
rescue Errno::EEXIST
|
|
129
|
-
times += 1
|
|
130
|
-
if times < 10
|
|
131
|
-
logger.debug("Waiting for lock #{lock}")
|
|
132
|
-
sleep 1
|
|
133
|
-
retry
|
|
134
|
-
end
|
|
135
|
-
logger.info("Timeout waiting for lock #{lock}")
|
|
136
|
-
raise "Timeout waiting for lock #{lock}"
|
|
137
|
-
end
|
|
138
|
-
logger.debug("Acquired lock #{lock}")
|
|
139
|
-
begin
|
|
140
|
-
yield
|
|
141
|
-
ensure
|
|
142
|
-
clear_atomic_fs_lock(lock)
|
|
143
|
-
end
|
|
164
|
+
def self.skip_id?(id)
|
|
165
|
+
!id.is_a?(Integer) || id <= 1
|
|
144
166
|
end
|
|
145
167
|
|
|
146
|
-
#
|
|
168
|
+
# Return the path of a process's pid journal
|
|
147
169
|
#
|
|
148
|
-
#
|
|
149
|
-
#
|
|
170
|
+
# @param [String] journal_name the process's name
|
|
171
|
+
# @param [String] dir the journal directory
|
|
150
172
|
#
|
|
151
|
-
# @return [
|
|
173
|
+
# @return [String] the journal's path
|
|
152
174
|
#
|
|
153
175
|
# @api private
|
|
154
|
-
def self.
|
|
155
|
-
|
|
156
|
-
clear_atomic_fs_lock(lock)
|
|
157
|
-
end
|
|
176
|
+
def self.pid_journal_filename(journal_name, dir = journal_base_dir)
|
|
177
|
+
journal_filename(PID, journal_name, dir)
|
|
158
178
|
end
|
|
159
179
|
|
|
160
|
-
# Return the path of a process's
|
|
180
|
+
# Return the path of a process's process group journal
|
|
161
181
|
#
|
|
162
182
|
# @param [String] journal_name the process's name
|
|
163
183
|
# @param [String] dir the journal directory
|
|
@@ -165,20 +185,21 @@ module Bluepill
|
|
|
165
185
|
# @return [String] the journal's path
|
|
166
186
|
#
|
|
167
187
|
# @api private
|
|
168
|
-
def self.
|
|
169
|
-
|
|
188
|
+
def self.pgid_journal_filename(journal_name, dir = journal_base_dir)
|
|
189
|
+
journal_filename(PGID, journal_name, dir)
|
|
170
190
|
end
|
|
171
191
|
|
|
172
|
-
# Return the path of a process's
|
|
192
|
+
# Return the path of one of a process's journals
|
|
173
193
|
#
|
|
194
|
+
# @param [Kind] kind PID or PGID
|
|
174
195
|
# @param [String] journal_name the process's name
|
|
175
196
|
# @param [String] dir the journal directory
|
|
176
197
|
#
|
|
177
198
|
# @return [String] the journal's path
|
|
178
199
|
#
|
|
179
200
|
# @api private
|
|
180
|
-
def self.
|
|
181
|
-
File.join(dir, ".
|
|
201
|
+
def self.journal_filename(kind, journal_name, dir = journal_base_dir)
|
|
202
|
+
File.join(dir, ".bluepill_#{kind.file_tag}_journal.#{journal_name}")
|
|
182
203
|
end
|
|
183
204
|
|
|
184
205
|
# Return the ids in a journal, each with when its process started
|
|
@@ -208,7 +229,7 @@ module Bluepill
|
|
|
208
229
|
#
|
|
209
230
|
# @api private
|
|
210
231
|
def self.journal_line(id)
|
|
211
|
-
[id, System.started_at(id)].compact.join(
|
|
232
|
+
[id, System.started_at(id)].compact.join(" ")
|
|
212
233
|
end
|
|
213
234
|
|
|
214
235
|
# Return the valid pids in a pid journal
|
|
@@ -219,12 +240,7 @@ module Bluepill
|
|
|
219
240
|
#
|
|
220
241
|
# @api private
|
|
221
242
|
def self.pid_journal(filename)
|
|
222
|
-
|
|
223
|
-
result = journal_entries(filename).collect(&:first).reject { |pid| skip_pid?(pid) }
|
|
224
|
-
logger.debug("pid journal = #{result.join(' ')}")
|
|
225
|
-
result
|
|
226
|
-
rescue Errno::ENOENT
|
|
227
|
-
[]
|
|
243
|
+
journal_ids(PID, filename)
|
|
228
244
|
end
|
|
229
245
|
|
|
230
246
|
# Return the valid process group ids in a process group journal
|
|
@@ -236,233 +252,110 @@ module Bluepill
|
|
|
236
252
|
#
|
|
237
253
|
# @api private
|
|
238
254
|
def self.pgid_journal(filename)
|
|
239
|
-
|
|
240
|
-
result = journal_entries(filename).collect(&:first).reject { |pgid| skip_pgid?(pgid) }
|
|
241
|
-
logger.debug("pgid journal = #{result.join(' ')}")
|
|
242
|
-
result
|
|
243
|
-
rescue Errno::ENOENT
|
|
244
|
-
[]
|
|
255
|
+
journal_ids(PGID, filename)
|
|
245
256
|
end
|
|
246
257
|
|
|
247
|
-
#
|
|
258
|
+
# Return the valid ids in a journal
|
|
248
259
|
#
|
|
249
|
-
# @param [
|
|
260
|
+
# @param [Kind] kind PID or PGID
|
|
261
|
+
# @param [String] filename the journal's path
|
|
250
262
|
#
|
|
251
|
-
# @return [
|
|
263
|
+
# @return [Array<Integer>] the ids, or none if there's no journal
|
|
252
264
|
#
|
|
253
265
|
# @api private
|
|
254
|
-
def self.
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
266
|
+
def self.journal_ids(kind, filename)
|
|
267
|
+
logger.debug("#{kind.name} journal file: #{filename}")
|
|
268
|
+
result = journal_entries(filename).collect(&:first).reject { |id| skip_id?(id) }
|
|
269
|
+
logger.debug("#{kind.name} journal = #{result.join(" ")}")
|
|
270
|
+
result
|
|
271
|
+
rescue Errno::ENOENT
|
|
272
|
+
[]
|
|
259
273
|
end
|
|
260
274
|
|
|
261
|
-
#
|
|
275
|
+
# Add a process group id to a process's journal, unless it's there
|
|
262
276
|
#
|
|
263
277
|
# @param [String] journal_name the process's name
|
|
278
|
+
# @param [Integer, nil] pgid the process group id
|
|
264
279
|
#
|
|
265
280
|
# @return [void]
|
|
266
281
|
#
|
|
267
282
|
# @api private
|
|
268
|
-
def self.
|
|
269
|
-
|
|
270
|
-
kill_all_pgids_from_journal(journal_name)
|
|
283
|
+
def self.append_pgid_to_journal(journal_name, pgid)
|
|
284
|
+
append_to_journal(PGID, journal_name, pgid)
|
|
271
285
|
end
|
|
272
286
|
|
|
273
|
-
#
|
|
274
|
-
#
|
|
275
|
-
# That's the app's journal, and the one that bluepill 0.1 and earlier
|
|
276
|
-
# shared between apps.
|
|
287
|
+
# Add a pid, and its process group id, to a process's journals
|
|
277
288
|
#
|
|
278
289
|
# @param [String] journal_name the process's name
|
|
290
|
+
# @param [Integer, nil] pid the pid
|
|
279
291
|
#
|
|
280
292
|
# @return [void]
|
|
281
293
|
#
|
|
282
294
|
# @api private
|
|
283
|
-
def self.
|
|
284
|
-
|
|
285
|
-
filename = pgid_journal_filename(journal_name, dir)
|
|
286
|
-
kill_all_from_journal_file(filename, pgid_journal(filename), 'process group', -1)
|
|
287
|
-
end
|
|
295
|
+
def self.append_pid_to_journal(journal_name, pid)
|
|
296
|
+
append_to_journal(PID, journal_name, pid) { |valid_pid| append_process_group_to_journal(journal_name, valid_pid) }
|
|
288
297
|
end
|
|
289
298
|
|
|
290
|
-
#
|
|
291
|
-
#
|
|
292
|
-
# That's the app's journal, and the one that bluepill 0.1 and earlier
|
|
293
|
-
# shared between apps.
|
|
299
|
+
# Add a process's process group id to the process's journal
|
|
294
300
|
#
|
|
295
301
|
# @param [String] journal_name the process's name
|
|
302
|
+
# @param [Integer] pid the process's pid
|
|
296
303
|
#
|
|
297
304
|
# @return [void]
|
|
298
305
|
#
|
|
299
306
|
# @api private
|
|
300
|
-
def self.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
end
|
|
307
|
+
def self.append_process_group_to_journal(journal_name, pid)
|
|
308
|
+
append_pgid_to_journal(journal_name, ::Process.getpgid(pid))
|
|
309
|
+
rescue Errno::ESRCH
|
|
310
|
+
# The process is gone, so it has no process group to journal
|
|
305
311
|
end
|
|
306
312
|
|
|
307
|
-
#
|
|
308
|
-
#
|
|
309
|
-
# @return [Array<String>] the app's, and then the one before 0.2
|
|
313
|
+
# Add an id to one of a process's journals, unless it's invalid
|
|
310
314
|
#
|
|
311
|
-
# @
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
end
|
|
315
|
-
|
|
316
|
-
# Kill the ids a journal lists, and then delete the journal
|
|
317
|
-
#
|
|
318
|
-
# Only kills the ids that still belong to what bluepill journaled (see
|
|
319
|
-
# journaled_ids). Sends TERM, waits a second for any survivors, then
|
|
320
|
-
# sends KILL to those still alive. A negative sign targets process groups
|
|
321
|
-
# instead of pids.
|
|
315
|
+
# @param [Kind] kind PID or PGID
|
|
316
|
+
# @param [String] journal_name the process's name
|
|
317
|
+
# @param [Integer, nil] id the pid or process group id
|
|
322
318
|
#
|
|
323
|
-
# @
|
|
324
|
-
# @
|
|
325
|
-
# @param [String] label what the ids are, for the log
|
|
326
|
-
# @param [Integer] sign 1 for pids, or -1 for process groups
|
|
319
|
+
# @yield [id] once it's known to be valid, before it's journaled
|
|
320
|
+
# @yieldparam [Integer] id the id
|
|
327
321
|
#
|
|
328
322
|
# @return [void]
|
|
329
323
|
#
|
|
330
324
|
# @api private
|
|
331
|
-
def self.
|
|
332
|
-
if
|
|
333
|
-
logger.debug(
|
|
325
|
+
def self.append_to_journal(kind, journal_name, id)
|
|
326
|
+
if skip_id?(id)
|
|
327
|
+
logger.debug("Skipping invalid #{kind.name} #{id} (class #{id.class})")
|
|
334
328
|
return
|
|
335
329
|
end
|
|
330
|
+
# skip_id? made sure it's an Integer
|
|
331
|
+
valid_id = id #: Integer
|
|
332
|
+
yield valid_id if block_given?
|
|
336
333
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
signal_journaled('TERM', ids, label, sign)
|
|
340
|
-
survivors = ids.select { |id| System.pid_alive?(sign * id) }
|
|
341
|
-
unless survivors.empty?
|
|
342
|
-
sleep(1)
|
|
343
|
-
survivors = survivors.select { |id| System.pid_alive?(sign * id) }
|
|
344
|
-
signal_journaled('KILL', survivors, label, sign)
|
|
345
|
-
end
|
|
346
|
-
System.delete_if_exists(filename) # reset journal
|
|
347
|
-
logger.debug('Journal cleanup completed')
|
|
348
|
-
end
|
|
334
|
+
filename = journal_filename(kind, journal_name)
|
|
335
|
+
write_to_journal(filename, journal_name, kind.name, valid_id) { journal_ids(kind, filename) }
|
|
349
336
|
end
|
|
350
337
|
|
|
351
|
-
#
|
|
352
|
-
#
|
|
353
|
-
# An id whose process is running is kept only if that process started
|
|
354
|
-
# when the journal says, give or take 2 seconds, since ps's elapsed time
|
|
355
|
-
# is only accurate to a second. One whose process has exited (a process
|
|
356
|
-
# group whose leader is gone) is kept only if it was journaled since the
|
|
357
|
-
# machine booted, and so is one without a start time.
|
|
338
|
+
# Add a line for an id to a journal, under its lock, unless it's there
|
|
358
339
|
#
|
|
359
340
|
# @param [String] filename the journal's path
|
|
360
|
-
# @param [Array<Integer>] ids the valid ids in it
|
|
361
|
-
# @param [String] label what the ids are, for the log
|
|
362
|
-
#
|
|
363
|
-
# @return [Array<Integer>] the ids that are safe to signal, or none if
|
|
364
|
-
# the journal was cleared since its ids were read
|
|
365
|
-
#
|
|
366
|
-
# @api private
|
|
367
|
-
def self.journaled_ids(filename, ids, label)
|
|
368
|
-
# Journals only grow until they're cleared, under the lock this runs in
|
|
369
|
-
return [] unless File.exist?(filename)
|
|
370
|
-
|
|
371
|
-
started = Hash[journal_entries(filename)]
|
|
372
|
-
boot = System.boot_time
|
|
373
|
-
written = Integer(File.mtime(filename))
|
|
374
|
-
ids.select do |id|
|
|
375
|
-
now = System.started_at(id)
|
|
376
|
-
journaled = started.fetch(id)
|
|
377
|
-
ours = if now && journaled
|
|
378
|
-
(now - journaled).abs <= 2
|
|
379
|
-
else
|
|
380
|
-
!boot.nil? && (journaled || written) >= boot - 2
|
|
381
|
-
end
|
|
382
|
-
logger.info("Skipping old #{label} #{id}, which isn't the one bluepill started") unless ours
|
|
383
|
-
ours
|
|
384
|
-
end
|
|
385
|
-
end
|
|
386
|
-
|
|
387
|
-
# Send a signal to each journaled id, and log what happened
|
|
388
|
-
#
|
|
389
|
-
# @param [String] signal TERM or KILL
|
|
390
|
-
# @param [Array<Integer>] ids the pids or process group ids
|
|
391
|
-
# @param [String] label what the ids are, for the log
|
|
392
|
-
# @param [Integer] sign 1 for pids, or -1 for process groups
|
|
393
|
-
#
|
|
394
|
-
# @return [void]
|
|
395
|
-
#
|
|
396
|
-
# @api private
|
|
397
|
-
def self.signal_journaled(signal, ids, label, sign)
|
|
398
|
-
past, verb = signal.eql?('TERM') ? %w[Termed term] : %w[Killed kill]
|
|
399
|
-
ids.each do |id|
|
|
400
|
-
begin
|
|
401
|
-
::Process.kill(signal, sign * id)
|
|
402
|
-
logger.info("#{past} old #{label} #{id}")
|
|
403
|
-
rescue Errno::ESRCH
|
|
404
|
-
logger.debug("Unable to #{verb} missing #{label} #{id}")
|
|
405
|
-
end
|
|
406
|
-
end
|
|
407
|
-
end
|
|
408
|
-
|
|
409
|
-
# Add a process group id to a process's journal, unless it's there
|
|
410
|
-
#
|
|
411
341
|
# @param [String] journal_name the process's name
|
|
412
|
-
# @param [
|
|
342
|
+
# @param [String] name what the id is, pid or pgid, for the log
|
|
343
|
+
# @param [Integer] id the pid or process group id
|
|
413
344
|
#
|
|
414
|
-
# @
|
|
415
|
-
#
|
|
416
|
-
# @api private
|
|
417
|
-
def self.append_pgid_to_journal(journal_name, pgid)
|
|
418
|
-
if skip_pgid?(pgid)
|
|
419
|
-
logger.debug("Skipping invalid pgid #{pgid} (class #{pgid.class})")
|
|
420
|
-
return
|
|
421
|
-
end
|
|
422
|
-
# skip_pgid? made sure it's an Integer
|
|
423
|
-
valid_pgid = pgid #: Integer
|
|
424
|
-
|
|
425
|
-
filename = pgid_journal_filename(journal_name)
|
|
426
|
-
acquire_atomic_fs_lock(filename) do
|
|
427
|
-
if pgid_journal(filename).include?(pgid)
|
|
428
|
-
logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}")
|
|
429
|
-
else
|
|
430
|
-
logger.debug("Saving pgid #{pgid} to process journal #{journal_name}")
|
|
431
|
-
File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pgid)) }
|
|
432
|
-
logger.info("Saved pgid #{pgid} to journal #{journal_name}")
|
|
433
|
-
logger.debug("Journal now = #{File.read(filename)}")
|
|
434
|
-
end
|
|
435
|
-
end
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
# Add a pid, and its process group id, to a process's journals
|
|
439
|
-
#
|
|
440
|
-
# @param [String] journal_name the process's name
|
|
441
|
-
# @param [Integer, nil] pid the pid
|
|
345
|
+
# @yield under the lock, to read the journal
|
|
346
|
+
# @yieldreturn [Array<Integer>] the ids already in the journal
|
|
442
347
|
#
|
|
443
348
|
# @return [void]
|
|
444
349
|
#
|
|
445
350
|
# @api private
|
|
446
|
-
def self.
|
|
447
|
-
if skip_pid?(pid)
|
|
448
|
-
logger.debug("Skipping invalid pid #{pid} (class #{pid.class})")
|
|
449
|
-
return
|
|
450
|
-
end
|
|
451
|
-
# skip_pid? made sure it's an Integer
|
|
452
|
-
valid_pid = pid #: Integer
|
|
453
|
-
begin
|
|
454
|
-
append_pgid_to_journal(journal_name, ::Process.getpgid(valid_pid))
|
|
455
|
-
rescue Errno::ESRCH
|
|
456
|
-
end
|
|
457
|
-
|
|
458
|
-
filename = pid_journal_filename(journal_name)
|
|
351
|
+
def self.write_to_journal(filename, journal_name, name, id)
|
|
459
352
|
acquire_atomic_fs_lock(filename) do
|
|
460
|
-
if
|
|
461
|
-
logger.debug("Skipping duplicate
|
|
353
|
+
if yield.include?(id)
|
|
354
|
+
logger.debug("Skipping duplicate #{name} #{id} already in journal #{journal_name}")
|
|
462
355
|
else
|
|
463
|
-
logger.debug("Saving
|
|
464
|
-
File.open(filename,
|
|
465
|
-
logger.info("Saved
|
|
356
|
+
logger.debug("Saving #{name} #{id} to process journal #{journal_name}")
|
|
357
|
+
File.open(filename, "a+", 0o600) { |f| f.puts(journal_line(id)) }
|
|
358
|
+
logger.info("Saved #{name} #{id} to journal #{journal_name}")
|
|
466
359
|
logger.debug("Journal now = #{File.read(filename)}")
|
|
467
360
|
end
|
|
468
361
|
end
|
|
@@ -4,7 +4,7 @@ module Bluepill
|
|
|
4
4
|
# @api private
|
|
5
5
|
class ProcessStatistics
|
|
6
6
|
# How status shows each event's time
|
|
7
|
-
STRFTIME =
|
|
7
|
+
STRFTIME = "%m/%d/%Y %H:%M:%S".freeze
|
|
8
8
|
|
|
9
9
|
# How many events are kept
|
|
10
10
|
EVENTS_TO_PERSIST = 10
|
|
@@ -46,7 +46,7 @@ module Bluepill
|
|
|
46
46
|
# @api private
|
|
47
47
|
def to_s
|
|
48
48
|
str = events.reverse.collect do |event, reason, time|
|
|
49
|
-
" #{event} at #{time.strftime(STRFTIME)} - #{reason ||
|
|
49
|
+
" #{event} at #{time.strftime(STRFTIME)} - #{reason || "unspecified"}"
|
|
50
50
|
end.join("\n")
|
|
51
51
|
|
|
52
52
|
"event history:\n#{str}"
|
data/lib/bluepill/socket.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "socket"
|
|
2
|
+
require "bluepill/base_directory"
|
|
2
3
|
|
|
3
4
|
module Bluepill
|
|
4
5
|
# The socket that the CLI sends a daemon its commands over
|
|
@@ -44,21 +45,48 @@ module Bluepill
|
|
|
44
45
|
res = nil
|
|
45
46
|
MAX_ATTEMPTS.times do |current_attempt|
|
|
46
47
|
begin
|
|
47
|
-
|
|
48
|
-
Timeout.timeout(TIMEOUT) do
|
|
49
|
-
socket.puts command
|
|
50
|
-
res = Marshal.load(socket.read)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
48
|
+
res = send_command(base_dir, name, command)
|
|
53
49
|
break
|
|
54
50
|
rescue EOFError, Timeout::Error
|
|
55
|
-
|
|
56
|
-
puts "Retry #{current_attempt + 1} of #{MAX_ATTEMPTS}"
|
|
51
|
+
retry_or_abort(current_attempt)
|
|
57
52
|
end
|
|
58
53
|
end
|
|
59
54
|
res
|
|
60
55
|
end
|
|
61
56
|
|
|
57
|
+
# Send a command to an application's daemon once, and return its answer
|
|
58
|
+
#
|
|
59
|
+
# It raises Timeout::Error if the daemon takes longer than TIMEOUT, and
|
|
60
|
+
# EOFError if it hangs up.
|
|
61
|
+
#
|
|
62
|
+
# @param [String] base_dir the directory with the daemons' sockets
|
|
63
|
+
# @param [String] name the application's name
|
|
64
|
+
# @param [String] command the command, such as start:group:process
|
|
65
|
+
#
|
|
66
|
+
# @return [Object] what the daemon answered, unmarshaled
|
|
67
|
+
#
|
|
68
|
+
# @api private
|
|
69
|
+
def self.send_command(base_dir, name, command)
|
|
70
|
+
client(base_dir, name) do |socket|
|
|
71
|
+
Timeout.timeout(TIMEOUT) do
|
|
72
|
+
socket.puts command
|
|
73
|
+
Marshal.load(socket.read)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Say that a client command tries again, or abort after the last attempt
|
|
79
|
+
#
|
|
80
|
+
# @param [Integer] current_attempt the attempt that failed, from 0
|
|
81
|
+
#
|
|
82
|
+
# @return [void]
|
|
83
|
+
#
|
|
84
|
+
# @api private
|
|
85
|
+
def self.retry_or_abort(current_attempt)
|
|
86
|
+
abort("Socket Timeout: Server may not be responding") if current_attempt.eql?(MAX_ATTEMPTS - 1)
|
|
87
|
+
puts "Retry #{current_attempt + 1} of #{MAX_ATTEMPTS}"
|
|
88
|
+
end
|
|
89
|
+
|
|
62
90
|
# Open the socket that an application's daemon listens on
|
|
63
91
|
#
|
|
64
92
|
# It replaces a socket file that no daemon answers on, and exits with
|
|
@@ -75,16 +103,28 @@ module Bluepill
|
|
|
75
103
|
path = socket_path(base_dir, name)
|
|
76
104
|
UNIXServer.open(path)
|
|
77
105
|
rescue Errno::EADDRINUSE
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
106
|
+
replace_stale_socket(path)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Open a daemon's socket in place of a socket file that already exists
|
|
110
|
+
#
|
|
111
|
+
# It deletes the file and opens the socket if no daemon answers on it,
|
|
112
|
+
# and exits with status 7 if a daemon does.
|
|
113
|
+
#
|
|
114
|
+
# @param [String] path the socket file
|
|
115
|
+
#
|
|
116
|
+
# @return [UNIXServer]
|
|
117
|
+
#
|
|
118
|
+
# @api private
|
|
119
|
+
def self.replace_stale_socket(path)
|
|
120
|
+
# if sock file has been created, test to see if there is a server
|
|
121
|
+
UNIXSocket.open(path)
|
|
122
|
+
rescue Errno::ECONNREFUSED
|
|
123
|
+
File.delete(path)
|
|
124
|
+
UNIXServer.open(path)
|
|
125
|
+
else
|
|
126
|
+
$stderr.puts("Server is already running!")
|
|
127
|
+
exit(7)
|
|
88
128
|
end
|
|
89
129
|
|
|
90
130
|
# Return the path of an application's socket
|
|
@@ -96,7 +136,7 @@ module Bluepill
|
|
|
96
136
|
#
|
|
97
137
|
# @api private
|
|
98
138
|
def self.socket_path(base_dir, name)
|
|
99
|
-
|
|
139
|
+
BaseDirectory.new(base_dir).socket_file(name)
|
|
100
140
|
end
|
|
101
141
|
end
|
|
102
142
|
end
|