bluepill 0.2.7 → 0.2.9

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.
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module Deprecation
5
+ # The attributes that 0.3.0 doesn't let a process block set
6
+ CHILD_ATTRIBUTES = %i[monitor_children child_process_factory].freeze
7
+
8
+ # Warn about a way of setting a process attribute that 0.3.0 removes
9
+ #
10
+ # Bluepill 0.3.0 sets an attribute only with `name = value`, so
11
+ # `name!` and `name value` fail, and a process block can't set
12
+ # monitor_children or child_process_factory.
13
+ #
14
+ # @param [Symbol] name the method the pill file called, such as
15
+ # :daemonize!
16
+ # @param [Symbol] key the attribute, such as :daemonize
17
+ #
18
+ # @return [void]
19
+ #
20
+ # @api private
21
+ def self.check_attribute(name, key)
22
+ if name.end_with?("!")
23
+ warn("bluepill 0.3.0 removes process.#{name}. Write process.#{key} = true, which 0.2 takes too.")
24
+ elsif !name.end_with?("=")
25
+ warn("bluepill 0.3.0 removes process.#{key} value. Write process.#{key} = value, which 0.2 takes too.")
26
+ end
27
+ return unless CHILD_ATTRIBUTES.include?(key)
28
+
29
+ warn("bluepill 0.3.0 doesn't let a process block set #{key}. " \
30
+ "Call process.monitor_children with a block that configures the children, which 0.2 takes too.")
31
+ end
32
+
33
+ # Warn about monitor_children without a block, which 0.3.0 rejects
34
+ #
35
+ # @param [String] process_name the process's name
36
+ # @param [Proc, nil] block the block that configures the children
37
+ #
38
+ # @return [void]
39
+ #
40
+ # @api private
41
+ def self.check_monitor_children(process_name, block)
42
+ return if block
43
+
44
+ warn("bluepill 0.3.0 fails the config when monitor_children has no block, " \
45
+ "as for process '#{process_name}'. Give it a block that sets the children's stop_command.")
46
+ end
47
+
48
+ # Warn about a monitor_children block that sets no stop_command
49
+ #
50
+ # @param [String] parent_name the parent process's name
51
+ # @param [Hash{Symbol => Object}] attributes the attributes that the
52
+ # block set on a child
53
+ #
54
+ # @return [void]
55
+ #
56
+ # @api private
57
+ def self.check_child_stop_command(parent_name, attributes)
58
+ return if attributes.key?(:stop_command)
59
+
60
+ warn("bluepill 0.3.0 fails the config when a monitor_children block sets no stop_command, " \
61
+ "as for the children of '#{parent_name}', where 0.2 exits the daemon when it first finds a child. " \
62
+ "Set the children's stop_command in the block.")
63
+ end
64
+
65
+ # Warn about a daemon older than 0.2.9, which 0.3.0's CLI refuses
66
+ #
67
+ # A daemon older than 0.2.9 answers the CLI with its Version module,
68
+ # which Marshal loads as the CLI's own.
69
+ #
70
+ # @param [Module, String] version what the daemon answered
71
+ #
72
+ # @return [void]
73
+ #
74
+ # @api private
75
+ def self.check_daemon_version(version)
76
+ return if version.instance_of?(String)
77
+
78
+ warn("this daemon runs a bluepill older than 0.2.9, which the CLI of bluepill 0.3.0 says is out of date. " \
79
+ "Run bluepill load again, so the daemon runs this version.")
80
+ end
81
+
82
+ # The options that 0.3.0's Bluepill.application takes
83
+ APPLICATION_OPTIONS = %i[foreground log_file log_level base_dir kill_timeout].freeze
84
+
85
+ # Warn about options passed as a Hash, without **
86
+ #
87
+ # Bluepill 0.3.0 takes the options of Bluepill.application and checks
88
+ # as keyword arguments, and Ruby 3 keeps a Hash passed without ** as a
89
+ # positional argument, which fails. Ruby 2.7 passes such a Hash as
90
+ # keywords, so it can't tell, and warns about none.
91
+ #
92
+ # @param [Hash{Symbol => Object}, nil] options the options passed as a
93
+ # Hash, or nil for none
94
+ # @param [Hash{Symbol => Object}] keywords the options passed as
95
+ # keywords
96
+ # @param [String] call the call, as the message shows it, such as
97
+ # "process.checks :mem_usage"
98
+ #
99
+ # @return [void]
100
+ #
101
+ # @raise [ArgumentError] for options passed both ways, which 0.2.7
102
+ # rejected as too many arguments
103
+ #
104
+ # @api private
105
+ def self.check_keywords(options, keywords, call)
106
+ return unless options
107
+ raise ArgumentError, "wrong number of arguments (given 3, expected 1..2)" unless keywords.empty?
108
+
109
+ warn("bluepill 0.3.0 takes the options of #{call} only as keyword arguments, " \
110
+ "so options kept in a Hash fail unless they're passed with **. Write #{call}, **options, which 0.2 takes too.")
111
+ end
112
+
113
+ # Warn about an application option that 0.3.0 rejects
114
+ #
115
+ # @param [Hash{Symbol => Object}] options the application's options
116
+ #
117
+ # @return [void]
118
+ #
119
+ # @api private
120
+ def self.check_application_options(options)
121
+ (options.keys - APPLICATION_OPTIONS).each do |option|
122
+ warn("bluepill 0.3.0 rejects the application option #{option}:, which 0.2 ignores. " \
123
+ "Remove it, or fix its name.")
124
+ end
125
+ end
126
+
127
+ # Warn about a second check of the same name in a process
128
+ #
129
+ # Bluepill 0.2 keeps only the last of them, and 0.3.0 runs them all.
130
+ #
131
+ # @param [Symbol] name the check's name
132
+ #
133
+ # @return [void]
134
+ #
135
+ # @api private
136
+ def self.check_duplicate_check(name)
137
+ warn("bluepill 0.3.0 runs every check of a process, so a process with two checks #{name.inspect} runs both, " \
138
+ "where 0.2 runs only the last. Remove the one you don't want.")
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bluepill
4
+ module Deprecation
5
+ # The events that 0.3.0 takes for fires: and on_start_timeout
6
+ EVENTS = %i[start stop unmonitor restart].freeze
7
+
8
+ # The events, as a message lists them
9
+ EVENTS_LIST = ":start, :stop, :unmonitor or :restart"
10
+
11
+ no_options = [] #: Array[Symbol]
12
+
13
+ # The options of a check that takes none of its own, made once
14
+ NO_OPTIONS = no_options.freeze
15
+
16
+ # The options that each of bluepill's checks and triggers takes in 0.3.0
17
+ CHECK_OPTIONS = {
18
+ always_true: NO_OPTIONS,
19
+ cpu_usage: %i[below],
20
+ file_time: %i[filename below],
21
+ flapping: %i[times within retry_in],
22
+ http: %i[url kind pattern timeout open_timeout read_timeout verify_ssl],
23
+ mem_usage: %i[below],
24
+ running_time: %i[below],
25
+ zombie_process: NO_OPTIONS
26
+ }.freeze
27
+
28
+ # The options that 0.3.0 needs, of the checks that need any
29
+ #
30
+ # An http check's url: isn't here, since 0.2 already fails without it.
31
+ REQUIRED_OPTIONS = {
32
+ cpu_usage: %i[below],
33
+ file_time: %i[filename below],
34
+ mem_usage: %i[below],
35
+ running_time: %i[below]
36
+ }.freeze
37
+
38
+ # Warn about the options of a check that 0.3.0 rejects or needs
39
+ #
40
+ # Bluepill 0.3.0 fails the config for an option that a check doesn't
41
+ # take, such as a misspelled one or within: on an http check, and for
42
+ # one it needs and doesn't have, such as below: on a mem_usage check.
43
+ # A custom check's options are its own business.
44
+ #
45
+ # @param [Symbol, String] name the check's name, such as :mem_usage
46
+ # @param [Hash{Symbol => Object}] options the check's options
47
+ # @param [Array<Symbol>] taken the options that bluepill takes for
48
+ # itself, ConditionWatch::WATCH_KEYS, or none for a trigger
49
+ #
50
+ # @return [void]
51
+ #
52
+ # @api private
53
+ def self.check_options(name, options, taken)
54
+ check = name.to_sym
55
+ known = CHECK_OPTIONS[check]
56
+ return unless known
57
+
58
+ warn_unknown_options(check, options.keys - taken - known)
59
+ warn_missing_options(check, REQUIRED_OPTIONS.fetch(check, NO_OPTIONS) - options.keys)
60
+ end
61
+
62
+ # Warn about the options of one of bluepill's checks that it doesn't take
63
+ #
64
+ # @param [Symbol] check the check's name
65
+ # @param [Array<Object>] unknown the options
66
+ #
67
+ # @return [void]
68
+ #
69
+ # @api private
70
+ def self.warn_unknown_options(check, unknown)
71
+ unknown.each do |option|
72
+ warn("bluepill 0.3.0 rejects the option #{option}: of checks #{check.inspect}, which 0.2 ignores. " \
73
+ "Remove it, or fix its name.")
74
+ end
75
+ end
76
+
77
+ # Warn about the options that one of bluepill's checks needs
78
+ #
79
+ # @param [Symbol] check the check's name
80
+ # @param [Array<Symbol>] missing the options it doesn't have
81
+ #
82
+ # @return [void]
83
+ #
84
+ # @api private
85
+ def self.warn_missing_options(check, missing)
86
+ missing.each do |option|
87
+ warn("bluepill 0.3.0 fails the config when checks #{check.inspect} has no #{option}: option, " \
88
+ "which it needs to check anything in 0.2 too. Add it.")
89
+ end
90
+ end
91
+
92
+ # Warn about a watch's options and events that 0.3.0 rejects
93
+ #
94
+ # @param [Symbol, String] name the check's name
95
+ # @param [Hash{Symbol => Object}] options the watch's options
96
+ # @param [Array<Object>] events the events it fires
97
+ #
98
+ # @return [void]
99
+ #
100
+ # @api private
101
+ def self.check_watch(name, options, events)
102
+ check_options(name, options, ConditionWatch::WATCH_KEYS)
103
+ check_fires(name, events)
104
+ end
105
+
106
+ # Warn about events of a check's fires: that 0.3.0 rejects
107
+ #
108
+ # @param [Symbol, String] name the check's name
109
+ # @param [Array<Object>] events the events it fires
110
+ #
111
+ # @return [void]
112
+ #
113
+ # @api private
114
+ def self.check_fires(name, events)
115
+ unknown = events - EVENTS
116
+ return if unknown.empty?
117
+
118
+ warn("bluepill 0.3.0 takes fires: only as #{EVENTS_LIST}, so fires: #{unknown.collect(&:inspect).join(", ")} " \
119
+ "of checks #{name.to_sym.inspect} fails the config. Use the Symbols, such as fires: :stop.")
120
+ end
121
+
122
+ # Warn about a process's on_start_timeout that 0.3.0 rejects
123
+ #
124
+ # @param [String] process_name the process's name
125
+ # @param [Hash{Symbol => Object}] attributes the process's attributes
126
+ #
127
+ # @return [void]
128
+ #
129
+ # @api private
130
+ def self.check_on_start_timeout(process_name, attributes)
131
+ return unless attributes.key?(:on_start_timeout)
132
+
133
+ event = attributes.fetch(:on_start_timeout)
134
+ return if EVENTS.include?(event)
135
+
136
+ warn("bluepill 0.3.0 takes on_start_timeout only as #{EVENTS_LIST}, so process '#{process_name}''s " \
137
+ "on_start_timeout of #{event.inspect} fails the config. Use the Symbols, such as on_start_timeout = :unmonitor.")
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Bluepill
6
+ # Warn, once each, about what bluepill 0.3.0 changes
7
+ #
8
+ # `bluepill load` loads a pill file before the daemon detaches, so a
9
+ # warning goes to stderr, where the user sees it, and to bluepill's log at
10
+ # warning, once there's a logger. Each warning names what it's about, so
11
+ # it's given once for each message.
12
+ #
13
+ # @example
14
+ # Bluepill::Deprecation.warn("bluepill 0.3.0 rejects the check option evry:. Remove it, or fix its name.")
15
+ # # DEPRECATION: bluepill 0.3.0 rejects the check option evry:. Remove it, or fix its name.
16
+ #
17
+ # @api private
18
+ module Deprecation
19
+ # The major version of the Ruby that bluepill 0.3.0 needs, or newer
20
+ RUBY_REQUIRED = 3
21
+
22
+ @warned = Set.new #: Set[String]
23
+ @logger = nil #: Logger?
24
+
25
+ # Warn about a change, unless the same warning was given
26
+ #
27
+ # @param [String] message what changes, in which version, and what to
28
+ # do now
29
+ #
30
+ # @return [void]
31
+ #
32
+ # @api private
33
+ def self.warn(message)
34
+ return unless @warned.add?(message)
35
+
36
+ line = "DEPRECATION: #{message}"
37
+ $stderr.puts(line)
38
+ @logger&.warning(line)
39
+ end
40
+
41
+ # Return the logger the warnings also go to
42
+ #
43
+ # @return [Logger, nil] the application's logger, or nil before there's
44
+ # one
45
+ #
46
+ # @api private
47
+ def self.logger
48
+ @logger
49
+ end
50
+
51
+ # Set the logger the warnings also go to
52
+ #
53
+ # @param [Logger, nil] logger the application's logger
54
+ #
55
+ # @return [void]
56
+ #
57
+ # @api private
58
+ def self.logger=(logger)
59
+ @logger = logger
60
+ end
61
+
62
+ # Forget the warnings given and the logger
63
+ #
64
+ # The specs call it before each example.
65
+ #
66
+ # @return [void]
67
+ #
68
+ # @api private
69
+ def self.reset!
70
+ @warned.clear
71
+ @logger = nil
72
+ end
73
+
74
+ # Warn when bluepill runs on a Ruby that 0.3.0 doesn't support
75
+ #
76
+ # @param [String] version the Ruby's version
77
+ #
78
+ # @return [void]
79
+ #
80
+ # @api private
81
+ def self.check_ruby(version = RUBY_VERSION)
82
+ return unless version.to_i < RUBY_REQUIRED
83
+
84
+ warn("bluepill 0.3.0 needs Ruby 3.0 or newer, or JRuby 9.4 or newer, and this is Ruby #{version}. " \
85
+ "Upgrade Ruby before you upgrade bluepill, or stay on 0.2.9.")
86
+ end
87
+ end
88
+ end
89
+
90
+ require "bluepill/deprecation/custom_checks"
91
+ require "bluepill/deprecation/options"
92
+ require "bluepill/deprecation/dsl"
@@ -18,6 +18,13 @@ module Bluepill
18
18
  # process sets its own
19
19
  APP_ATTRIBUTES = %i[working_dir uid gid supplementary_groups environment auto_start notify_command].freeze
20
20
 
21
+ no_options = {} #: Hash[Symbol, untyped]
22
+
23
+ # The options of an application that has none, made once
24
+ #
25
+ # @api private
26
+ NO_OPTIONS = no_options.freeze
27
+
21
28
  # The directory that the application's processes start in
22
29
  #
23
30
  # @example
@@ -110,7 +117,7 @@ module Bluepill
110
117
  # @return [void]
111
118
  #
112
119
  # @api private
113
- def initialize(app_name, options)
120
+ def initialize(app_name, options = NO_OPTIONS)
114
121
  @app = Application.new(app_name.to_s, options)
115
122
  @registry = ProcessFactory::Registry.new
116
123
  end
@@ -36,6 +36,32 @@ module Bluepill
36
36
  process
37
37
  end
38
38
 
39
+ # Warn about what 0.3.0 rejects in the children's block
40
+ #
41
+ # It runs while the pill file loads.
42
+ #
43
+ # Bluepill 0.3.0 runs the block once when the pill file loads, for a
44
+ # child whose pid isn't known yet, and fails the config for a mistake
45
+ # in it, such as a missing stop_command, where 0.2 exits the daemon
46
+ # the first time it finds a child. This runs the block the same way,
47
+ # to warn about the mistakes and about the checks that the children
48
+ # get. A block that raises for such a child, or no block, which
49
+ # monitor_children has warned about, warns about nothing.
50
+ #
51
+ # @param [String] parent_name the parent process's name
52
+ #
53
+ # @return [void]
54
+ #
55
+ # @api private
56
+ def check_children(parent_name)
57
+ child = ProcessProxy.new("<child of #{parent_name}>", inherited_attributes, @process_block)
58
+ Deprecation.check_on_start_timeout(child.name, child.attributes)
59
+ Deprecation.check_child_stop_command(parent_name, child.attributes)
60
+ child.to_process
61
+ rescue
62
+ nil
63
+ end
64
+
39
65
  protected
40
66
 
41
67
  # Return the attributes a child gets from its parent
@@ -57,8 +57,9 @@ module Bluepill
57
57
  default_pid_file = assign_default_pid_file(attributes, name, pids_dir)
58
58
 
59
59
  process = ProcessProxy.new(name, attributes, @process_block)
60
+ Deprecation.check_on_start_timeout(name, attributes)
60
61
  child_process_block = attributes.delete(:child_process_block)
61
- attributes[:child_process_factory] = ProcessFactory.new(attributes, child_process_block) if attributes[:monitor_children]
62
+ attributes[:child_process_factory] = child_factory(name, attributes, child_process_block) if attributes[:monitor_children]
62
63
 
63
64
  validate_process! process
64
65
  warn_unless_pid_known(process, default_pid_file)
@@ -67,6 +68,22 @@ module Bluepill
67
68
 
68
69
  protected
69
70
 
71
+ # Create the factory of a process's children, and check their block
72
+ #
73
+ # @param [String] name the process's name
74
+ # @param [Hash{Symbol => Object}] attributes the process's attributes
75
+ # @param [Proc, nil] child_process_block the block that configures each
76
+ # child
77
+ #
78
+ # @return [ProcessFactory] the children's factory
79
+ #
80
+ # @api private
81
+ def child_factory(name, attributes, child_process_block)
82
+ factory = ProcessFactory.new(attributes, child_process_block)
83
+ factory.check_children(name)
84
+ factory
85
+ end
86
+
70
87
  # Set the default pid file, unless the process sets its own
71
88
  #
72
89
  # @param [Hash{Symbol => Object}] attributes the process's attributes
@@ -6,13 +6,14 @@ module Bluepill
6
6
  # The self of a process block in a config file
7
7
  #
8
8
  # Any of the process's attributes can be set with `name = value`,
9
- # `name value`, or `name!` for true, and read back with `name`. See
9
+ # `name value`, or `name!` for true, and read back with `name`. Bluepill
10
+ # 0.3.0 takes only `name = value`, so the others warn. See
10
11
  # {Process::CONFIGURABLE_ATTRIBUTES} for the attributes a process has.
11
12
  #
12
13
  # @example
13
14
  # app.process('web') do |process|
14
15
  # process.start_command = '/usr/bin/my_server'
15
- # process.daemonize!
16
+ # process.daemonize = true
16
17
  # process.checks :mem_usage, every: 10.seconds, below: 100.megabytes
17
18
  # end
18
19
  #
@@ -63,12 +64,13 @@ module Bluepill
63
64
  # A name ending in `=`, or one given a value, sets the attribute. A name
64
65
  # ending in `!` sets it to true. A name alone reads an attribute that's
65
66
  # set. Only the attributes a process has can be set, so a misspelled one
66
- # fails when the config loads instead of being ignored.
67
+ # fails when the config loads instead of being ignored. Bluepill 0.3.0
68
+ # removes the `name value` and `name!` forms, so they warn.
67
69
  #
68
70
  # @example
69
71
  # process.start_command = '/usr/bin/my_server'
70
- # process.stop_grace_time 10.seconds
71
- # process.daemonize!
72
+ # process.stop_grace_time 10.seconds # warns
73
+ # process.daemonize! # warns
72
74
  # process.start_command # => "/usr/bin/my_server"
73
75
  #
74
76
  # @param [Symbol] name the attribute's name
@@ -86,6 +88,7 @@ module Bluepill
86
88
  return @attributes.fetch(key) if reader && @attributes.key?(key)
87
89
 
88
90
  unknown_attribute!(key) unless self.class.attribute?(key)
91
+ Deprecation.check_attribute(name, key) unless reader
89
92
  return @attributes[key] = value unless reader
90
93
  end
91
94
  # A call with the wrong number of values, or the reader of a known
@@ -141,14 +144,20 @@ module Bluepill
141
144
  # process.checks :flapping, times: 2, within: 30.seconds, retry_in: 7.seconds
142
145
  #
143
146
  # @param [Symbol] name the check's name, such as :mem_usage
144
- # @param [Hash{Symbol => Object}] options the check's options, such as
147
+ # @param [Hash{Symbol => Object}, nil] options the check's options, as
148
+ # a Hash, which bluepill 0.3.0 doesn't take
149
+ # @param [Hash{Symbol => Object}] keywords the check's options, such as
145
150
  # :every, :below and :times
146
151
  #
147
152
  # @return [Hash{Symbol => Object}] the options
148
153
  #
154
+ # @raise [ArgumentError] for options as a Hash and as keywords
155
+ #
149
156
  # @api public
150
- def checks(name, options = {})
151
- @watches[name] = options
157
+ def checks(name, options = nil, **keywords)
158
+ Deprecation.check_keywords(options, keywords, "process.checks #{name.inspect}")
159
+ Deprecation.check_duplicate_check(name) if @watches.key?(name)
160
+ @watches[name] = options || keywords
152
161
  end
153
162
 
154
163
  # Monitor the process's children too
@@ -170,6 +179,7 @@ module Bluepill
170
179
  #
171
180
  # @api public
172
181
  def monitor_children(&child_process_block)
182
+ Deprecation.check_monitor_children(@name, child_process_block)
173
183
  @attributes[:monitor_children] = true
174
184
  @attributes[:child_process_block] = child_process_block
175
185
  end
data/lib/bluepill/dsl.rb CHANGED
@@ -16,8 +16,10 @@ module Bluepill
16
16
  # end
17
17
  #
18
18
  # @param [#to_s] app_name the application's name
19
- # @param [Hash{Symbol => Object}] options the application's options, such
20
- # as :log_file, :foreground and :base_dir
19
+ # @param [Hash{Symbol => Object}, nil] options the application's options,
20
+ # as a Hash, which bluepill 0.3.0 doesn't take
21
+ # @param [Hash{Symbol => Object}] keywords the application's options,
22
+ # such as :log_file, :foreground and :base_dir
21
23
  #
22
24
  # @yield [app] describes the application's processes
23
25
  # @yieldparam [AppProxy] app the application's proxy, which is also self
@@ -25,9 +27,15 @@ module Bluepill
25
27
  #
26
28
  # @return [void]
27
29
  #
30
+ # @raise [ArgumentError] for options as a Hash and as keywords
31
+ #
28
32
  # @api public
29
- def self.application(app_name, options = {}, &block)
33
+ def self.application(app_name, options = nil, **keywords, &block)
34
+ Deprecation.check_keywords(options, keywords, "Bluepill.application #{app_name.to_s.inspect}")
35
+ options ||= keywords
30
36
  app_proxy = AppProxy.new(app_name, options)
37
+ Deprecation.check_ruby
38
+ Deprecation.check_application_options(options)
31
39
  # instance_eval also passes the proxy to a block that takes an argument
32
40
  app_proxy.instance_eval(&block)
33
41
  app_proxy.app.load
@@ -55,20 +55,17 @@ module Bluepill
55
55
 
56
56
  # Tell every trigger about a transition
57
57
  #
58
+ # An error a trigger raises stops the transition, and tick logs it.
59
+ #
58
60
  # @param [#from_name, #to_name, #loopback?] transition the transition
59
61
  #
60
62
  # @return [void]
61
63
  #
62
- # @raise [StandardError] what a trigger raised, after logging it
64
+ # @raise [StandardError] what a trigger raised
63
65
  #
64
66
  # @api private
65
67
  def notify_triggers(transition)
66
- triggers.each do |trigger|
67
- trigger.notify(transition)
68
- rescue => e
69
- logger.err e.backtrace
70
- raise
71
- end
68
+ triggers.each { |trigger| trigger.notify(transition) }
72
69
  end
73
70
 
74
71
  # Clear the watches' history after the process changes state
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bluepill/system"
4
+ require "bluepill/util/log_error"
4
5
  require "bluepill/process_journal"
5
6
  require "bluepill/process_commands"
6
7
  require "bluepill/process/attributes"
@@ -138,28 +139,39 @@ module Bluepill
138
139
  # Unless ticks are being skipped, fire the state machine's tick event, and
139
140
  # then, for a process that's up, run its watches and tick its children.
140
141
  #
142
+ # An error, such as one a trigger raised, is logged with its backtrace,
143
+ # and the process ticks again next time, so one broken check or trigger
144
+ # doesn't stop the daemon.
145
+ #
141
146
  # @return [void]
142
147
  #
143
148
  # @api private
144
149
  def tick
145
150
  return if skipping_ticks?
146
151
 
147
- self.skip_ticks_until = nil
148
-
152
+ @skip_ticks_until = nil
149
153
  # clear the memoization per tick
150
154
  @process_running = nil
151
-
152
155
  # Deal with thread cleanup here since the stopping state isn't used
153
156
  clean_threads if unmonitored?
154
-
155
157
  fire_event(:tick)
156
-
157
158
  return unless up?
158
159
 
159
160
  run_watches
161
+ tick_children if monitor_children?
162
+ rescue => e
163
+ Util.log_error(logger, "tick failed", e)
164
+ end
160
165
 
161
- return unless monitor_children?
162
-
166
+ # Find the process's children, and tick each of them
167
+ #
168
+ # Each child's tick logs its own errors, so one that fails doesn't keep
169
+ # the others from ticking.
170
+ #
171
+ # @return [void]
172
+ #
173
+ # @api private
174
+ def tick_children
163
175
  refresh_children!
164
176
  children.each(&:tick)
165
177
  end
@@ -232,7 +244,7 @@ module Bluepill
232
244
  #
233
245
  # @api private
234
246
  def add_trigger(name, options)
235
- triggers << Trigger[name].new(self, options.merge(logger: logger))
247
+ triggers << Trigger[name].create(self, name, **options, logger: logger)
236
248
  end
237
249
 
238
250
  # Run every watch that's due, and dispatch the events they fire