bluepill 0.1.5 → 0.1.7

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.
@@ -78,12 +78,12 @@ module Bluepill
78
78
 
79
79
  # Return the triggers notified of the process's transitions
80
80
  #
81
- # @return [Array<Trigger>]
81
+ # @return [Array<Trigger, Trigger::Custom>]
82
82
  #
83
83
  # @api private
84
84
  attr_accessor :triggers
85
85
 
86
- # Return the time, in epoch seconds, before which ticks are skipped
86
+ # Return the time, from Clock.now, before which ticks are skipped
87
87
  #
88
88
  # Set while a start, stop or restart command has its grace time to take
89
89
  # effect.
@@ -467,6 +467,10 @@ module Bluepill
467
467
  # Unless ticks are being skipped, fire the state machine's tick event, and
468
468
  # then, for a process that's up, run its watches and tick its children.
469
469
  #
470
+ # An error, such as one a trigger raised, is logged with its backtrace,
471
+ # and the process ticks again next time, so one broken check or trigger
472
+ # doesn't stop the daemon.
473
+ #
470
474
  # @return [void]
471
475
  #
472
476
  # @api private
@@ -493,6 +497,8 @@ module Bluepill
493
497
 
494
498
  refresh_children!
495
499
  children.each(&:tick)
500
+ rescue StandardError => e
501
+ Util.log_error(logger, 'tick failed', e)
496
502
  end
497
503
 
498
504
  # Set the logger, for the process and for its watches and triggers
@@ -551,22 +557,17 @@ module Bluepill
551
557
 
552
558
  # Tell every trigger about a transition
553
559
  #
560
+ # An error a trigger raises stops the transition, and tick logs it.
561
+ #
554
562
  # @param [#from_name, #to_name, #loopback?] transition the transition
555
563
  #
556
564
  # @return [void]
557
565
  #
558
- # @raise [StandardError] what a trigger raised, after logging it
566
+ # @raise [StandardError] what a trigger raised
559
567
  #
560
568
  # @api private
561
569
  def notify_triggers(transition)
562
- triggers.each do |trigger|
563
- begin
564
- trigger.notify(transition)
565
- rescue StandardError => e
566
- logger.err e.backtrace
567
- raise
568
- end
569
- end
570
+ triggers.each { |trigger| trigger.notify(transition) }
570
571
  end
571
572
 
572
573
  # Add a check to run on the process every interval
@@ -583,14 +584,19 @@ module Bluepill
583
584
 
584
585
  # Add a trigger to notify of the process's transitions
585
586
  #
587
+ # A trigger that a config defines gets transitions that warn about what
588
+ # bluepill 0.2.0's don't have. Bluepill's own, Flapping, gets them as
589
+ # they are.
590
+ #
586
591
  # @param [Symbol] name the trigger's name, such as :flapping
587
592
  # @param [Hash{Symbol => Object}] options the trigger's options
588
593
  #
589
- # @return [Array<Trigger>] the process's triggers
594
+ # @return [Array<Trigger, Trigger::Custom>] the process's triggers
590
595
  #
591
596
  # @api private
592
597
  def add_trigger(name, options)
593
- triggers << Trigger[name].new(self, options.merge(logger: logger))
598
+ trigger = Trigger[name].new(self, options.merge(logger: logger))
599
+ triggers << (trigger.instance_of?(Triggers::Flapping) ? trigger : Trigger::Custom.new(trigger))
594
600
  end
595
601
 
596
602
  # Run every watch, and dispatch the events they fire
@@ -602,7 +608,7 @@ module Bluepill
602
608
  #
603
609
  # @api private
604
610
  def run_watches
605
- now = Integer(Time.now)
611
+ now = Clock.now
606
612
 
607
613
  threads = watches.collect do |watch|
608
614
  [watch, Thread.new { watch.run(actual_pid, now) }] #: [ConditionWatch, Thread]
@@ -847,11 +853,11 @@ module Bluepill
847
853
  #
848
854
  # @param [Integer, Float] seconds how long to skip ticks for
849
855
  #
850
- # @return [Integer] when ticks resume, in epoch seconds
856
+ # @return [Integer] when ticks resume, from Clock.now
851
857
  #
852
858
  # @api private
853
859
  def skip_ticks_for(seconds)
854
- self.skip_ticks_until = (skip_ticks_until || Integer(Time.now)) + Integer(seconds)
860
+ self.skip_ticks_until = (skip_ticks_until || Clock.now) + Integer(seconds)
855
861
  end
856
862
 
857
863
  # Return whether ticks are being skipped
@@ -860,7 +866,7 @@ module Bluepill
860
866
  #
861
867
  # @api private
862
868
  def skipping_ticks?
863
- skip_ticks_until && skip_ticks_until > Integer(Time.now)
869
+ skip_ticks_until && skip_ticks_until > Clock.now
864
870
  end
865
871
 
866
872
  # Update the list of children from the tick's ps table
@@ -11,7 +11,7 @@ module Bluepill
11
11
  # and then on_start_timeout is dispatched. Ticks are skipped for
12
12
  # start_grace_time either way.
13
13
  #
14
- # @return [Integer] when ticks resume, in epoch seconds
14
+ # @return [Integer] when ticks resume, from Clock.now
15
15
  #
16
16
  # @api private
17
17
  def start_process
@@ -59,7 +59,7 @@ module Bluepill
59
59
  # Stop signals are sent from a thread of their own, with their delays.
60
60
  # Ticks are skipped for stop_grace_time.
61
61
  #
62
- # @return [Integer] when ticks resume, in epoch seconds
62
+ # @return [Integer] when ticks resume, from Clock.now
63
63
  #
64
64
  # @api private
65
65
  def stop_process
@@ -92,7 +92,7 @@ module Bluepill
92
92
 
93
93
  # Restart the process with its restart command, or by stopping and starting
94
94
  #
95
- # @return [Integer] when ticks resume, in epoch seconds
95
+ # @return [Integer] when ticks resume, from Clock.now
96
96
  #
97
97
  # @api private
98
98
  def restart_process
@@ -9,17 +9,23 @@ module Bluepill
9
9
  # response it should be (a Net::HTTP class name such as :success or
10
10
  # :ok, or a status code such as 200, :success by default), pattern: a
11
11
  # Regexp its body should match, and timeout: (or open_timeout: and
12
- # read_timeout:) in seconds, 5 by default.
12
+ # read_timeout:) in seconds, 5 by default. With verify_ssl: true, an
13
+ # https URL's certificate must be valid. Bluepill 0.1 doesn't verify it
14
+ # by default, but bluepill 0.3.0 does.
13
15
  #
14
16
  # @example
15
17
  # process.checks :http, url: 'http://localhost:4567/', kind: :success, pattern: /html/, timeout: 3.seconds
16
18
  #
19
+ # @example A server whose certificate must be valid
20
+ # process.checks :http, url: 'https://localhost:4443/', verify_ssl: true
21
+ #
17
22
  # @api private
18
23
  class Http < ProcessCondition
19
24
  # Create the check
20
25
  #
21
26
  # @param [Hash{Symbol => Object}] options the check's options, with
22
- # :url, :kind, :pattern, :timeout, :open_timeout and :read_timeout
27
+ # :url, :kind, :pattern, :timeout, :open_timeout, :read_timeout and
28
+ # :verify_ssl
23
29
  #
24
30
  # @return [void]
25
31
  #
@@ -43,11 +49,13 @@ module Bluepill
43
49
  @pattern = options[:pattern]
44
50
  @open_timeout = (options[:open_timeout] || options[:timeout] || 5).to_i
45
51
  @read_timeout = (options[:read_timeout] || options[:timeout] || 5).to_i
52
+ @verify_ssl = options[:verify_ssl]
53
+ warn_unverified unless options.key?(:verify_ssl)
46
54
  end
47
55
 
48
56
  # Get the URL
49
57
  #
50
- # HTTPS is checked without verifying the certificate.
58
+ # HTTPS verifies the server's certificate only with verify_ssl.
51
59
  #
52
60
  # @param [Integer, nil] _pid the process's pid
53
61
  # @param [Boolean] _include_children whether to include its children
@@ -62,7 +70,7 @@ module Bluepill
62
70
  if @uri.scheme.eql?('https')
63
71
  require 'net/https'
64
72
  session.use_ssl = true
65
- session.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+ session.verify_mode = @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
66
74
  end
67
75
  session.open_timeout = @open_timeout
68
76
  session.read_timeout = @read_timeout
@@ -93,6 +101,23 @@ module Bluepill
93
101
 
94
102
  private
95
103
 
104
+ # Warn that bluepill 0.3.0 verifies an https URL's certificate
105
+ #
106
+ # It warns about a check of an https URL that doesn't set verify_ssl,
107
+ # once for each URL.
108
+ #
109
+ # @return [void]
110
+ #
111
+ # @api private
112
+ def warn_unverified
113
+ return unless @uri.scheme.eql?('https')
114
+
115
+ message = "bluepill 0.3.0 verifies the TLS certificate of the http check of #{@uri}, where 0.1 and 0.2 don't. " \
116
+ 'Add verify_ssl: true to verify it now, or verify_ssl: false to keep checking a self-signed one.'
117
+ # The message names the URL, so it's the key
118
+ Deprecation.warn(message, message)
119
+ end
120
+
96
121
  # Run the block, raising a refused connection as one
97
122
  #
98
123
  # Net::HTTP raises NoMethodError (closed? for nil) when it can't
@@ -0,0 +1,147 @@
1
+ module Bluepill
2
+ class Trigger
3
+ # A trigger that a config defines, as a process's triggers hold it
4
+ #
5
+ # It hands the trigger each transition wrapped in a {Transition}, which
6
+ # warns when the trigger reads what bluepill 0.2.0's transitions don't
7
+ # have. Bluepill's own trigger only reads what they have, so the process
8
+ # holds it as it is, and its transitions cost nothing more.
9
+ #
10
+ # @api private
11
+ class Custom
12
+ # Return the trigger the config defined
13
+ #
14
+ # @return [Trigger]
15
+ #
16
+ # @api private
17
+ attr_reader :trigger
18
+
19
+ # Wrap a trigger that a config defined
20
+ #
21
+ # @param [Trigger] trigger the trigger
22
+ #
23
+ # @return [void]
24
+ #
25
+ # @api private
26
+ def initialize(trigger)
27
+ @trigger = trigger
28
+ end
29
+
30
+ # Tell the trigger about a transition, wrapped in a Transition
31
+ #
32
+ # The wrapper warns about what bluepill 0.2.0's transitions lack.
33
+ #
34
+ # @param [StateMachine::Transition] transition the transition
35
+ #
36
+ # @return [void]
37
+ #
38
+ # @api private
39
+ def notify(transition)
40
+ trigger.notify(Transition.new(transition, trigger.class))
41
+ end
42
+
43
+ # Cancel the trigger's scheduled events
44
+ #
45
+ # @return [void]
46
+ #
47
+ # @api private
48
+ def reset!
49
+ trigger.reset!
50
+ end
51
+
52
+ # Set the logger the trigger writes to
53
+ #
54
+ # @param [Logger] logger the process's logger
55
+ #
56
+ # @return [void]
57
+ #
58
+ # @api private
59
+ def logger=(logger)
60
+ trigger.logger = logger
61
+ end
62
+ end
63
+
64
+ # A transition, as a trigger that a config defines gets it
65
+ #
66
+ # Bluepill 0.2.0 gives a trigger's notify a transition with only
67
+ # from_name, to_name and loopback?. This one answers everything
68
+ # state_machine's does, but warns about anything else a trigger calls,
69
+ # such as from, to or event.
70
+ #
71
+ # @api private
72
+ class Transition
73
+ # Wrap a transition
74
+ #
75
+ # @param [StateMachine::Transition] transition the transition
76
+ # @param [Class] trigger_class the trigger's class, which the warning
77
+ # names
78
+ #
79
+ # @return [void]
80
+ #
81
+ # @api private
82
+ def initialize(transition, trigger_class)
83
+ @transition = transition
84
+ @trigger_class = trigger_class
85
+ end
86
+
87
+ # Return the state the process is leaving
88
+ #
89
+ # @return [Symbol]
90
+ #
91
+ # @api private
92
+ def from_name
93
+ @transition.from_name
94
+ end
95
+
96
+ # Return the state the process is going to
97
+ #
98
+ # @return [Symbol]
99
+ #
100
+ # @api private
101
+ def to_name
102
+ @transition.to_name
103
+ end
104
+
105
+ # Return whether the process stays in the same state
106
+ #
107
+ # @return [Boolean]
108
+ #
109
+ # @api private
110
+ def loopback?
111
+ @transition.loopback?
112
+ end
113
+
114
+ # Call one of the transition's other methods, with a warning
115
+ #
116
+ # @param [Symbol] name the method, such as :event
117
+ # @param [Array<Object>] args its arguments
118
+ # @param [Proc, nil] block its block
119
+ #
120
+ # @return [Object] what the transition's method returns
121
+ #
122
+ # @raise [NoMethodError] for a method the transition doesn't have
123
+ #
124
+ # @api private
125
+ def method_missing(name, *args, &block)
126
+ # The transition raises NoMethodError for a method it doesn't have
127
+ result = @transition.public_send(name, *args, &block)
128
+ message = "bluepill 0.2.0 gives a trigger's notify a transition with only from_name, to_name and loopback?, " \
129
+ "so #{@trigger_class}'s call of its #{name} fails there. Use from_name, to_name or loopback? instead."
130
+ Deprecation.warn([@trigger_class, name], message)
131
+ result
132
+ end
133
+
134
+ # Return whether the transition has a method
135
+ #
136
+ # @param [Symbol] name the method's name
137
+ # @param [Boolean] include_private whether to count private methods
138
+ #
139
+ # @return [Boolean]
140
+ #
141
+ # @api private
142
+ def respond_to_missing?(name, include_private)
143
+ @transition.respond_to?(name, include_private)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -129,8 +129,7 @@ module Bluepill
129
129
  scheduled_events.delete_if { |_, t| t.equal?(Thread.current) }
130
130
  end
131
131
  rescue StandardError => e
132
- logger.err(e)
133
- logger.err(e.backtrace.to_a.join("\n"))
132
+ Util.log_error(logger, "#{self.class.name.split('::').last} failed to dispatch #{event}", e)
134
133
  end
135
134
  end
136
135
  scheduled_events.push([event, thread])
@@ -86,7 +86,7 @@ module Bluepill
86
86
  def notify(transition)
87
87
  return unless TRIGGER_STATES.include?(transition.to_name)
88
88
 
89
- timeline << Integer(Time.now)
89
+ timeline << Clock.now
90
90
  check_flapping
91
91
  end
92
92
 
@@ -0,0 +1,29 @@
1
+ module Bluepill
2
+ # Small helpers for the rest of bluepill
3
+ #
4
+ # @api private
5
+ module Util
6
+ # Log an error at err, with a line of its own for each backtrace line
7
+ #
8
+ # The daemon rescues what a check, a trigger or a tick raises, so the
9
+ # log is the only place the error shows.
10
+ #
11
+ # @example
12
+ # Bluepill::Util.log_error(logger, 'mem_usage check failed', error)
13
+ # # mem_usage check failed: RuntimeError: boom
14
+ # # /path/to/check.rb:12:in `run'
15
+ #
16
+ # @param [Logger] logger the logger to write to
17
+ # @param [String] summary what failed, which the error's class and
18
+ # message follow
19
+ # @param [Exception] error the error
20
+ #
21
+ # @return [void]
22
+ #
23
+ # @api private
24
+ def self.log_error(logger, summary, error)
25
+ logger.err("#{summary}: #{error.class}: #{error}")
26
+ Array(error.backtrace).each { |line| logger.err(line) }
27
+ end
28
+ end
29
+ end
@@ -2,7 +2,7 @@ module Bluepill
2
2
  # The version of bluepill
3
3
  #
4
4
  # @example
5
- # Bluepill::Version.to_s # => "0.1.5"
5
+ # Bluepill::Version.to_s # => "0.1.7"
6
6
  #
7
7
  # @api public
8
8
  module Version
@@ -33,13 +33,13 @@ module Bluepill
33
33
  # Return the patch version
34
34
  #
35
35
  # @example
36
- # Bluepill::Version.patch # => 5
36
+ # Bluepill::Version.patch # => 7
37
37
  #
38
38
  # @return [Integer] the patch version
39
39
  #
40
40
  # @api public
41
41
  def self.patch
42
- 5
42
+ 7
43
43
  end
44
44
 
45
45
  # Return the pre-release version
@@ -55,7 +55,7 @@ module Bluepill
55
55
  # Return the version's parts by name
56
56
  #
57
57
  # @example
58
- # Bluepill::Version.to_h # => {major: 0, minor: 1, patch: 5, pre: nil}
58
+ # Bluepill::Version.to_h # => {major: 0, minor: 1, patch: 7, pre: nil}
59
59
  #
60
60
  # @return [Hash{Symbol => Integer, nil}] the major, minor, patch and
61
61
  # pre-release versions
@@ -73,7 +73,7 @@ module Bluepill
73
73
  # Return the version's parts, without a missing pre-release version
74
74
  #
75
75
  # @example
76
- # Bluepill::Version.to_a # => [0, 1, 5]
76
+ # Bluepill::Version.to_a # => [0, 1, 7]
77
77
  #
78
78
  # @return [Array<Integer>] the parts of the version
79
79
  #
@@ -85,9 +85,9 @@ module Bluepill
85
85
  # Return the version as a string
86
86
  #
87
87
  # @example
88
- # Bluepill::Version.to_s # => "0.1.5"
88
+ # Bluepill::Version.to_s # => "0.1.7"
89
89
  #
90
- # @return [String] the version, such as "0.1.5"
90
+ # @return [String] the version, such as "0.1.7"
91
91
  #
92
92
  # @api public
93
93
  def self.to_s
data/lib/bluepill.rb CHANGED
@@ -31,6 +31,9 @@ require 'monitor'
31
31
  require 'timeout'
32
32
  require 'logger'
33
33
 
34
+ require 'bluepill/clock'
35
+ require 'bluepill/deprecation'
36
+
34
37
  require 'active_support'
35
38
  require 'active_support/inflector'
36
39
  require 'active_support/core_ext/hash'
@@ -50,12 +53,14 @@ require 'bluepill/group'
50
53
  require 'bluepill/logger'
51
54
  require 'bluepill/condition_watch'
52
55
  require 'bluepill/trigger'
56
+ require 'bluepill/trigger/custom'
53
57
  require 'bluepill/triggers/flapping'
54
58
  require 'bluepill/dsl'
55
59
  require 'bluepill/system'
56
60
 
57
61
  require 'bluepill/process_conditions'
58
62
 
63
+ require 'bluepill/util/log_error'
59
64
  require 'bluepill/util/rotational_array'
60
65
 
61
66
  require 'bluepill/version'