bluepill 0.2.9 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -2
  3. data/README.md +16 -13
  4. data/bin/bluepill +21 -8
  5. data/bluepill.gemspec +7 -2
  6. data/lib/bluepill/application/lifecycle.rb +48 -35
  7. data/lib/bluepill/application/listener.rb +2 -4
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +36 -30
  10. data/lib/bluepill/base_directory.rb +18 -20
  11. data/lib/bluepill/check_registry.rb +104 -0
  12. data/lib/bluepill/clock.rb +1 -3
  13. data/lib/bluepill/condition_watch.rb +46 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +35 -37
  16. data/lib/bluepill/daemonize.rb +52 -0
  17. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  18. data/lib/bluepill/dsl/attributes.rb +135 -0
  19. data/lib/bluepill/dsl/process_factory/child_factory.rb +83 -0
  20. data/lib/bluepill/dsl/process_factory/registry.rb +4 -26
  21. data/lib/bluepill/dsl/process_factory.rb +85 -84
  22. data/lib/bluepill/dsl/process_proxy.rb +80 -160
  23. data/lib/bluepill/dsl.rb +16 -15
  24. data/lib/bluepill/errors.rb +190 -0
  25. data/lib/bluepill/group.rb +14 -37
  26. data/lib/bluepill/logger.rb +65 -99
  27. data/lib/bluepill/process/attributes.rb +66 -32
  28. data/lib/bluepill/process/children.rb +6 -7
  29. data/lib/bluepill/process/pid.rb +43 -35
  30. data/lib/bluepill/process/state_machine.rb +15 -59
  31. data/lib/bluepill/process/stop_signals.rb +123 -0
  32. data/lib/bluepill/process/transition.rb +1 -3
  33. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  34. data/lib/bluepill/process/triggers.rb +90 -0
  35. data/lib/bluepill/process/user_commands.rb +1 -1
  36. data/lib/bluepill/process/watches.rb +9 -23
  37. data/lib/bluepill/process.rb +55 -52
  38. data/lib/bluepill/process_commands/execution.rb +16 -24
  39. data/lib/bluepill/process_commands/hooks.rb +1 -31
  40. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  41. data/lib/bluepill/process_commands.rb +10 -8
  42. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  43. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  44. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  45. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  46. data/lib/bluepill/process_conditions/http.rb +29 -67
  47. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  48. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  49. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  50. data/lib/bluepill/process_conditions.rb +5 -14
  51. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  52. data/lib/bluepill/process_journal/config.rb +73 -0
  53. data/lib/bluepill/process_journal/entries.rb +128 -0
  54. data/lib/bluepill/process_journal/journal.rb +69 -0
  55. data/lib/bluepill/process_journal/kind.rb +60 -0
  56. data/lib/bluepill/process_journal/lock.rb +9 -4
  57. data/lib/bluepill/process_journal.rb +13 -324
  58. data/lib/bluepill/process_statistics.rb +3 -7
  59. data/lib/bluepill/socket.rb +19 -17
  60. data/lib/bluepill/system/command_options.rb +136 -0
  61. data/lib/bluepill/system/command_result.rb +73 -0
  62. data/lib/bluepill/system/command_setup.rb +3 -4
  63. data/lib/bluepill/system/command_spawn.rb +244 -0
  64. data/lib/bluepill/system/commands.rb +27 -40
  65. data/lib/bluepill/system/daemon.rb +97 -15
  66. data/lib/bluepill/system/files.rb +35 -0
  67. data/lib/bluepill/system/ps.rb +20 -17
  68. data/lib/bluepill/system/ps_table.rb +7 -9
  69. data/lib/bluepill/system/usage.rb +175 -0
  70. data/lib/bluepill/system.rb +7 -192
  71. data/lib/bluepill/trigger.rb +18 -69
  72. data/lib/bluepill/triggers/flapping.rb +12 -8
  73. data/lib/bluepill/units.rb +7 -21
  74. data/lib/bluepill/version.rb +16 -26
  75. data/lib/bluepill.rb +1 -1
  76. data/sig/bluepill/application.rbs +11 -12
  77. data/sig/bluepill/attributes.rbs +48 -0
  78. data/sig/bluepill/checks.rbs +40 -40
  79. data/sig/bluepill/dsl.rbs +100 -59
  80. data/sig/bluepill/errors.rbs +48 -0
  81. data/sig/bluepill/logger.rbs +15 -31
  82. data/sig/bluepill/process.rbs +39 -305
  83. data/sig/bluepill/process_commands.rbs +235 -0
  84. data/sig/bluepill/stop_signals.rbs +21 -0
  85. data/sig/bluepill/system.rbs +191 -101
  86. data/sig/bluepill/version.rbs +17 -0
  87. data/sig/bluepill.rbs +2 -17
  88. data/sig/manifest.yaml +1 -0
  89. metadata +22 -8
  90. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  91. data/lib/bluepill/deprecation/dsl.rb +0 -141
  92. data/lib/bluepill/deprecation/options.rb +0 -140
  93. data/lib/bluepill/deprecation.rb +0 -92
  94. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  95. data/sig/bluepill/deprecation.rbs +0 -87
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module ProcessJournal
5
+ # Read and write the ids in a process's journals
6
+ #
7
+ # ProcessJournal extends it.
8
+ #
9
+ # @api private
10
+ module Entries
11
+ # Return a process's journals
12
+ #
13
+ # @param [#to_s] name the process's name
14
+ #
15
+ # @return [Journal]
16
+ #
17
+ # @api private
18
+ def for(name) = Journal.new(name)
19
+
20
+ # Return whether an id can't be journaled or signaled
21
+ #
22
+ # An id of 1 or less would reach init, or a whole process group.
23
+ #
24
+ # @param [Integer, nil] id the pid or process group id to check
25
+ #
26
+ # @return [Boolean] true unless id is an Integer above 1
27
+ #
28
+ # @api private
29
+ def skip_id?(id) = !id.is_a?(Integer) || id <= 1
30
+
31
+ # Return the ids in a journal, each with when its process started
32
+ #
33
+ # @param [String] filename the journal's path
34
+ #
35
+ # @return [Array<Array(Integer, Integer)>, Array<Array(Integer, nil)>]
36
+ # each id, with when its process started, or nil for a line from
37
+ # bluepill 0.1 and earlier, which has none
38
+ #
39
+ # @raise [Errno::ENOENT] if there's no journal
40
+ #
41
+ # @api private
42
+ def journal_entries(filename)
43
+ File.readlines(filename).collect do |line|
44
+ id, started = line.split
45
+ [id.to_i, started&.to_i]
46
+ end
47
+ end
48
+
49
+ # Return a journal line for an id: the id, and when its process started
50
+ #
51
+ # @param [Integer] id a pid, or a process group id, whose leader's start
52
+ # is recorded
53
+ #
54
+ # @return [String] the line, without a start time if ps can't tell
55
+ #
56
+ # @api private
57
+ def journal_line(id) = [id, System.started_at(id)].compact.join(" ")
58
+
59
+ # Return the valid ids in a journal
60
+ #
61
+ # @param [Kind] kind PID or PGID
62
+ # @param [String] filename the journal's path
63
+ #
64
+ # @return [Array<Integer>] the ids, or none if there's no journal
65
+ #
66
+ # @api private
67
+ def journal_ids(kind, filename)
68
+ logger.debug("#{kind.name} journal file: #{filename}")
69
+ result = journal_entries(filename).collect(&:first).reject { |id| skip_id?(id) }
70
+ logger.debug("#{kind.name} journal = #{result.join(" ")}")
71
+ result
72
+ rescue Errno::ENOENT
73
+ []
74
+ end
75
+
76
+ # Add an id to one of a process's journals, unless it's invalid
77
+ #
78
+ # @param [Kind] kind PID or PGID
79
+ # @param [#to_s] journal_name the process's name
80
+ # @param [Integer, nil] id the pid or process group id
81
+ #
82
+ # @yield [id] once it's known to be valid, before it's journaled
83
+ # @yieldparam [Integer] id the id
84
+ #
85
+ # @return [void]
86
+ #
87
+ # @api private
88
+ def append(kind, journal_name, id)
89
+ if skip_id?(id)
90
+ logger.debug("Skipping invalid #{kind.name} #{id} (class #{id.class})")
91
+ return
92
+ end
93
+ # skip_id? made sure it's an Integer
94
+ valid_id = id #: Integer
95
+ yield valid_id if block_given?
96
+
97
+ filename = kind.filename(journal_base_dir, journal_name)
98
+ write_to_journal(filename, journal_name, kind.name, valid_id) { journal_ids(kind, filename) }
99
+ end
100
+
101
+ # Add a line for an id to a journal, under its lock, unless it's there
102
+ #
103
+ # @param [String] filename the journal's path
104
+ # @param [#to_s] journal_name the process's name
105
+ # @param [String] name what the id is, pid or pgid, for the log
106
+ # @param [Integer] id the pid or process group id
107
+ #
108
+ # @yield under the lock, to read the journal
109
+ # @yieldreturn [Array<Integer>] the ids already in the journal
110
+ #
111
+ # @return [void]
112
+ #
113
+ # @api private
114
+ def write_to_journal(filename, journal_name, name, id)
115
+ acquire_atomic_fs_lock(filename) do
116
+ if yield.include?(id)
117
+ logger.debug("Skipping duplicate #{name} #{id} already in journal #{journal_name}")
118
+ else
119
+ logger.debug("Saving #{name} #{id} to process journal #{journal_name}")
120
+ File.open(filename, "a+", 0o600) { |f| f.puts(journal_line(id)) }
121
+ logger.info("Saved #{name} #{id} to journal #{journal_name}")
122
+ logger.debug("Journal now = #{File.read(filename)}")
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module ProcessJournal
5
+ # A process's journals: its pid journal and its process group journal
6
+ #
7
+ # ProcessJournal.for makes one.
8
+ #
9
+ # @api private
10
+ class Journal
11
+ # Return the process's name
12
+ #
13
+ # @return [#to_s]
14
+ #
15
+ # @api private
16
+ attr_reader :name
17
+
18
+ # Make the journals of a process
19
+ #
20
+ # @param [#to_s] name the process's name
21
+ #
22
+ # @return [void]
23
+ #
24
+ # @api private
25
+ def initialize(name) = @name = name
26
+
27
+ # Add a pid, and its process group id, to the journals
28
+ #
29
+ # An invalid pid, nil or 1 or less, is skipped.
30
+ #
31
+ # @param [Integer, nil] pid the pid
32
+ #
33
+ # @return [void]
34
+ #
35
+ # @api private
36
+ def append(pid)
37
+ ProcessJournal.append(PID, name, pid) { |valid_pid| append_process_group(valid_pid) }
38
+ end
39
+
40
+ # Kill the processes, and then the process groups, the journals list
41
+ #
42
+ # That's the app's journals, and the ones that bluepill 0.1 and earlier
43
+ # shared between apps.
44
+ #
45
+ # @return [void]
46
+ #
47
+ # @api private
48
+ def kill_all
49
+ ProcessJournal.kill_all(PID, name)
50
+ ProcessJournal.kill_all(PGID, name)
51
+ end
52
+
53
+ private
54
+
55
+ # Add a process's process group id to the process group journal
56
+ #
57
+ # @param [Integer] pid the process's pid
58
+ #
59
+ # @return [void]
60
+ #
61
+ # @api private
62
+ def append_process_group(pid)
63
+ ProcessJournal.append(PGID, name, ::Process.getpgid(pid))
64
+ rescue Errno::ESRCH
65
+ # The process is gone, so it has no process group to journal
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module ProcessJournal
5
+ # What a journal holds: pids, or process group ids
6
+ #
7
+ # @api private
8
+ class Kind
9
+ # Return what the journal's log calls an id
10
+ #
11
+ # @return [String] pid or pgid
12
+ #
13
+ # @api private
14
+ attr_reader :name
15
+
16
+ # Return what the cleanup's log calls an id's process
17
+ #
18
+ # @return [String] process or process group
19
+ #
20
+ # @api private
21
+ attr_reader :label
22
+
23
+ # Return what an id is multiplied by to signal it
24
+ #
25
+ # A negative id signals the whole process group.
26
+ #
27
+ # @return [Integer] 1 for pids, or -1 for process groups
28
+ #
29
+ # @api private
30
+ attr_reader :sign
31
+
32
+ # Describe a kind of journal
33
+ #
34
+ # @param [String] name what the journal's log calls an id
35
+ # @param [String] label what the cleanup's log calls an id's process
36
+ # @param [String] file_tag what the journal's filename says it holds
37
+ # @param [Integer] sign what an id is multiplied by to signal it
38
+ #
39
+ # @return [void]
40
+ #
41
+ # @api private
42
+ def initialize(name, label, file_tag, sign)
43
+ @name = name
44
+ @label = label
45
+ @file_tag = file_tag
46
+ @sign = sign
47
+ end
48
+
49
+ # Return the path of a process's journal of this kind
50
+ #
51
+ # @param [String] dir the journal directory
52
+ # @param [#to_s] journal_name the process's name
53
+ #
54
+ # @return [String] the journal's path
55
+ #
56
+ # @api private
57
+ def filename(dir, journal_name) = File.join(dir, ".bluepill_#{@file_tag}_journal.#{journal_name}")
58
+ end
59
+ end
60
+ end
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Bluepill
4
4
  module ProcessJournal
5
+ # Raised when another process holds a journal's lock for too long
6
+ #
7
+ # @api private
8
+ class LockTimeoutError < StandardError; end
9
+
5
10
  # Lock a journal while bluepill reads and writes it
6
11
  #
7
12
  # Processes that run as other users write to the same journals, so each
@@ -22,7 +27,7 @@ module Bluepill
22
27
  #
23
28
  # @return [Object] what the block returns
24
29
  #
25
- # @raise [RuntimeError] if the lock is still held after 10 tries
30
+ # @raise [LockTimeoutError] if the lock is still held after 10 tries
26
31
  #
27
32
  # @api private
28
33
  def acquire_atomic_fs_lock(name)
@@ -70,7 +75,7 @@ module Bluepill
70
75
  #
71
76
  # @return [void]
72
77
  #
73
- # @raise [RuntimeError] if the lock is still held after 10 tries
78
+ # @raise [LockTimeoutError] if the lock is still held after 10 tries
74
79
  #
75
80
  # @api private
76
81
  def make_atomic_fs_lock(lock)
@@ -91,7 +96,7 @@ module Bluepill
91
96
  #
92
97
  # @return [void]
93
98
  #
94
- # @raise [RuntimeError] after the 10th try
99
+ # @raise [LockTimeoutError] after the 10th try
95
100
  #
96
101
  # @api private
97
102
  def wait_for_atomic_fs_lock(lock, attempts)
@@ -100,7 +105,7 @@ module Bluepill
100
105
  sleep 1
101
106
  else
102
107
  logger.info("Timeout waiting for lock #{lock}")
103
- raise "Timeout waiting for lock #{lock}"
108
+ raise LockTimeoutError, "Timeout waiting for lock #{lock}"
104
109
  end
105
110
  end
106
111
  end
@@ -1,6 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
4
+ require "bluepill/base_directory"
3
5
  require "bluepill/system"
6
+ require "bluepill/process_journal/kind"
7
+ require "bluepill/process_journal/journal"
8
+ require "bluepill/process_journal/config"
9
+ require "bluepill/process_journal/entries"
4
10
  require "bluepill/process_journal/cleanup"
5
11
  require "bluepill/process_journal/lock"
6
12
 
@@ -10,7 +16,7 @@ module Bluepill
10
16
  # Each process has a pid journal and a process group journal, files in the
11
17
  # app's journal directory with one id a line. Before bluepill starts a
12
18
  # process, it kills whatever an earlier run left in that process's
13
- # journals.
19
+ # journals. A process reaches its journals through ProcessJournal.for.
14
20
  #
15
21
  # A pid can belong to another process by then: after a reboot, or once
16
22
  # pids wrap around. So each line also has when the process started, and
@@ -25,46 +31,17 @@ module Bluepill
25
31
  # every app's journals in one directory, and those are still read and
26
32
  # cleared, so upgrading doesn't leave their processes behind.
27
33
  #
34
+ # One daemon monitors one app, so the journal directories and the logger
35
+ # are kept on the module, which extends Config, Entries, Cleanup and
36
+ # Lock.
37
+ #
28
38
  # @api private
29
39
  module ProcessJournal
40
+ extend Config
41
+ extend Entries
30
42
  extend Cleanup
31
43
  extend Lock
32
44
 
33
- # What a journal holds: pids, or process group ids
34
- #
35
- # @!attribute name
36
- # Return what the journal's log calls an id
37
- #
38
- # @return [String] pid or pgid
39
- #
40
- # @api private
41
- #
42
- # @!attribute label
43
- # Return what the cleanup's log calls an id's process
44
- #
45
- # @return [String] process or process group
46
- #
47
- # @api private
48
- #
49
- # @!attribute file_tag
50
- # Return what the journal's filename says it holds
51
- #
52
- # @return [String] pids or pgids
53
- #
54
- # @api private
55
- #
56
- # @!attribute sign
57
- # Return what an id is multiplied by to signal it
58
- #
59
- # A negative id signals the whole process group.
60
- #
61
- # @return [Integer] 1 for pids, or -1 for process groups
62
- #
63
- # @api private
64
- #
65
- # @api private
66
- Kind = Struct.new(:name, :label, :file_tag, :sign)
67
-
68
45
  # A pid journal's kind
69
46
  #
70
47
  # @api private
@@ -74,293 +51,5 @@ module Bluepill
74
51
  #
75
52
  # @api private
76
53
  PGID = Kind.new("pgid", "process group", "pgids", -1).freeze
77
-
78
- class << self
79
- # Return the logger the journal writes to
80
- #
81
- # @return [Logger] the first logger that was set
82
- #
83
- # @api private
84
- attr_reader :logger
85
-
86
- # Return the directory the app's journals are in
87
- #
88
- # @return [String] the app's directory in the journals directory
89
- #
90
- # @api private
91
- attr_reader :journal_base_dir
92
-
93
- # Return the directory every app's journals were in before 0.2
94
- #
95
- # @return [String] the journals directory under bluepill's base dir
96
- #
97
- # @api private
98
- attr_reader :legacy_journal_dir
99
-
100
- # Set the logger the journal writes to, unless it already has one
101
- #
102
- # Only the first logger is kept.
103
- #
104
- # @param [Logger] new_logger the logger to use
105
- #
106
- # @return [Logger] the logger
107
- #
108
- # @api private
109
- def logger=(new_logger)
110
- @logger ||= new_logger
111
- end
112
-
113
- # Set the app's journal directory from bluepill's base dir, and create it
114
- #
115
- # Only the first base dir and app are kept. The directories are made
116
- # world-writable so processes that run as other users can lock their
117
- # journals.
118
- #
119
- # @param [String] base_dir bluepill's base directory
120
- # @param [#to_s] app_name the app's name
121
- #
122
- # @return [void]
123
- #
124
- # @api private
125
- def setup(base_dir, app_name)
126
- @legacy_journal_dir ||= File.join(base_dir, "journals")
127
- @journal_base_dir ||= File.join(@legacy_journal_dir, app_name.to_s)
128
- [@legacy_journal_dir, @journal_base_dir].each do |dir|
129
- FileUtils.mkdir_p(dir)
130
- FileUtils.chmod(0o777, dir)
131
- end
132
- end
133
- end
134
-
135
- # Return whether a pid can't be journaled or signaled
136
- #
137
- # A pid of 1 or less would reach init, or a whole process group.
138
- #
139
- # @param [Integer, nil] pid the pid to check
140
- #
141
- # @return [Boolean] true unless pid is an Integer above 1
142
- #
143
- # @api private
144
- def self.skip_pid?(pid)
145
- skip_id?(pid)
146
- end
147
-
148
- # Return whether a process group id can't be journaled or signaled
149
- #
150
- # @param [Integer, nil] pgid the process group id to check
151
- #
152
- # @return [Boolean] true unless pgid is an Integer above 1
153
- #
154
- # @api private
155
- def self.skip_pgid?(pgid)
156
- skip_id?(pgid)
157
- end
158
-
159
- # Return whether an id can't be journaled or signaled
160
- #
161
- # @param [Integer, nil] id the pid or process group id to check
162
- #
163
- # @return [Boolean] true unless id is an Integer above 1
164
- #
165
- # @api private
166
- def self.skip_id?(id)
167
- !id.is_a?(Integer) || id <= 1
168
- end
169
-
170
- # Return the path of a process's pid journal
171
- #
172
- # @param [String] journal_name the process's name
173
- # @param [String] dir the journal directory
174
- #
175
- # @return [String] the journal's path
176
- #
177
- # @api private
178
- def self.pid_journal_filename(journal_name, dir = journal_base_dir)
179
- journal_filename(PID, journal_name, dir)
180
- end
181
-
182
- # Return the path of a process's process group journal
183
- #
184
- # @param [String] journal_name the process's name
185
- # @param [String] dir the journal directory
186
- #
187
- # @return [String] the journal's path
188
- #
189
- # @api private
190
- def self.pgid_journal_filename(journal_name, dir = journal_base_dir)
191
- journal_filename(PGID, journal_name, dir)
192
- end
193
-
194
- # Return the path of one of a process's journals
195
- #
196
- # @param [Kind] kind PID or PGID
197
- # @param [String] journal_name the process's name
198
- # @param [String] dir the journal directory
199
- #
200
- # @return [String] the journal's path
201
- #
202
- # @api private
203
- def self.journal_filename(kind, journal_name, dir = journal_base_dir)
204
- File.join(dir, ".bluepill_#{kind.file_tag}_journal.#{journal_name}")
205
- end
206
-
207
- # Return the ids in a journal, each with when its process started
208
- #
209
- # @param [String] filename the journal's path
210
- #
211
- # @return [Array<Array(Integer, Integer)>, Array<Array(Integer, nil)>]
212
- # each id, with when its process started, or nil for a line from
213
- # bluepill 0.1 and earlier, which has none
214
- #
215
- # @raise [Errno::ENOENT] if there's no journal
216
- #
217
- # @api private
218
- def self.journal_entries(filename)
219
- File.readlines(filename).collect do |line|
220
- id, started = line.split
221
- [id.to_i, started&.to_i]
222
- end
223
- end
224
-
225
- # Return a journal line for an id: the id, and when its process started
226
- #
227
- # @param [Integer] id a pid, or a process group id, whose leader's start
228
- # is recorded
229
- #
230
- # @return [String] the line, without a start time if ps can't tell
231
- #
232
- # @api private
233
- def self.journal_line(id)
234
- [id, System.started_at(id)].compact.join(" ")
235
- end
236
-
237
- # Return the valid pids in a pid journal
238
- #
239
- # @param [String] filename the journal's path
240
- #
241
- # @return [Array<Integer>] the pids, or none if there's no journal
242
- #
243
- # @api private
244
- def self.pid_journal(filename)
245
- journal_ids(PID, filename)
246
- end
247
-
248
- # Return the valid process group ids in a process group journal
249
- #
250
- # @param [String] filename the journal's path
251
- #
252
- # @return [Array<Integer>] the process group ids, or none if there's no
253
- # journal
254
- #
255
- # @api private
256
- def self.pgid_journal(filename)
257
- journal_ids(PGID, filename)
258
- end
259
-
260
- # Return the valid ids in a journal
261
- #
262
- # @param [Kind] kind PID or PGID
263
- # @param [String] filename the journal's path
264
- #
265
- # @return [Array<Integer>] the ids, or none if there's no journal
266
- #
267
- # @api private
268
- def self.journal_ids(kind, filename)
269
- logger.debug("#{kind.name} journal file: #{filename}")
270
- result = journal_entries(filename).collect(&:first).reject { |id| skip_id?(id) }
271
- logger.debug("#{kind.name} journal = #{result.join(" ")}")
272
- result
273
- rescue Errno::ENOENT
274
- []
275
- end
276
-
277
- # Add a process group id to a process's journal, unless it's there
278
- #
279
- # @param [String] journal_name the process's name
280
- # @param [Integer, nil] pgid the process group id
281
- #
282
- # @return [void]
283
- #
284
- # @api private
285
- def self.append_pgid_to_journal(journal_name, pgid)
286
- append_to_journal(PGID, journal_name, pgid)
287
- end
288
-
289
- # Add a pid, and its process group id, to a process's journals
290
- #
291
- # @param [String] journal_name the process's name
292
- # @param [Integer, nil] pid the pid
293
- #
294
- # @return [void]
295
- #
296
- # @api private
297
- def self.append_pid_to_journal(journal_name, pid)
298
- append_to_journal(PID, journal_name, pid) { |valid_pid| append_process_group_to_journal(journal_name, valid_pid) }
299
- end
300
-
301
- # Add a process's process group id to the process's journal
302
- #
303
- # @param [String] journal_name the process's name
304
- # @param [Integer] pid the process's pid
305
- #
306
- # @return [void]
307
- #
308
- # @api private
309
- def self.append_process_group_to_journal(journal_name, pid)
310
- append_pgid_to_journal(journal_name, ::Process.getpgid(pid))
311
- rescue Errno::ESRCH
312
- # The process is gone, so it has no process group to journal
313
- end
314
-
315
- # Add an id to one of a process's journals, unless it's invalid
316
- #
317
- # @param [Kind] kind PID or PGID
318
- # @param [String] journal_name the process's name
319
- # @param [Integer, nil] id the pid or process group id
320
- #
321
- # @yield [id] once it's known to be valid, before it's journaled
322
- # @yieldparam [Integer] id the id
323
- #
324
- # @return [void]
325
- #
326
- # @api private
327
- def self.append_to_journal(kind, journal_name, id)
328
- if skip_id?(id)
329
- logger.debug("Skipping invalid #{kind.name} #{id} (class #{id.class})")
330
- return
331
- end
332
- # skip_id? made sure it's an Integer
333
- valid_id = id #: Integer
334
- yield valid_id if block_given?
335
-
336
- filename = journal_filename(kind, journal_name)
337
- write_to_journal(filename, journal_name, kind.name, valid_id) { journal_ids(kind, filename) }
338
- end
339
-
340
- # Add a line for an id to a journal, under its lock, unless it's there
341
- #
342
- # @param [String] filename the journal's path
343
- # @param [String] journal_name the process's name
344
- # @param [String] name what the id is, pid or pgid, for the log
345
- # @param [Integer] id the pid or process group id
346
- #
347
- # @yield under the lock, to read the journal
348
- # @yieldreturn [Array<Integer>] the ids already in the journal
349
- #
350
- # @return [void]
351
- #
352
- # @api private
353
- def self.write_to_journal(filename, journal_name, name, id)
354
- acquire_atomic_fs_lock(filename) do
355
- if yield.include?(id)
356
- logger.debug("Skipping duplicate #{name} #{id} already in journal #{journal_name}")
357
- else
358
- logger.debug("Saving #{name} #{id} to process journal #{journal_name}")
359
- File.open(filename, "a+", 0o600) { |f| f.puts(journal_line(id)) }
360
- logger.info("Saved #{name} #{id} to journal #{journal_name}")
361
- logger.debug("Journal now = #{File.read(filename)}")
362
- end
363
- end
364
- end
365
54
  end
366
55
  end
@@ -24,22 +24,18 @@ module Bluepill
24
24
  # @return [void]
25
25
  #
26
26
  # @api private
27
- def initialize
28
- @events = Util::RotationalArray.new(EVENTS_TO_PERSIST)
29
- end
27
+ def initialize = @events = Util::RotationalArray.new(EVENTS_TO_PERSIST)
30
28
 
31
29
  # Add an event to the history, with the time it happened
32
30
  #
33
- # @param [Symbol, String] event the event, such as :restart
31
+ # @param [Symbol] event the event, such as :restart
34
32
  # @param [String, nil] reason why it happened
35
33
  #
36
34
  # @return [Util::RotationalArray<Array(Symbol, String, nil, Time)>] the
37
35
  # history, newest last
38
36
  #
39
37
  # @api private
40
- def record_event(event, reason)
41
- events.push([event, reason, Time.now])
42
- end
38
+ def record_event(event, reason) = events.push([event, reason, Time.now])
43
39
 
44
40
  # Return the history as text, newest first, for bluepill status
45
41
  #