wait 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ class Wait
2
+ class BaseCounter
3
+ attr_reader :attempt
4
+
5
+ def initialize(logger, total)
6
+ @logger = logger
7
+ # Attempt to prevent causing an infinite loop by being very strict about
8
+ # the value passed.
9
+ unless total.is_a?(Fixnum) and total > 0
10
+ raise(ArgumentError, "invalid number of attempts: #{total.inspect}")
11
+ end
12
+
13
+ @total = total
14
+ reset
15
+ end
16
+
17
+ # Called before all attempts to reset the counter.
18
+ def reset
19
+ @attempt = 0
20
+ end
21
+
22
+ # Called before each attempt to increment the counter.
23
+ def increment
24
+ @attempt += 1
25
+ log
26
+ end
27
+
28
+ # Returns +true+ if this is the last attempt.
29
+ def last_attempt?
30
+ @attempt == @total
31
+ end
32
+
33
+ # Logs the current attempt count.
34
+ def log
35
+ @logger.debug "[Counter] attempt #{self}"
36
+ end
37
+
38
+ # Returns a string representation of the current count.
39
+ def to_s
40
+ [@attempt, @total].join("/")
41
+ end
42
+ end # BaseCounter
43
+ end # Wait
@@ -0,0 +1,24 @@
1
+ class Wait
2
+ class BaseDelayer
3
+ def initialize(logger, initial_delay)
4
+ @logger = logger
5
+ @delay = initial_delay
6
+ end
7
+
8
+ # Called before a reattempt to sleep a certain about of time.
9
+ def sleep
10
+ log_delay
11
+ Kernel.sleep(@delay)
12
+ end
13
+
14
+ # Logs how long the delay is.
15
+ def log_delay
16
+ @logger.debug "[Delayer] delaying for #{self}"
17
+ end
18
+
19
+ # Returns a string representation of the delay.
20
+ def to_s
21
+ "#{@delay}s"
22
+ end
23
+ end # BaseDelayer
24
+ end # Wait
@@ -0,0 +1,12 @@
1
+ class Wait
2
+ class ExponentialDelayer < RegularDelayer
3
+ def sleep
4
+ super
5
+ increment
6
+ end
7
+
8
+ def increment
9
+ @delay *= 2
10
+ end
11
+ end # ExponentialDelayer
12
+ end # Wait
@@ -0,0 +1,3 @@
1
+ class Wait
2
+ class RegularDelayer < BaseDelayer; end
3
+ end # Wait
@@ -0,0 +1,16 @@
1
+ require "timeout"
2
+ require "logger"
3
+ require "forwardable"
4
+
5
+ require File.expand_path("../loggers/base", __FILE__)
6
+ require File.expand_path("../loggers/debug", __FILE__)
7
+ require File.expand_path("../counters/base", __FILE__)
8
+ require File.expand_path("../delayers/base", __FILE__)
9
+ require File.expand_path("../delayers/regular", __FILE__)
10
+ require File.expand_path("../delayers/exponential", __FILE__)
11
+ require File.expand_path("../testers/base", __FILE__)
12
+ require File.expand_path("../testers/passive", __FILE__)
13
+ require File.expand_path("../testers/truthy", __FILE__)
14
+ require File.expand_path("../rescuers/base", __FILE__)
15
+ require File.expand_path("../raisers/base", __FILE__)
16
+ require File.expand_path("../raisers/passive", __FILE__)
@@ -0,0 +1,32 @@
1
+ class Wait
2
+ class BaseLogger
3
+ extend Forwardable
4
+
5
+ attr_reader :logger
6
+ def_delegators :logger, :fatal,
7
+ :error,
8
+ :warn,
9
+ :info,
10
+ :debug
11
+
12
+ def initialize
13
+ @logger = ::Logger.new(STDOUT)
14
+ @logger.level = level
15
+ @logger.formatter = formatter
16
+ end
17
+
18
+ def level
19
+ ::Logger::WARN
20
+ end
21
+
22
+ def formatter
23
+ proc do |severity, datetime, program_name, message|
24
+ [severity.ljust(5), message].join(" ") + "\n"
25
+ end
26
+ end
27
+
28
+ def backtrace(backtrace)
29
+ backtrace.map { |line| (" " * 25) + line }.join("\n")
30
+ end
31
+ end # Logger
32
+ end # Wait
@@ -0,0 +1,7 @@
1
+ class Wait
2
+ class DebugLogger < BaseLogger
3
+ def level
4
+ ::Logger::DEBUG
5
+ end
6
+ end # Logger
7
+ end # Wait
@@ -0,0 +1,18 @@
1
+ class Wait
2
+ class BaseRaiser
3
+ def initialize(logger, exception)
4
+ @logger = logger
5
+ @exception = exception
6
+ log
7
+ end
8
+
9
+ # Returns +true+ if an exception ought to be raised.
10
+ def raise?
11
+ false
12
+ end
13
+
14
+ def log
15
+ @logger.debug "[Raiser] raise? #{@exception.class.name}: #{raise?}"
16
+ end
17
+ end # BaseRaiser
18
+ end # Wait
@@ -0,0 +1,3 @@
1
+ class Wait
2
+ class PassiveRaiser < BaseRaiser; end
3
+ end # Wait
@@ -0,0 +1,15 @@
1
+ class Wait
2
+ class BaseRescuer
3
+ attr_reader :exceptions
4
+
5
+ def initialize(logger, *exceptions)
6
+ @logger = logger
7
+ @exceptions = Array(exceptions).flatten
8
+ end
9
+
10
+ # Logs an exception.
11
+ def log(exception)
12
+ @logger.debug "[Rescuer] rescued: #{exception.class.name}: #{exception.message}\n#{@logger.backtrace(exception.backtrace)}"
13
+ end
14
+ end # BaseRescuer
15
+ end # Wait
@@ -0,0 +1,18 @@
1
+ class Wait
2
+ class BaseTester
3
+ def initialize(logger, result)
4
+ @logger = logger
5
+ @result = result
6
+ log
7
+ end
8
+
9
+ # Returns +true+ if a result if valid.
10
+ def valid?
11
+ true
12
+ end
13
+
14
+ def log
15
+ @logger.debug "[Tester] result: #{@result.inspect}"
16
+ end
17
+ end # BaseTester
18
+ end # Wait
@@ -0,0 +1,3 @@
1
+ class Wait
2
+ class PassiveTester < BaseTester; end
3
+ end # Wait
@@ -0,0 +1,7 @@
1
+ class Wait
2
+ class TruthyTester < BaseTester
3
+ def valid?
4
+ not (@result.nil? or @result == false)
5
+ end
6
+ end # TruthyTester
7
+ end # Wait
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wait
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-08 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: todd@paperlesspost.com
@@ -17,6 +17,19 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/counters/base.rb
21
+ - lib/delayers/base.rb
22
+ - lib/delayers/exponential.rb
23
+ - lib/delayers/regular.rb
24
+ - lib/initialize.rb
25
+ - lib/loggers/base.rb
26
+ - lib/loggers/debug.rb
27
+ - lib/raisers/base.rb
28
+ - lib/raisers/passive.rb
29
+ - lib/rescuers/base.rb
30
+ - lib/testers/base.rb
31
+ - lib/testers/passive.rb
32
+ - lib/testers/truthy.rb
20
33
  - lib/wait.rb
21
34
  homepage: http://github.com/paperlesspost/wait
22
35
  licenses: []