retriable 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b679f765910990f95431fe330d2ec2a1340056d
4
- data.tar.gz: f3eb6ca16690e20b673d51cd7c93f240a846ab3f
3
+ metadata.gz: bfe5a1956e7b8921928094fd4306632da5852693
4
+ data.tar.gz: bc171d15fa1f55ce45151f6e6ce6976b2082a415
5
5
  SHA512:
6
- metadata.gz: 613b716a3e0a2164f03def1a0db227868733f066830130da7bc05afeff520948100288eef753f1e94387b7cdd365207202aa8ff38cac69def7680e60305ff70e
7
- data.tar.gz: c4ec29c385614fcbe2525b073e509a99b0e803ef889d8365ed45c7493167afa299424c55ec6bc8321d29769f76c2712c9b4fad8eb2f05358f2d4289e6536e990
6
+ metadata.gz: 80395340aa4890214aeb12f331904da265f42af80d4e8bd7400e532d7a47f4828abc659f7ae629a74f6d16ce77f1e88b23fc8518ca4531087d23a566ec5bcacc
7
+ data.tar.gz: c8385720ed1095508eae24dc918eb620ea4715b13d38ef7d2c921abd65f9fb05f6e95065307be644819405a1d9411e20961e95e06b734aae1333b54c3f45745d
@@ -5,12 +5,12 @@ language: ruby
5
5
  rvm:
6
6
  - 2.0.0
7
7
  - 2.1.10
8
- - 2.2.6
9
- - 2.3.3
10
- - 2.4.0
8
+ - 2.2.7
9
+ - 2.3.4
10
+ - 2.4.1
11
11
  - rbx
12
12
  - jruby-9.0.5.0
13
- - jruby-9.1.7.0
13
+ - jruby-9.1.8.0
14
14
  - ruby-head
15
15
  - jruby-head
16
16
 
@@ -23,7 +23,7 @@ matrix:
23
23
  before_install:
24
24
  - gem update --system
25
25
  - gem install bundler
26
-
26
+
27
27
  addons:
28
28
  code_climate:
29
29
  repo_token: 20a1139ef1830b4f813a10a03d90e8aa179b5226f75e75c5a949b25756ebf558
@@ -1,5 +1,9 @@
1
1
  ## HEAD
2
2
 
3
+ ## 3.0.2
4
+
5
+ * Add configuration and options validation.
6
+
3
7
  ## 3.0.1
4
8
  * Add `rubocop` linter to enforce coding styles for this library. Also, fix rule violations.
5
9
  * Removed `attr_reader :config` that caused a warning. @bruno-
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- #Retriable
1
+ # Retriable
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/kamui/retriable.svg)](http://travis-ci.org/kamui/retriable)
4
4
  [![Code Climate](https://codeclimate.com/github/kamui/retriable/badges/gpa.svg)](https://codeclimate.com/github/kamui/retriable)
@@ -15,16 +15,19 @@ module Retriable
15
15
  end
16
16
 
17
17
  def retriable(opts = {})
18
- tries = opts[:tries] || config.tries
19
- base_interval = opts[:base_interval] || config.base_interval
20
- max_interval = opts[:max_interval] || config.max_interval
21
- rand_factor = opts[:rand_factor] || config.rand_factor
22
- multiplier = opts[:multiplier] || config.multiplier
23
- max_elapsed_time = opts[:max_elapsed_time] || config.max_elapsed_time
24
- intervals = opts[:intervals] || config.intervals
25
- timeout = opts[:timeout] || config.timeout
26
- on = opts[:on] || config.on
27
- on_retry = opts[:on_retry] || config.on_retry
18
+ local_config = opts.empty? ? config : Config.new(config.to_h.merge(opts))
19
+
20
+ tries = local_config.tries
21
+ base_interval = local_config.base_interval
22
+ max_interval = local_config.max_interval
23
+ rand_factor = local_config.rand_factor
24
+ multiplier = local_config.multiplier
25
+ max_elapsed_time = local_config.max_elapsed_time
26
+ intervals = local_config.intervals
27
+ timeout = local_config.timeout
28
+ on = local_config.on
29
+ on_retry = local_config.on_retry
30
+ sleep_disabled = local_config.sleep_disabled
28
31
 
29
32
  start_time = Time.now
30
33
  elapsed_time = -> { Time.now - start_time }
@@ -58,7 +61,7 @@ module Retriable
58
61
  interval = intervals[index]
59
62
  on_retry.call(exception, try, elapsed_time.call, interval) if on_retry
60
63
  raise if try >= tries || (elapsed_time.call + interval) > max_elapsed_time
61
- sleep interval if config.sleep_disabled != true
64
+ sleep interval if sleep_disabled != true
62
65
  end
63
66
  end
64
67
  end
@@ -1,29 +1,43 @@
1
+ require_relative "exponential_backoff"
2
+
1
3
  module Retriable
2
4
  class Config
3
- attr_accessor :sleep_disabled
4
- attr_accessor :tries
5
- attr_accessor :base_interval
6
- attr_accessor :max_interval
7
- attr_accessor :rand_factor
8
- attr_accessor :multiplier
9
- attr_accessor :max_elapsed_time
10
- attr_accessor :intervals
11
- attr_accessor :timeout
12
- attr_accessor :on
13
- attr_accessor :on_retry
5
+ ATTRIBUTES = ExponentialBackoff::ATTRIBUTES + [
6
+ :sleep_disabled,
7
+ :max_elapsed_time,
8
+ :intervals,
9
+ :timeout,
10
+ :on,
11
+ :on_retry,
12
+ ].freeze
13
+
14
+ attr_accessor(*ATTRIBUTES)
15
+
16
+ def initialize(opts = {})
17
+ backoff = ExponentialBackoff.new
18
+
19
+ @tries = backoff.tries
20
+ @base_interval = backoff.base_interval
21
+ @max_interval = backoff.max_interval
22
+ @rand_factor = backoff.rand_factor
23
+ @multiplier = backoff.multiplier
24
+ @sleep_disabled = false
25
+ @max_elapsed_time = 900 # 15 min
26
+ @intervals = nil
27
+ @timeout = nil
28
+ @on = [StandardError]
29
+ @on_retry = nil
30
+
31
+ opts.each do |k, v|
32
+ raise ArgumentError, "#{k} is not a valid option" if !ATTRIBUTES.include?(k)
33
+ instance_variable_set(:"@#{k}", v)
34
+ end
35
+ end
14
36
 
15
- def initialize
16
- @sleep_disabled = false
17
- @tries = 3
18
- @base_interval = 0.5
19
- @max_interval = 60
20
- @rand_factor = 0.5
21
- @multiplier = 1.5
22
- @max_elapsed_time = 900 # 15 min
23
- @intervals = nil
24
- @timeout = nil
25
- @on = [StandardError]
26
- @on_retry = nil
37
+ def to_h
38
+ ATTRIBUTES.each_with_object({}) do |key, hash|
39
+ hash[key] = public_send(key)
40
+ end
27
41
  end
28
42
  end
29
43
  end
@@ -1,17 +1,26 @@
1
1
  module Retriable
2
2
  class ExponentialBackoff
3
- attr_accessor :tries
4
- attr_accessor :base_interval
5
- attr_accessor :multiplier
6
- attr_accessor :max_interval
7
- attr_accessor :rand_factor
3
+ ATTRIBUTES = [
4
+ :tries,
5
+ :base_interval,
6
+ :multiplier,
7
+ :max_interval,
8
+ :rand_factor,
9
+ ].freeze
10
+
11
+ attr_accessor(*ATTRIBUTES)
8
12
 
9
13
  def initialize(opts = {})
10
- @tries = opts[:tries] || Retriable.config.tries
11
- @base_interval = opts[:base_interval] || Retriable.config.base_interval
12
- @max_interval = opts[:max_interval] || Retriable.config.max_interval
13
- @rand_factor = opts[:rand_factor] || Retriable.config.rand_factor
14
- @multiplier = opts[:multiplier] || Retriable.config.multiplier
14
+ @tries = 3
15
+ @base_interval = 0.5
16
+ @max_interval = 60
17
+ @rand_factor = 0.5
18
+ @multiplier = 1.5
19
+
20
+ opts.each do |k, v|
21
+ raise ArgumentError, "#{k} is not a valid option" if !ATTRIBUTES.include?(k)
22
+ instance_variable_set(:"@#{k}", v)
23
+ end
15
24
  end
16
25
 
17
26
  def intervals
@@ -1,3 +1,3 @@
1
1
  module Retriable
2
- VERSION = "3.0.1".freeze
2
+ VERSION = "3.0.2".freeze
3
3
  end
@@ -44,4 +44,10 @@ describe Retriable::Config do
44
44
  it "on retry handler defaults to nil" do
45
45
  expect(subject.new.on_retry).must_be_nil
46
46
  end
47
+
48
+ it "raises errors on invalid configuration" do
49
+ assert_raises ArgumentError do
50
+ subject.new(does_not_exist: 123)
51
+ end
52
+ end
47
53
  end
@@ -364,4 +364,16 @@ describe Retriable do
364
364
 
365
365
  expect(tries).must_equal 2
366
366
  end
367
+
368
+ it "raises NoMethodError on invalid configuration" do
369
+ assert_raises NoMethodError do
370
+ Retriable.configure { |c| c.does_not_exist = 123 }
371
+ end
372
+ end
373
+
374
+ it "raises ArgumentError on invalid option on #retriable" do
375
+ assert_raises ArgumentError do
376
+ Retriable.retriable(does_not_exist: 123)
377
+ end
378
+ end
367
379
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retriable
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Chu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-19 00:00:00.000000000 Z
11
+ date: 2017-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  requirements: []
144
144
  rubyforge_project:
145
- rubygems_version: 2.6.8
145
+ rubygems_version: 2.6.11
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: Retriable is an simple DSL to retry failed code blocks with randomized exponential