retries 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,11 +1,12 @@
1
1
  # Retries
2
2
 
3
- Retries is a small gem that provides a single function, `with_retries`, to evaluate a block with randomized,
3
+ Retries is a gem that provides a single function, `with_retries`, to evaluate a block with randomized,
4
4
  truncated, exponential backoff.
5
5
 
6
- There are similar projects out there (see [here](https://github.com/afazio/retry_block) and
7
- [here](https://bitbucket.org/amanking/retry_this/wiki/Home)) but these will require you to implement the
8
- backoff scheme yourself. If you don't need randomized exponential backoff, you should check out those gems.
6
+ There are similar projects out there (see [retry_block](https://github.com/afazio/retry_block) and
7
+ [retry_this](https://bitbucket.org/amanking/retry_this/wiki/Home), for example) but these will require you to
8
+ implement the backoff scheme yourself. If you don't need randomized exponential backoff, you should check out
9
+ those gems.
9
10
 
10
11
  ## Installation
11
12
 
@@ -19,7 +20,6 @@ or a flaky service. Here's how you can try it three times before failing:
19
20
 
20
21
  ``` ruby
21
22
  require "retries"
22
-
23
23
  with_retries(:max_tries => 3) { do_the_thing }
24
24
  ```
25
25
 
@@ -82,6 +82,18 @@ seconds:
82
82
  with_retries(:max_tries => 10, :base_sleep_seconds => 0.1, :max_sleep_seconds => 2.0) { do_the_thing }
83
83
  ```
84
84
 
85
+ ### Testing
86
+
87
+ In tests, you may wish to test that retries are being performed without any delay for sleeping:
88
+
89
+ ``` ruby
90
+ Retries.sleep_enabled = false
91
+ with_retries(:max_tries => 100) { raise "Boo!" } # Now this fails fast
92
+ ```
93
+
94
+ Of course, this will mask any errors to the `:base_sleep_seconds` and `:max_sleep_seconds` parameters, so use
95
+ with caution.
96
+
85
97
  ## Issues
86
98
 
87
99
  File tickets here on Github.
@@ -93,6 +105,11 @@ To run the tests: first clone the repo, then
93
105
  $ bundle install
94
106
  $ bundle exec rake test
95
107
 
108
+ ## Authors
109
+
110
+ * Harry Robertson
111
+ * Caleb Spare
112
+
96
113
  ## License
97
114
 
98
115
  Retries is released under the [MIT License](http://opensource.org/licenses/mit-license.php/).
@@ -1,5 +1,15 @@
1
1
  require "retries/version"
2
2
 
3
+ class Retries
4
+ class << self
5
+ # You can use this to turn off all sleeping in with_retries. This can be useful in tests. Defaults to
6
+ # `true`.
7
+ attr_accessor :sleep_enabled
8
+ end
9
+ end
10
+
11
+ Retries.sleep_enabled = true
12
+
3
13
  module Kernel
4
14
  # Runs the supplied code block an retries with an exponential backoff.
5
15
  #
@@ -36,15 +46,19 @@ module Kernel
36
46
  rescue *exception_types_to_rescue => exception
37
47
  raise exception if attempts >= max_tries
38
48
  handler.call(exception, attempts) if handler
39
- # The sleep time is an exponentially-increasing function of base_sleep_seconds. But, it never exceeds
40
- # max_sleep_seconds.
41
- sleep_seconds = [base_sleep_seconds * (2 ** (attempts - 1)), max_sleep_seconds].min
42
- # Randomize to a random value in the range sleep_seconds/2 .. sleep_seconds
43
- sleep_seconds = sleep_seconds * (0.5 * (1 + rand()))
44
- # But never sleep less than base_sleep_seconds
45
- sleep_seconds = [base_sleep_seconds, sleep_seconds].max
46
- sleep sleep_seconds
49
+ # Don't sleep at all if sleeping is disabled (used in testing).
50
+ if Retries.sleep_enabled
51
+ # The sleep time is an exponentially-increasing function of base_sleep_seconds. But, it never exceeds
52
+ # max_sleep_seconds.
53
+ sleep_seconds = [base_sleep_seconds * (2 ** (attempts - 1)), max_sleep_seconds].min
54
+ # Randomize to a random value in the range sleep_seconds/2 .. sleep_seconds
55
+ sleep_seconds = sleep_seconds * (0.5 * (1 + rand()))
56
+ # But never sleep less than base_sleep_seconds
57
+ sleep_seconds = [base_sleep_seconds, sleep_seconds].max
58
+ sleep sleep_seconds
59
+ end
47
60
  retry
48
61
  end
49
62
  end
50
63
  end
64
+
@@ -1,3 +1,3 @@
1
- module Retries
2
- VERSION = "0.0.1"
1
+ class Retries
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "scope"
2
2
  require "minitest/autorun"
3
+ require "timeout"
3
4
 
4
5
  $:.unshift File.join(File.dirname(__FILE__), "../lib")
5
6
  require "retries"
@@ -8,6 +9,10 @@ class CustomErrorA < RuntimeError; end
8
9
  class CustomErrorB < RuntimeError; end
9
10
 
10
11
  class RetriesTest < Scope::TestCase
12
+ setup do
13
+ Retries.sleep_enabled = true
14
+ end
15
+
11
16
  context "with_retries" do
12
17
  should "retry until successful" do
13
18
  tries = 0
@@ -69,5 +74,16 @@ class RetriesTest < Scope::TestCase
69
74
  assert_equal 4, tries
70
75
  assert_equal 3, exception_handler_run_times
71
76
  end
77
+
78
+ should "not sleep if Retries.sleep_enabled is false" do
79
+ Retries.sleep_enabled = false
80
+ assert_raises(RuntimeError) do # If we get a Timeout::Error, this won't pass.
81
+ Timeout.timeout(2) do
82
+ with_retries(:max_tries => 10, :base_sleep_seconds => 100, :max_sleep_seconds => 10000) do
83
+ raise "blah"
84
+ end
85
+ end
86
+ end
87
+ end
72
88
  end
73
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-15 00:00:00.000000000Z
12
+ date: 2012-10-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &19157880 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *19157880
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: scope
27
- requirement: &19157420 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *19157420
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: yard
38
- requirement: &19156980 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *19156980
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: redcarpet
49
- requirement: &19156540 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *19156540
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: Retries is a gem for retrying blocks with randomized exponential backoff.
59
79
  email:
60
80
  - caleb@ooyala.com
@@ -92,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
112
  version: '0'
93
113
  requirements: []
94
114
  rubyforge_project:
95
- rubygems_version: 1.8.10
115
+ rubygems_version: 1.8.23
96
116
  signing_key:
97
117
  specification_version: 3
98
118
  summary: Retries is a gem for retrying blocks with randomized exponential backoff.