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.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +144 -1
  3. data/README.md +17 -10
  4. data/bin/bluepill +8 -2
  5. data/bluepill.gemspec +1 -1
  6. data/lib/bluepill/application/lifecycle.rb +43 -31
  7. data/lib/bluepill/application/listener.rb +1 -1
  8. data/lib/bluepill/application/previous_daemon.rb +14 -14
  9. data/lib/bluepill/application.rb +48 -28
  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 +61 -67
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +46 -38
  16. data/lib/bluepill/dsl/app_proxy.rb +69 -58
  17. data/lib/bluepill/dsl/attributes.rb +135 -0
  18. data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
  20. data/lib/bluepill/dsl/process_factory.rb +86 -70
  21. data/lib/bluepill/dsl/process_proxy.rb +61 -158
  22. data/lib/bluepill/dsl.rb +15 -6
  23. data/lib/bluepill/errors.rb +190 -0
  24. data/lib/bluepill/group.rb +14 -37
  25. data/lib/bluepill/logger.rb +55 -97
  26. data/lib/bluepill/process/attributes.rb +66 -32
  27. data/lib/bluepill/process/children.rb +4 -6
  28. data/lib/bluepill/process/pid.rb +41 -35
  29. data/lib/bluepill/process/state_machine.rb +15 -59
  30. data/lib/bluepill/process/stop_signals.rb +120 -0
  31. data/lib/bluepill/process/transition.rb +1 -3
  32. data/lib/bluepill/process/transition_callbacks.rb +2 -19
  33. data/lib/bluepill/process/triggers.rb +90 -0
  34. data/lib/bluepill/process/user_commands.rb +1 -1
  35. data/lib/bluepill/process/watches.rb +9 -23
  36. data/lib/bluepill/process.rb +70 -55
  37. data/lib/bluepill/process_commands/execution.rb +14 -22
  38. data/lib/bluepill/process_commands/hooks.rb +1 -31
  39. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  40. data/lib/bluepill/process_commands.rb +10 -8
  41. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  42. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  43. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  44. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  45. data/lib/bluepill/process_conditions/http.rb +28 -43
  46. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  47. data/lib/bluepill/process_conditions/process_condition.rb +30 -32
  48. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  49. data/lib/bluepill/process_conditions.rb +5 -14
  50. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  51. data/lib/bluepill/process_journal/config.rb +73 -0
  52. data/lib/bluepill/process_journal/entries.rb +128 -0
  53. data/lib/bluepill/process_journal/journal.rb +69 -0
  54. data/lib/bluepill/process_journal/kind.rb +60 -0
  55. data/lib/bluepill/process_journal/lock.rb +9 -4
  56. data/lib/bluepill/process_journal.rb +13 -324
  57. data/lib/bluepill/process_statistics.rb +3 -7
  58. data/lib/bluepill/socket.rb +19 -17
  59. data/lib/bluepill/system/command_options.rb +135 -0
  60. data/lib/bluepill/system/command_result.rb +73 -0
  61. data/lib/bluepill/system/command_setup.rb +3 -4
  62. data/lib/bluepill/system/commands.rb +21 -18
  63. data/lib/bluepill/system/daemon.rb +19 -14
  64. data/lib/bluepill/system/files.rb +35 -0
  65. data/lib/bluepill/system/ps.rb +20 -17
  66. data/lib/bluepill/system/ps_table.rb +7 -9
  67. data/lib/bluepill/system/usage.rb +155 -0
  68. data/lib/bluepill/system.rb +7 -192
  69. data/lib/bluepill/trigger.rb +22 -32
  70. data/lib/bluepill/triggers/flapping.rb +12 -8
  71. data/lib/bluepill/units.rb +7 -21
  72. data/lib/bluepill/util/log_error.rb +28 -0
  73. data/lib/bluepill/version.rb +12 -22
  74. data/lib/bluepill.rb +1 -0
  75. data/sig/bluepill/application.rbs +17 -12
  76. data/sig/bluepill/attributes.rbs +48 -0
  77. data/sig/bluepill/checks.rbs +43 -30
  78. data/sig/bluepill/dsl.rbs +98 -55
  79. data/sig/bluepill/errors.rbs +48 -0
  80. data/sig/bluepill/logger.rbs +15 -31
  81. data/sig/bluepill/process.rbs +42 -304
  82. data/sig/bluepill/process_commands.rbs +235 -0
  83. data/sig/bluepill/stop_signals.rbs +21 -0
  84. data/sig/bluepill/system.rbs +154 -99
  85. data/sig/bluepill/version.rbs +17 -0
  86. data/sig/bluepill.rbs +4 -17
  87. data/sig/manifest.yaml +1 -0
  88. metadata +22 -3
  89. data/lib/bluepill/dsl/process_factory/children.rb +0 -73
@@ -14,9 +14,7 @@ module Bluepill
14
14
  # @return [Integer] always 1
15
15
  #
16
16
  # @api private
17
- def run(_pid, _include_children)
18
- 1
19
- end
17
+ def run(_pid, _include_children) = 1
20
18
 
21
19
  # Pass whatever the value is
22
20
  #
@@ -25,9 +23,7 @@ module Bluepill
25
23
  # @return [Boolean] always true
26
24
  #
27
25
  # @api private
28
- def check?(_value)
29
- true
30
- end
26
+ def check?(_value) = true
31
27
  end
32
28
  end
33
29
  end
@@ -12,15 +12,15 @@ module Bluepill
12
12
  class BelowThreshold < ProcessCondition
13
13
  # Create the check
14
14
  #
15
- # @param [Hash{Symbol => Object}] options the check's options, with
16
- # :below, the limit
15
+ # @param [Numeric] below the limit
16
+ # @param [Hash{Symbol => Object}] options the check's other options
17
17
  #
18
18
  # @return [void]
19
19
  #
20
20
  # @api private
21
- def initialize(options = {})
22
- super
23
- @below = options[:below]
21
+ def initialize(below:, **options)
22
+ super(**options)
23
+ @below = below
24
24
  end
25
25
 
26
26
  # Return whether a value is below the limit
@@ -30,9 +30,7 @@ module Bluepill
30
30
  # @return [Boolean] true when it's below the limit
31
31
  #
32
32
  # @api private
33
- def check?(value)
34
- value < @below
35
- end
33
+ def check?(value) = value < @below
36
34
  end
37
35
  end
38
36
  end
@@ -21,10 +21,7 @@ module Bluepill
21
21
  # @return [Float] the percentage, or 0.0 for a process ps doesn't list
22
22
  #
23
23
  # @api private
24
- def run(pid, include_children)
25
- # third col in the ps axu output
26
- System.cpu_usage(pid, include_children).to_f
27
- end
24
+ def run(pid, include_children) = System.cpu_usage(pid, include_children).to_f
28
25
  end
29
26
  end
30
27
  end
@@ -15,16 +15,17 @@ module Bluepill
15
15
  class FileTime < BelowThreshold
16
16
  # Create the check
17
17
  #
18
- # @param [Hash{Symbol => Object}] options the check's options, with
19
- # :filename, the file, and :below, the longest it may go without
20
- # changing, in seconds
18
+ # @param [String] filename the file
19
+ # @param [Numeric] below the longest the file may go without changing,
20
+ # in seconds
21
+ # @param [Hash{Symbol => Object}] options the check's other options
21
22
  #
22
23
  # @return [void]
23
24
  #
24
25
  # @api private
25
- def initialize(options = {})
26
- super
27
- @filename = options[:filename]
26
+ def initialize(filename:, below:, **options)
27
+ super(below: below, **options)
28
+ @filename = filename
28
29
  end
29
30
 
30
31
  # Read the seconds since the file changed
@@ -50,9 +51,7 @@ module Bluepill
50
51
  # for a missing file or an error
51
52
  #
52
53
  # @api private
53
- def check?(value)
54
- value.is_a?(Numeric) && super
55
- end
54
+ def check?(value) = value.is_a?(Numeric) && super
56
55
  end
57
56
  end
58
57
  end
@@ -11,17 +11,31 @@ module Bluepill
11
11
  # response it should be (a Net::HTTP class name such as :success or
12
12
  # :ok, or a status code such as 200, :success by default), pattern: a
13
13
  # Regexp its body should match, and timeout: (or open_timeout: and
14
- # read_timeout:) in seconds, 5 by default.
14
+ # read_timeout:) in seconds, 5 by default. An https URL's certificate
15
+ # must be valid, unless verify_ssl: is false, as for a self-signed one.
15
16
  #
16
17
  # @example
17
18
  # process.checks :http, url: 'http://localhost:4567/', kind: :success, pattern: /html/, timeout: 3.seconds
18
19
  #
20
+ # @example A server with a self-signed certificate
21
+ # process.checks :http, url: 'https://localhost:4443/', verify_ssl: false
22
+ #
19
23
  # @api private
20
24
  class Http < ProcessCondition
21
25
  # Create the check
22
26
  #
23
- # @param [Hash{Symbol => Object}] options the check's options, with
24
- # :url, :kind, :pattern, :timeout, :open_timeout and :read_timeout
27
+ # @param [String] url the URL to get
28
+ # @param [Integer, String, Symbol, nil] kind the response it expects,
29
+ # as a status code, such as 200, or a Net::HTTP class name, such as
30
+ # :success, the default
31
+ # @param [Regexp, nil] pattern what the body must match, if anything
32
+ # @param [Numeric, String] timeout the seconds to wait to connect and
33
+ # to read, unless open_timeout or read_timeout is given
34
+ # @param [Numeric, String] open_timeout the seconds to wait to connect
35
+ # @param [Numeric, String] read_timeout the seconds to wait to read
36
+ # @param [Boolean] verify_ssl whether an https URL's certificate must
37
+ # be valid, true by default
38
+ # @param [Hash{Symbol => Object}] options the check's other options
25
39
  #
26
40
  # @return [void]
27
41
  #
@@ -30,19 +44,18 @@ module Bluepill
30
44
  # @raise [NameError] for a kind that names no Net::HTTP response
31
45
  #
32
46
  # @api private
33
- def initialize(options = {})
34
- super
35
- @uri = URI.parse(options[:url]) #: URI::HTTP
36
- @kind = response_class(options[:kind])
37
- @pattern = options[:pattern]
38
- @open_timeout = (options[:open_timeout] || options[:timeout] || 5).to_i
39
- @read_timeout = (options[:read_timeout] || options[:timeout] || 5).to_i
47
+ def initialize(url:, kind: nil, pattern: nil, timeout: 5, open_timeout: timeout, read_timeout: timeout, verify_ssl: true, **options)
48
+ super(**options)
49
+ @verify_ssl = verify_ssl
50
+ @uri = URI.parse(url) #: URI::HTTP
51
+ @kind = response_class(kind)
52
+ @pattern = pattern
53
+ @open_timeout = open_timeout.to_i
54
+ @read_timeout = read_timeout.to_i
40
55
  end
41
56
 
42
57
  # Get the URL
43
58
  #
44
- # HTTPS is checked without verifying the certificate.
45
- #
46
59
  # @param [Integer, nil] _pid the process's pid
47
60
  # @param [Boolean] _include_children whether to include its children
48
61
  #
@@ -51,12 +64,7 @@ module Bluepill
51
64
  #
52
65
  # @api private
53
66
  def run(_pid, _include_children)
54
- session = new_session
55
- hide_net_http_bug do
56
- session.start do |http|
57
- http.get(@uri.request_uri)
58
- end
59
- end
67
+ new_session.start { |http| http.get(@uri.request_uri) }
60
68
  rescue => e
61
69
  e
62
70
  end
@@ -107,7 +115,7 @@ module Bluepill
107
115
 
108
116
  # Return a session with the URL's host, set up to get the URL
109
117
  #
110
- # HTTPS is checked without verifying the certificate.
118
+ # HTTPS verifies the server's certificate, unless verify_ssl is false.
111
119
  #
112
120
  # @return [Net::HTTP] the session, not started yet
113
121
  #
@@ -118,35 +126,12 @@ module Bluepill
118
126
  if @uri.scheme.eql?("https")
119
127
  require "net/https"
120
128
  session.use_ssl = true
121
- session.verify_mode = OpenSSL::SSL::VERIFY_NONE
129
+ session.verify_mode = @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
122
130
  end
123
131
  session.open_timeout = @open_timeout
124
132
  session.read_timeout = @read_timeout
125
133
  session
126
134
  end
127
-
128
- # Run the block, raising a refused connection as one
129
- #
130
- # Net::HTTP raises NoMethodError (closed? for nil) when it can't
131
- # connect, so this raises Errno::ECONNREFUSED instead.
132
- #
133
- # @yield the request
134
- # @yieldreturn [Object] the response
135
- #
136
- # @return [Object] what the block returns
137
- #
138
- # @raise [Errno::ECONNREFUSED] when the connection was refused
139
- #
140
- # @api private
141
- def hide_net_http_bug
142
- yield
143
- rescue NoMethodError => e
144
- # Ruby 3.4 changed the wording of this error
145
- message = e.to_s
146
- raise(Errno::ECONNREFUSED, "Connection refused attempting to contact #{@uri.scheme}://#{@uri.host}:#{@uri.port}") if message.include?("undefined method `closed?' for nil:NilClass") || message.include?("undefined method 'closed?' for nil")
147
-
148
- raise
149
- end
150
135
  end
151
136
  end
152
137
  end
@@ -13,67 +13,29 @@ module Bluepill
13
13
  #
14
14
  # @api private
15
15
  class MemUsage < BelowThreshold
16
- # The bytes in a megabyte, from which a value shows in megabytes
17
- #
18
- # @api private
19
- MB = (1024**2) #: Integer
20
-
21
- # How a value shows in the history: a number and its unit
22
- #
23
- # @api private
24
- FORMAT_STR = "%d%s"
25
-
26
- # The unit of a value that shows in megabytes
27
- #
28
- # @api private
29
- MB_LABEL = "MB"
30
-
31
- # The unit of a value that shows in kilobytes
32
- #
33
- # @api private
34
- KB_LABEL = "KB"
35
-
36
16
  # Read the process's memory usage from the tick's ps table
37
17
  #
38
18
  # @param [Integer, nil] pid the process's pid
39
19
  # @param [Boolean] include_children whether to add its children's usage
40
20
  #
41
- # @return [Float] the usage in kilobytes, or 0.0 for a process ps
42
- # doesn't list
43
- #
44
- # @api private
45
- def run(pid, include_children)
46
- # rss is on the 5th col
47
- System.memory_usage(pid, include_children).to_f
48
- end
49
-
50
- # Return whether the usage is below the limit
51
- #
52
- # A limit that can't be compared counts as fine.
53
- #
54
- # @param [Float] value the kilobytes run read
55
- #
56
- # @return [Boolean] true when it's below the limit
21
+ # @return [Float] the usage in bytes, or 0.0 for a process ps doesn't
22
+ # list
57
23
  #
58
24
  # @api private
59
- def check?(value)
60
- value * 1024 < @below
61
- rescue
62
- true
63
- end
25
+ def run(pid, include_children) = System.memory_usage(pid, include_children).to_f * Units::KILOBYTE
64
26
 
65
27
  # Format the usage in kilobytes, or in megabytes from a megabyte up
66
28
  #
67
- # @param [Float] value the kilobytes run read
29
+ # @param [Float] value the bytes run read
68
30
  #
69
31
  # @return [String] the usage and its unit, such as "312KB" or "250MB"
70
32
  #
71
33
  # @api private
72
34
  def format_value(value)
73
- if value * 1024 >= MB
74
- format(FORMAT_STR, (value / 1024).round, MB_LABEL)
35
+ if value >= Units::MEGABYTE
36
+ format("%dMB", (value / Units::MEGABYTE).round)
75
37
  else
76
- format(FORMAT_STR, value, KB_LABEL)
38
+ format("%dKB", value / Units::KILOBYTE)
77
39
  end
78
40
  end
79
41
  end
@@ -7,10 +7,30 @@ module Bluepill
7
7
  # A ConditionWatch runs the condition every so often. The condition reads
8
8
  # a value (run), says whether the value is fine (check), and formats it
9
9
  # for the watch's history (format_value). Subclasses implement run and
10
- # check.
10
+ # check?, and add themselves to CheckRegistry by their underscored
11
+ # name, in whatever module they're defined.
11
12
  #
12
13
  # @api private
13
14
  class ProcessCondition
15
+ # The methods a subclass must define
16
+ #
17
+ # @api private
18
+ REQUIRED_METHODS = %i[run check?].freeze
19
+
20
+ # Add a subclass to the checks a config can name
21
+ #
22
+ # A config names it by its underscored name, such as :mem_usage.
23
+ #
24
+ # @param [Class] klass the subclass
25
+ #
26
+ # @return [void]
27
+ #
28
+ # @api private
29
+ def self.inherited(klass)
30
+ super
31
+ CheckRegistry.add(klass, ProcessCondition, REQUIRED_METHODS)
32
+ end
33
+
14
34
  # Return the logger the condition can write to
15
35
  #
16
36
  # It's the watch's logger, so lines carry the app's and the process's
@@ -23,19 +43,19 @@ module Bluepill
23
43
  # @return [Logger, nil] the process's logger
24
44
  #
25
45
  # @api public
26
- attr_accessor :logger
46
+ attr_reader :logger
27
47
 
28
48
  # Create a condition with the options a config gave its check
29
49
  #
30
- # @param [Hash{Symbol => Object}] options the check's options, such as
31
- # below: 100.megabytes, and the watch's logger as logger:
50
+ # @param [Logger, nil] logger the watch's logger
32
51
  #
33
52
  # @return [void]
34
53
  #
54
+ # @raise [ArgumentError] for an option that no condition takes
55
+ #
35
56
  # @api private
36
- def initialize(options = {})
37
- @logger = options.delete(:logger)
38
- @options = options
57
+ def initialize(logger: nil)
58
+ @logger = logger
39
59
  end
40
60
 
41
61
  # Read the value to check from the process
@@ -48,28 +68,10 @@ module Bluepill
48
68
  # @raise [RuntimeError] unless a subclass implements it
49
69
  #
50
70
  # @api private
51
- def run(_pid, _include_children)
52
- raise "Implement in subclass!"
53
- end
71
+ def run(_pid, _include_children) = raise("Implement in subclass!")
54
72
 
55
73
  # Return whether a value is fine
56
74
  #
57
- # A subclass implements it. This one calls check, so a custom check
58
- # written for bluepill 0.1, which defines check, still works.
59
- #
60
- # @param [Object] value what run read
61
- #
62
- # @return [Boolean] true when the value is fine, false when it's not
63
- #
64
- # @raise [RuntimeError] unless a subclass implements check? or check
65
- #
66
- # @api private
67
- def check?(value)
68
- check(value)
69
- end
70
-
71
- # Return whether a value is fine, as bluepill 0.1 named check?
72
- #
73
75
  # @param [Object] _value what run read
74
76
  #
75
77
  # @return [Boolean] true when the value is fine, false when it's not
@@ -77,9 +79,7 @@ module Bluepill
77
79
  # @raise [RuntimeError] unless a subclass implements it
78
80
  #
79
81
  # @api private
80
- def check(_value)
81
- raise "Implement in subclass!"
82
- end
82
+ def check?(_value) = raise("Implement in subclass!")
83
83
 
84
84
  # Format a value for the watch's history and log
85
85
  #
@@ -89,9 +89,7 @@ module Bluepill
89
89
  # value itself
90
90
  #
91
91
  # @api private
92
- def format_value(value)
93
- value
94
- end
92
+ def format_value(value) = value
95
93
  end
96
94
  end
97
95
  end
@@ -22,9 +22,7 @@ module Bluepill
22
22
  # doesn't list
23
23
  #
24
24
  # @api private
25
- def run(pid, _include_children)
26
- System.command(pid)
27
- end
25
+ def run(pid, _include_children) = System.command(pid)
28
26
 
29
27
  # Return whether the process isn't a zombie
30
28
  #
@@ -33,9 +31,7 @@ module Bluepill
33
31
  # @return [Boolean] true unless the command line says <defunct>
34
32
  #
35
33
  # @api private
36
- def check?(value)
37
- !value&.match?(/<defunct>/)
38
- end
34
+ def check?(value) = !value&.match?(/<defunct>/)
39
35
  end
40
36
  end
41
37
  end
@@ -1,29 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bluepill/check_registry"
4
+
3
5
  module Bluepill
4
6
  # The checks a process can run, named in a config by their underscored
5
7
  # class name, such as `checks :mem_usage`
6
8
  #
9
+ # CheckRegistry finds them by that name.
10
+ #
7
11
  # @api private
8
12
  module ProcessConditions
9
- # Return the check class a config names
10
- #
11
- # @param [Symbol, String] name the check's underscored name, such as
12
- # :mem_usage
13
- #
14
- # @return [Class] the ProcessCondition subclass, such as MemUsage
15
- #
16
- # @raise [NameError] when no check has that name
17
- #
18
- # @api private
19
- def self.[](name)
20
- const_get(Util.camelize(name))
21
- end
22
13
  end
23
14
  end
24
15
 
25
16
  require "bluepill/process_conditions/process_condition"
26
17
  require "bluepill/process_conditions/below_threshold"
27
- Dir["#{File.dirname(__FILE__)}/process_conditions/*.rb"].sort.each do |pc|
18
+ Dir["#{File.dirname(__FILE__)}/process_conditions/*.rb"].each do |pc|
28
19
  require pc
29
20
  end
@@ -6,77 +6,29 @@ module Bluepill
6
6
  #
7
7
  # Before bluepill starts a process, it kills the processes and process
8
8
  # groups its journals list that are still the ones bluepill journaled,
9
- # and then clears the journals. ProcessJournal extends it.
9
+ # and then clears the journals. ProcessJournal extends it, and a
10
+ # Journal calls it.
10
11
  #
11
12
  # @api private
12
13
  module Cleanup
13
- # Kill the processes and process groups that a process's journals list
14
- #
15
- # @param [String] journal_name the process's name
16
- #
17
- # @return [void]
18
- #
19
- # @api private
20
- def kill_all_from_journal(journal_name)
21
- kill_all_pids_from_journal(journal_name)
22
- kill_all_pgids_from_journal(journal_name)
23
- end
24
-
25
- # Kill the process groups that a process's journals list
26
- #
27
- # That's the app's journal, and the one that bluepill 0.1 and earlier
28
- # shared between apps.
29
- #
30
- # @param [String] journal_name the process's name
31
- #
32
- # @return [void]
33
- #
34
- # @api private
35
- def kill_all_pgids_from_journal(journal_name)
36
- kill_all_ids_from_journal(PGID, journal_name)
37
- end
38
-
39
- # Kill the processes that a process's journals list
40
- #
41
- # That's the app's journal, and the one that bluepill 0.1 and earlier
42
- # shared between apps.
43
- #
44
- # @param [String] journal_name the process's name
45
- #
46
- # @return [void]
47
- #
48
- # @api private
49
- def kill_all_pids_from_journal(journal_name)
50
- kill_all_ids_from_journal(PID, journal_name)
51
- end
52
-
53
14
  # Kill the ids that one kind of a process's journals list
54
15
  #
55
16
  # That's the app's journal, and the one that bluepill 0.1 and earlier
56
17
  # shared between apps.
57
18
  #
58
19
  # @param [Kind] kind PID or PGID
59
- # @param [String] journal_name the process's name
20
+ # @param [#to_s] journal_name the process's name
60
21
  #
61
22
  # @return [void]
62
23
  #
63
24
  # @api private
64
- def kill_all_ids_from_journal(kind, journal_name)
25
+ def kill_all(kind, journal_name)
65
26
  journal_dirs.each do |dir|
66
- filename = journal_filename(kind, journal_name, dir)
27
+ filename = kind.filename(dir, journal_name)
67
28
  kill_all_from_journal_file(filename, journal_ids(kind, filename), kind)
68
29
  end
69
30
  end
70
31
 
71
- # Return the directories a process's journals can be in
72
- #
73
- # @return [Array<String>] the app's, and then the one before 0.2
74
- #
75
- # @api private
76
- def journal_dirs
77
- [journal_base_dir, legacy_journal_dir]
78
- end
79
-
80
32
  # Kill the ids a journal lists, and then delete the journal
81
33
  #
82
34
  # Only kills the ids that still belong to what bluepill journaled (see
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module ProcessJournal
5
+ # Where the journals are, and the logger they write to
6
+ #
7
+ # Only the first app and logger are kept, since one daemon monitors one
8
+ # app. ProcessJournal extends it.
9
+ #
10
+ # @api private
11
+ module Config
12
+ # Return the logger the journal writes to
13
+ #
14
+ # @return [Logger] the first logger that was set
15
+ #
16
+ # @api private
17
+ attr_reader :logger
18
+
19
+ # Return the directory the app's journals are in
20
+ #
21
+ # @return [String] the app's directory in the journals directory
22
+ #
23
+ # @api private
24
+ attr_reader :journal_base_dir
25
+
26
+ # Return the directory every app's journals were in before 0.2
27
+ #
28
+ # @return [String] the journals directory under bluepill's base dir
29
+ #
30
+ # @api private
31
+ attr_reader :legacy_journal_dir
32
+
33
+ # Set the logger the journal writes to, unless it already has one
34
+ #
35
+ # @param [Logger] new_logger the logger to use
36
+ #
37
+ # @return [Logger] the logger
38
+ #
39
+ # @api private
40
+ def logger=(new_logger)
41
+ @logger ||= new_logger
42
+ end
43
+
44
+ # Set the app's journal directory from bluepill's base dir, and create it
45
+ #
46
+ # Only the first base dir and app are kept. The directories are made
47
+ # world-writable so processes that run as other users can lock their
48
+ # journals.
49
+ #
50
+ # @param [String] base_dir bluepill's base directory
51
+ # @param [#to_s] app_name the app's name
52
+ #
53
+ # @return [void]
54
+ #
55
+ # @api private
56
+ def setup(base_dir, app_name)
57
+ @legacy_journal_dir ||= BaseDirectory.new(base_dir).journals_dir
58
+ @journal_base_dir ||= File.join(@legacy_journal_dir, app_name.to_s)
59
+ journal_dirs.each do |dir|
60
+ FileUtils.mkdir_p(dir)
61
+ FileUtils.chmod(0o777, dir)
62
+ end
63
+ end
64
+
65
+ # Return the directories a process's journals can be in
66
+ #
67
+ # @return [Array<String>] the app's, and then the one before 0.2
68
+ #
69
+ # @api private
70
+ def journal_dirs = [journal_base_dir, legacy_journal_dir]
71
+ end
72
+ end
73
+ end