bluepill 0.2.9 → 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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -2
  3. data/README.md +12 -7
  4. data/bin/bluepill +8 -2
  5. data/bluepill.gemspec +1 -1
  6. data/lib/bluepill/application/lifecycle.rb +44 -34
  7. data/lib/bluepill/application/listener.rb +1 -1
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +33 -29
  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 +45 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +32 -37
  16. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  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 +83 -84
  21. data/lib/bluepill/dsl/process_proxy.rb +61 -168
  22. data/lib/bluepill/dsl.rb +16 -15
  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 -16
  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 +55 -52
  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 -67
  46. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  47. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  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 +18 -69
  70. data/lib/bluepill/triggers/flapping.rb +12 -8
  71. data/lib/bluepill/units.rb +7 -21
  72. data/lib/bluepill/version.rb +12 -22
  73. data/lib/bluepill.rb +1 -1
  74. data/sig/bluepill/application.rbs +11 -12
  75. data/sig/bluepill/attributes.rbs +48 -0
  76. data/sig/bluepill/checks.rbs +40 -40
  77. data/sig/bluepill/dsl.rbs +97 -60
  78. data/sig/bluepill/errors.rbs +48 -0
  79. data/sig/bluepill/logger.rbs +15 -31
  80. data/sig/bluepill/process.rbs +39 -305
  81. data/sig/bluepill/process_commands.rbs +235 -0
  82. data/sig/bluepill/stop_signals.rbs +21 -0
  83. data/sig/bluepill/system.rbs +154 -99
  84. data/sig/bluepill/version.rbs +17 -0
  85. data/sig/bluepill.rbs +1 -17
  86. data/sig/manifest.yaml +1 -0
  87. metadata +21 -8
  88. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  89. data/lib/bluepill/deprecation/dsl.rb +0 -141
  90. data/lib/bluepill/deprecation/options.rb +0 -140
  91. data/lib/bluepill/deprecation.rb +0 -92
  92. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  93. data/sig/bluepill/deprecation.rbs +0 -87
@@ -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,93 +17,88 @@ module Bluepill
14
17
  #
15
18
  # @api public
16
19
  class AppProxy
17
- # The attributes that every process of the application gets, unless the
18
- # process sets its own
19
- APP_ATTRIBUTES = %i[working_dir uid gid supplementary_groups environment auto_start notify_command].freeze
20
-
21
- no_options = {} #: Hash[Symbol, untyped]
20
+ include DSL::Attributes
22
21
 
23
- # The options of an application that has none, made once
24
- #
25
- # @api private
26
- NO_OPTIONS = no_options.freeze
22
+ # Every process of the application gets these, unless it sets its own
23
+ attribute(*Process::Attributes::APP_ATTRIBUTES)
27
24
 
28
- # The directory that the application's processes start in
25
+ # @!attribute [rw] working_dir
26
+ # The directory that the application's processes start in
29
27
  #
30
- # @example
31
- # app.working_dir = '/srv/my_app'
28
+ # @example
29
+ # app.working_dir = '/srv/my_app'
32
30
  #
33
- # @return [String, nil] the directory, or nil for the default
31
+ # @return [String, nil] the directory, or nil for the default
34
32
  #
35
- # @api public
36
- attr_accessor :working_dir
33
+ # @api public
37
34
 
38
- # The user that the application's processes run as
35
+ # @!attribute [rw] uid
36
+ # The user that the application's processes run as
39
37
  #
40
- # @example
41
- # app.uid = 'deploy'
38
+ # @example
39
+ # app.uid = 'deploy'
42
40
  #
43
- # @return [String, nil] the user's name, or nil for bluepill's own user
41
+ # @return [String, nil] the user's name, or nil for bluepill's own user
44
42
  #
45
- # @api public
46
- attr_accessor :uid
43
+ # @api public
47
44
 
48
- # The group that the application's processes run as
45
+ # @!attribute [rw] gid
46
+ # The group that the application's processes run as
49
47
  #
50
- # @example
51
- # app.gid = 'deploy'
48
+ # @example
49
+ # app.gid = 'deploy'
52
50
  #
53
- # @return [String, nil] the group's name, or nil for bluepill's own group
51
+ # @return [String, nil] the group's name, or nil for bluepill's own
52
+ # group
54
53
  #
55
- # @api public
56
- attr_accessor :gid
54
+ # @api public
57
55
 
58
- # More groups that the application's processes run with
56
+ # @!attribute [rw] supplementary_groups
57
+ # More groups that the application's processes run with
59
58
  #
60
- # The processes already get every group that their uid belongs to. These
61
- # are added to those.
59
+ # The processes already get every group that their uid belongs to.
60
+ # These are added to those.
62
61
  #
63
- # @example
64
- # app.supplementary_groups = ['rvm', 'ssl-cert']
62
+ # @example
63
+ # app.supplementary_groups = ['rvm', 'ssl-cert']
65
64
  #
66
- # @return [Array<String>, nil] the groups' names
65
+ # @return [Array<String>, nil] the groups' names
67
66
  #
68
- # @api public
69
- attr_accessor :supplementary_groups
67
+ # @api public
70
68
 
71
- # The environment variables that the application's processes get
69
+ # @!attribute [rw] environment
70
+ # The environment variables that the application's processes get
72
71
  #
73
- # @example
74
- # app.environment = {'RAILS_ENV' => 'production'}
72
+ # @example
73
+ # app.environment = {'RAILS_ENV' => 'production'}
75
74
  #
76
- # @return [Hash{#to_s => #to_s}, nil] the variables by name
75
+ # @return [Hash{#to_s => #to_s}, nil] the variables by name
77
76
  #
78
- # @api public
79
- attr_accessor :environment
77
+ # @api public
80
78
 
81
- # Whether the application's processes start when bluepill loads
79
+ # @!attribute [rw] auto_start
80
+ # Whether the application's processes start when bluepill loads
82
81
  #
83
- # @example
84
- # app.auto_start = false
82
+ # @example
83
+ # app.auto_start = false
85
84
  #
86
- # @return [Boolean, nil] false to leave the processes unmonitored until
87
- # they're started
85
+ # @return [Boolean, nil] false to leave the processes unmonitored
86
+ # until they're started
88
87
  #
89
- # @api public
90
- attr_accessor :auto_start
88
+ # @api public
91
89
 
92
- # The command the application's processes run on every change of state
90
+ # @!attribute [rw] notify_command
91
+ # The command the application's processes run on each change of state
93
92
  #
94
- # A process can set its own. See Process#notify_command for the
95
- # environment variables it gets.
93
+ # A process can set its own. See Process#notify_command for the
94
+ # environment variables it gets.
96
95
  #
97
- # @example
98
- # app.notify_command = '/usr/local/bin/alert'
96
+ # @example
97
+ # app.notify_command = '/usr/local/bin/alert'
99
98
  #
100
- # @return [String, nil] the command, without a shell
99
+ # @return [String, nil] the command, without a shell
101
100
  #
102
- # @api public
103
- attr_accessor :notify_command
101
+ # @api public
104
102
 
105
103
  # Return the application that the config describes
106
104
  #
@@ -117,16 +115,18 @@ module Bluepill
117
115
  # @return [void]
118
116
  #
119
117
  # @api private
120
- def initialize(app_name, options = NO_OPTIONS)
121
- @app = Application.new(app_name.to_s, options)
118
+ def initialize(app_name, **options)
119
+ @name = app_name.to_s
120
+ @app = Application.new(@name, **options)
121
+ @attributes = {}
122
122
  @registry = ProcessFactory::Registry.new
123
123
  end
124
124
 
125
125
  # Define a process of the application
126
126
  #
127
127
  # The block runs with self set to a {ProcessProxy}, and also gets the
128
- # proxy as an argument. The process starts with the application's
129
- # 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.
130
130
  #
131
131
  # @example
132
132
  # app.process('web') do |process|
@@ -145,12 +145,16 @@ module Bluepill
145
145
  #
146
146
  # @api public
147
147
  def process(process_name, &process_block)
148
- attributes = {} #: Hash[Symbol, untyped]
149
- APP_ATTRIBUTES.each { |a| attributes[a] = public_send(a) }
150
-
151
148
  process_factory = ProcessFactory.new(attributes, process_block, @registry)
152
- 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)
153
150
  @app.add_process(process, group)
154
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}'"
155
159
  end
156
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
- # Only the keys matter
21
- @process_keys = {} #: Hash[Array[String?], nil]
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