attempt 0.6.1 → 0.6.2
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.md +3 -0
- data/Gemfile +2 -8
- data/README.md +24 -32
- data/Rakefile +1 -1
- data/attempt.gemspec +4 -3
- data/lib/attempt.rb +40 -33
- data/spec/attempt_spec.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +19 -5
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c954f55aef2408950c9aeafde149fa2562576e62af9f6a0215f6dc90bec184eb
|
4
|
+
data.tar.gz: 1951edd8ffc473008f37850504fda990593721378c26c84c3375f9c58df868ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 687f808427b5c2a686c89892af635b5528886207c6bf0a38036c0b4cceb92592a2a89579301083eb7a3ca01e297878e02ccf57148504e4fb10f1188ebf1841c4
|
7
|
+
data.tar.gz: '028d54de4b4602f76d2ffae474c1f1bdc2835604aac33d24cc503cdd250661839bb6eefcf15a7e8fa0d5e25af401cf5a91ff144213c2599551f3d28d38788972'
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGES.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,42 +1,40 @@
|
|
1
1
|
## Description
|
2
|
-
|
3
2
|
A thin wrapper for begin + rescue + sleep + retry.
|
4
3
|
|
5
4
|
## Installation
|
5
|
+
`gem install attempt`
|
6
6
|
|
7
|
-
|
7
|
+
## Adding the trusted cert
|
8
|
+
`gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/attempt/main/certs/djberg96_pub.pem)`
|
8
9
|
|
9
10
|
## Synopsis
|
11
|
+
```ruby
|
12
|
+
require 'attempt'
|
13
|
+
|
14
|
+
# Attempt to ftp to some host, trying 3 times with 30 seconds between
|
15
|
+
# attempts before finally raising an error.
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# Attempt to ftp to some host, trying 3 times with 30 seconds between
|
15
|
-
# attempts before finally raising an error.
|
17
|
+
attempt(tries: 3, interval: 30){
|
18
|
+
Net::FTP.open(host, user, passwd){ ... }
|
19
|
+
}
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
code.attempt{
|
28
|
-
Net::FTP.open(host, user, passwd){ ... }
|
29
|
-
}
|
21
|
+
# Or, do things the long way...
|
22
|
+
code = Attempt.new do |a|
|
23
|
+
a.tries = 3
|
24
|
+
a.interval = 30
|
25
|
+
end
|
26
|
+
|
27
|
+
code.attempt{
|
28
|
+
Net::FTP.open(host, user, passwd){ ... }
|
29
|
+
}
|
30
30
|
```
|
31
31
|
|
32
32
|
## Known Bugs
|
33
|
-
|
34
33
|
None that I'm aware of. If you find any bugs, please log them on the project page at:
|
35
34
|
|
36
35
|
https://github.com/djberg96/attempt
|
37
36
|
|
38
37
|
## Caveats
|
39
|
-
|
40
38
|
Use with caution. Specifically, make sure you aren't inadvertantly
|
41
39
|
wrapping code that already performs sleep + retry. Otherwise, you'll
|
42
40
|
end up with a series of nested retry's that could take much longer to
|
@@ -49,36 +47,30 @@ As of version 0.3.0, this library requires structured_warnings 0.3.0 or
|
|
49
47
|
later. This is necessary because of changes in Ruby 2.4.
|
50
48
|
|
51
49
|
Update: I've switched from the timeout library in the standard library to
|
52
|
-
the safe_timeout library which should improve things.
|
53
|
-
structured_warnings library requirement is now 0.4.0 or later
|
54
|
-
work with Ruby 2.7.
|
50
|
+
the safe_timeout library on non-Windows platforms which should improve things.
|
51
|
+
In addition, the structured_warnings library requirement is now 0.4.0 or later
|
52
|
+
in order to work with Ruby 2.7.
|
55
53
|
|
56
54
|
## Future Plans
|
57
|
-
|
58
55
|
Add the ability to set an absolute maximum number of seconds to prevent
|
59
56
|
nested sleep/retry from delaying attempts longer than expected.
|
60
57
|
|
61
58
|
Replace the timeout library with a self selecting pipe if possible.
|
62
59
|
|
63
60
|
## Acknowledgements
|
64
|
-
|
65
61
|
This library is partially based on Mark Fowler's 'Attempt' Perl module.
|
66
62
|
|
67
63
|
## Warranty
|
68
|
-
|
69
64
|
This package is provided "as is" and without any express or
|
70
65
|
implied warranties, including, without limitation, the implied
|
71
66
|
warranties of merchantability and fitness for a particular purpose.
|
72
67
|
|
73
68
|
## License
|
74
|
-
|
75
69
|
Apache-2.0
|
76
70
|
|
77
71
|
## Copyright
|
78
|
-
|
79
|
-
(C) 2006-2020, Daniel J. Berger
|
72
|
+
(C) 2006-2022, Daniel J. Berger
|
80
73
|
All Rights Reserved
|
81
74
|
|
82
75
|
## Author
|
83
|
-
|
84
76
|
Daniel J. Berger
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ namespace :gem do
|
|
8
8
|
desc 'Build the attempt gem'
|
9
9
|
task :create => [:clean] do
|
10
10
|
require 'rubygems/package'
|
11
|
-
spec =
|
11
|
+
spec = Gem::Specification.load('attempt.gemspec')
|
12
12
|
spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
|
13
13
|
Gem::Package.build(spec)
|
14
14
|
end
|
data/attempt.gemspec
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = 'attempt'
|
5
|
-
spec.version = '0.6.
|
5
|
+
spec.version = '0.6.2'
|
6
6
|
spec.author = 'Daniel J. Berger'
|
7
7
|
spec.license = 'Apache-2.0'
|
8
8
|
spec.email = 'djberg96@gmail.com'
|
@@ -13,12 +13,13 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.cert_chain = Dir['certs/*']
|
14
14
|
|
15
15
|
spec.metadata = {
|
16
|
-
'changelog_uri' => 'https://github.com/djberg96/attempt/blob/
|
17
|
-
'source_code_uri' => 'https://github.com/djberg96/attempt/blob/
|
16
|
+
'changelog_uri' => 'https://github.com/djberg96/attempt/blob/main/CHANGES.md',
|
17
|
+
'source_code_uri' => 'https://github.com/djberg96/attempt/blob/main/lib/attempt.rb',
|
18
18
|
'bug_tracker_uri' => 'https://github.com/djberg96/attempt/issues',
|
19
19
|
'wiki_uri' => 'https://github.com/djberg96/attempt/wiki'
|
20
20
|
}
|
21
21
|
|
22
|
+
spec.add_development_dependency('rake')
|
22
23
|
spec.add_dependency('structured_warnings', '~> 0.4.0')
|
23
24
|
spec.add_dependency('safe_timeout', '~> 0.0.5')
|
24
25
|
spec.add_dependency('rspec', '~> 3.9')
|
data/lib/attempt.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
if File::ALT_SEPARATOR
|
4
|
+
require 'timeout'
|
5
|
+
else
|
6
|
+
require 'safe_timeout'
|
7
|
+
end
|
8
|
+
|
2
9
|
require 'structured_warnings'
|
3
10
|
|
4
11
|
# The Attempt class encapsulates methods related to multiple attempts at
|
5
12
|
# running the same method before actually failing.
|
6
13
|
class Attempt
|
7
|
-
|
8
14
|
# The version of the attempt library.
|
9
|
-
VERSION = '0.6.
|
15
|
+
VERSION = '0.6.2'
|
10
16
|
|
11
17
|
# Warning raised if an attempt fails before the maximum number of tries
|
12
18
|
# has been reached.
|
@@ -51,7 +57,7 @@ class Attempt
|
|
51
57
|
# * warnings - Boolean value that indicates whether or not errors are treated as warnings
|
52
58
|
# until the maximum number of attempts has been made. The default is true.
|
53
59
|
# * timeout - Boolean value to indicate whether or not to automatically wrap your
|
54
|
-
# proc in a SafeTimeout block. The default is false.
|
60
|
+
# proc in a Timeout/SafeTimeout block. The default is false.
|
55
61
|
#
|
56
62
|
# Example:
|
57
63
|
#
|
@@ -74,18 +80,18 @@ class Attempt
|
|
74
80
|
# You will not typically use this method directly, but the Kernel#attempt
|
75
81
|
# method instead.
|
76
82
|
#
|
77
|
-
def attempt
|
83
|
+
def attempt(&block)
|
78
84
|
count = 1
|
79
85
|
begin
|
80
86
|
if @timeout
|
81
|
-
|
87
|
+
File::ALT_SEPARATOR ? Timeout.timeout(@timeout, &block) : SafeTimeout.timeout(@timeout, &block)
|
82
88
|
else
|
83
89
|
yield
|
84
90
|
end
|
85
|
-
rescue @level =>
|
91
|
+
rescue @level => err
|
86
92
|
@tries -= 1
|
87
93
|
if @tries > 0
|
88
|
-
msg = "Error on attempt # #{count}: #{
|
94
|
+
msg = "Error on attempt # #{count}: #{err}; retrying"
|
89
95
|
count += 1
|
90
96
|
warn Warning, msg if @warnings
|
91
97
|
|
@@ -102,30 +108,31 @@ class Attempt
|
|
102
108
|
end
|
103
109
|
end
|
104
110
|
|
111
|
+
# Extend the Kernel module with a simple interface for the Attempt class.
|
105
112
|
module Kernel
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
113
|
+
# :call-seq:
|
114
|
+
# attempt(tries: 3, interval: 60, timeout: 10){ # some op }
|
115
|
+
#
|
116
|
+
# Attempt to perform the operation in the provided block up to +tries+
|
117
|
+
# times, sleeping +interval+ between each try. By default the number
|
118
|
+
# of tries defaults to 3, the interval defaults to 60 seconds, and there
|
119
|
+
# is no timeout specified.
|
120
|
+
#
|
121
|
+
# If +timeout+ is provided then the operation is wrapped in a Timeout
|
122
|
+
# block as well. This is handy for those rare occasions when an IO
|
123
|
+
# connection could hang indefinitely, for example.
|
124
|
+
#
|
125
|
+
# If the operation still fails the (last) error is then re-raised.
|
126
|
+
#
|
127
|
+
# This is really just a convenient wrapper for Attempt.new + Attempt#attempt.
|
128
|
+
#
|
129
|
+
# Example:
|
130
|
+
#
|
131
|
+
# # Make 3 attempts to connect to the database, 60 seconds apart.
|
132
|
+
# attempt{ DBI.connect(dsn, user, passwd) }
|
133
|
+
#
|
134
|
+
def attempt(**kwargs, &block)
|
135
|
+
object = Attempt.new(**kwargs)
|
136
|
+
object.attempt(&block)
|
137
|
+
end
|
131
138
|
end
|
data/spec/attempt_spec.rb
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attempt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel J. Berger
|
@@ -35,8 +35,22 @@ cert_chain:
|
|
35
35
|
ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
|
36
36
|
WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date:
|
38
|
+
date: 2022-06-02 00:00:00.000000000 Z
|
39
39
|
dependencies:
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rake
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
40
54
|
- !ruby/object:Gem::Dependency
|
41
55
|
name: structured_warnings
|
42
56
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,8 +118,8 @@ homepage: https://github.com/djberg96/attempt
|
|
104
118
|
licenses:
|
105
119
|
- Apache-2.0
|
106
120
|
metadata:
|
107
|
-
changelog_uri: https://github.com/djberg96/attempt/blob/
|
108
|
-
source_code_uri: https://github.com/djberg96/attempt/blob/
|
121
|
+
changelog_uri: https://github.com/djberg96/attempt/blob/main/CHANGES.md
|
122
|
+
source_code_uri: https://github.com/djberg96/attempt/blob/main/lib/attempt.rb
|
109
123
|
bug_tracker_uri: https://github.com/djberg96/attempt/issues
|
110
124
|
wiki_uri: https://github.com/djberg96/attempt/wiki
|
111
125
|
post_install_message:
|
@@ -123,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
137
|
- !ruby/object:Gem::Version
|
124
138
|
version: '0'
|
125
139
|
requirements: []
|
126
|
-
rubygems_version: 3.
|
140
|
+
rubygems_version: 3.3.15
|
127
141
|
signing_key:
|
128
142
|
specification_version: 4
|
129
143
|
summary: A thin wrapper for begin + rescue + sleep + retry
|
metadata.gz.sig
CHANGED
Binary file
|