retry-this 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,45 @@
1
+ RETRY_THIS
2
+ by Aman King
3
+
4
+ http://bitbucket.org/amanking/retry_this/
5
+
6
+ A module that allows a Ruby block to be tried a specified number of times in case specified types of errors are raised by the block. Especially helpful when dealing with blocks that invoke third-party systems that may be down.
7
+
8
+ Eg:
9
+ require 'retry_this'
10
+ class Gateway
11
+ include RetryThis
12
+ # ...
13
+ def make_request(url)
14
+ connection = create_connection
15
+ retry_this(:times => 3, :error_types => [SocketError, Timeout::Error]) do
16
+ # this is retried at least 3 times if a SocketError or Timeout::Error occurs
17
+ connection.start do |http_connection|
18
+ http_connection.request(Net::HTTP::Get.new(url))
19
+ end
20
+ end
21
+ rescue SocketError, Timeout::Error => e
22
+ raise Gateway::ConnectionError, "Connection failed: #{e.message}"
23
+ rescue StandardError => error
24
+ raise Gateway::UnexpectedError, "Unexpected error: #{error.message}"
25
+ end
26
+ # ...
27
+ end
28
+
29
+ (see test/retry_this_test.rb for more)
30
+
31
+ License
32
+
33
+ Copyright 2009 Aman King
34
+
35
+ Licensed under the Apache License, Version 2.0 (the "License");
36
+ you may not use this file except in compliance with the License.
37
+ You may obtain a copy of the License at
38
+
39
+ http://www.apache.org/licenses/LICENSE-2.0
40
+
41
+ Unless required by applicable law or agreed to in writing, software
42
+ distributed under the License is distributed on an "AS IS" BASIS,
43
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44
+ See the License for the specific language governing permissions and
45
+ limitations under the License.
@@ -0,0 +1,17 @@
1
+ =begin
2
+ Copyright 2009 Aman King
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+
17
+ require 'retry_this'
@@ -0,0 +1,30 @@
1
+ =begin
2
+ Copyright 2009 Aman King
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+
17
+ module RetryThis
18
+ def retry_this(options = {})
19
+ number_of_retries = options[:times] || 1
20
+ error_types = options[:error_types] || [StandardError]
21
+ (1 + number_of_retries).times do |attempt_number|
22
+ begin
23
+ return yield if block_given?
24
+ rescue *error_types => e
25
+ raise e unless attempt_number < number_of_retries
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,70 @@
1
+ =begin
2
+ Copyright 2009 Aman King
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ =end
16
+
17
+ require 'test/unit'
18
+ require File.join(File.dirname(__FILE__), '../lib/retry_this')
19
+
20
+ class RetryThisTest < Test::Unit::TestCase
21
+ include RetryThis
22
+
23
+ def test_should_retry_block_for_given_number_of_times_for_given_error_types_and_raise_error_if_all_attempts_fail
24
+ attempts = 0
25
+ retry_this(:times => 3, :error_types => [TypeError, RuntimeError]) do
26
+ attempts += 1
27
+ if attempts % 2 == 0
28
+ raise 'some runtime error'
29
+ else
30
+ 'hi' + 1 # TypeError
31
+ end
32
+ end
33
+ rescue => e
34
+ assert e.kind_of?(TypeError) || e.kind_of?(RuntimeError)
35
+ assert_equal(1 + 3, attempts)
36
+ end
37
+
38
+ def test_should_retry_block_for_default_1_time_for_default_standard_error_and_raise_error_if_all_attempts_fail
39
+ attempts = 0
40
+ retry_this do
41
+ attempts += 1
42
+ raise StandardError, 'some error'
43
+ end
44
+ rescue => e
45
+ assert_equal('some error', e.message)
46
+ assert_equal(1 + 1, attempts)
47
+ end
48
+
49
+ def test_should_pass_on_an_error_that_is_not_among_given_errors
50
+ attempts = 0
51
+ retry_this(:error_types => [TypeError]) do
52
+ attempts += 1
53
+ raise StandardError, 'some error'
54
+ end
55
+ rescue => e
56
+ assert_equal('some error', e.message)
57
+ assert_equal(1, attempts)
58
+ end
59
+
60
+ def test_should_return_value_of_block_if_any_attempt_leads_to_no_error
61
+ attempts = 0
62
+ value = retry_this do
63
+ attempts += 1
64
+ raise 'some error' if attempts == 1
65
+ 'hi'
66
+ end
67
+ assert_equal('hi', value)
68
+ assert_equal(1 + 1, attempts)
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retry-this
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ version: "1.0"
9
+ platform: ruby
10
+ authors:
11
+ - Aman King
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2009-04-13 00:00:00 +05:30
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: RetryThis provides a method that takes a block which it will invoke a given number of times before giving up for specified errors
21
+ email: amanking@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - README
30
+ - lib/retry-this.rb
31
+ - lib/retry_this.rb
32
+ - test/retry_this_test.rb
33
+ has_rdoc: true
34
+ homepage: http://bitbucket.org/amanking/retry_this/
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.6
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: RetryThis provides a method that takes a block which it will invoke a given number of times before giving up for specified errors
63
+ test_files:
64
+ - test/retry_this_test.rb