tlopo-retry 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +30 -0
- data/.travis.yml +10 -3
- data/Gemfile +11 -0
- data/README.md +78 -0
- data/Rakefile +11 -5
- data/lib/tlopo/retry.rb +63 -52
- data/lib/tlopo/retry/version.rb +1 -1
- data/tlopo-retry.gemspec +18 -17
- metadata +21 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77ef23b75122a02c372f335adf6277b8b17bb7ed
|
4
|
+
data.tar.gz: a7c1846fc1d8981a2c9e224d033936337644e67a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3789283628c94ebb4697d5e38e85f68486886cde318072298c17e69a054d87209b453679f7ca368c4dd788472e6fb60d85c0f215a83557579188161446988af
|
7
|
+
data.tar.gz: 85ed915f99ad0a494b9b1f7901c29f02b39f6143ae4008f0e62963601d30ae2abe0fb3819fb1f5590ce08fd3b4c830ecc4db5c5ce4cf8f89ff0a2d1c5ad8c78d
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
AllCops:
|
3
|
+
DisplayCopNames: true
|
4
|
+
TargetRubyVersion: 2.2 # Oldest version kubeclient supports
|
5
|
+
MethodLength:
|
6
|
+
Enabled: false
|
7
|
+
ClassLength:
|
8
|
+
Enabled: false
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Enabled: false
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 100
|
13
|
+
Metrics/ParameterLists:
|
14
|
+
Max: 5
|
15
|
+
CountKeywordArgs: false
|
16
|
+
Metrics/CyclomaticComplexity:
|
17
|
+
Max: 8
|
18
|
+
Exclude:
|
19
|
+
- lib/tlopo/retry.rb
|
20
|
+
Metrics/ModuleLength:
|
21
|
+
Enabled: false
|
22
|
+
Style/MethodCallWithArgsParentheses:
|
23
|
+
Enabled: true
|
24
|
+
IgnoredMethods: [require, raise, include, attr_reader, refute, assert]
|
25
|
+
Exclude: [Gemfile, Rakefile, kubeclient.gemspec]
|
26
|
+
Security/MarshalLoad:
|
27
|
+
Exclude:
|
28
|
+
- test/**/*
|
29
|
+
- lib/tlopo/retry.rb
|
30
|
+
|
data/.travis.yml
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
sudo: false
|
2
1
|
language: ruby
|
3
2
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
3
|
+
- "2.2"
|
4
|
+
- "2.3.0"
|
5
|
+
- "2.4.0"
|
6
|
+
sudo: false
|
7
|
+
cache: bundler
|
8
|
+
script: bundle exec rake $TASK
|
9
|
+
env:
|
10
|
+
- TASK=test
|
11
|
+
- TASK=rubocop
|
12
|
+
- TASK=test_with_coveralls
|
data/Gemfile
CHANGED
@@ -2,3 +2,14 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in tlopo-retry.gemspec
|
4
4
|
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'coveralls', '~> 0.8.17'
|
8
|
+
gem 'simplecov', '~> 0.12.0'
|
9
|
+
end
|
10
|
+
|
11
|
+
if RUBY_VERSION > '2.1.0'
|
12
|
+
group :perf do
|
13
|
+
gem 'memory_profiler', '~> 0.9.8'
|
14
|
+
end
|
15
|
+
end
|
data/README.md
CHANGED
@@ -1,2 +1,80 @@
|
|
1
1
|
# tlopo-retry
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/tlopo-retry.svg)](http://badge.fury.io/rb/tlopo-retry)
|
3
|
+
[![Build Status](https://travis-ci.org/tlopo-ruby/tlopo-retry.svg?branch=master)](https://travis-ci.org/tlopo-ruby/tlopo-retry)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/tlopo-ruby/tlopo-retry/badges/gpa.svg)](https://codeclimate.com/github/tlopo-ruby/tlopo-retry)
|
5
|
+
[![Dependency Status](https://gemnasium.com/tlopo-ruby/tlopo-retry.svg)](https://gemnasium.com/tlopo-ruby/tlopo-retry)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/github/tlopo-ruby/tlopo-retry/badge.svg?branch=master)](https://coveralls.io/github/tlopo-ruby/tlopo-retry?branch=master)
|
7
|
+
|
2
8
|
A reusable retry mechanism which supports timeout, cleanup and fork
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'tlopo-retry'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```Bash
|
21
|
+
bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
```Bash
|
27
|
+
gem install tlopo-retry
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Simple retry usage
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# That will retry 3 times with no timeout and 1 second interval
|
36
|
+
Tlopo::Retry.retry do
|
37
|
+
TCPSocket.new('www.google.co.uk','8080').close
|
38
|
+
end
|
39
|
+
```
|
40
|
+
Full options
|
41
|
+
```ruby
|
42
|
+
require 'logger'
|
43
|
+
require 'socket'
|
44
|
+
|
45
|
+
# Enable logging
|
46
|
+
ENV['TLOPO_LOG_LEVEL'] = 'debug'
|
47
|
+
|
48
|
+
require 'tlopo/retry'
|
49
|
+
|
50
|
+
Tlopo::Retry.retry({
|
51
|
+
desc: 'check if port 8080 is open on www.google.co.uk',
|
52
|
+
tries: 2,
|
53
|
+
interval: 5,
|
54
|
+
timeout: 1,
|
55
|
+
cleanup: proc { p 'Run your cleanup code here'}
|
56
|
+
}) do
|
57
|
+
TCPSocket.new('www.google.co.uk','8080').close
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it ( https://github.com/[my-github-username]/kubeclient/fork )
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Test your changes with `rake test rubocop`, add new tests if needed.
|
67
|
+
4. If you added a new functionality, add it to README
|
68
|
+
5. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
6. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
7. Create a new Pull Request
|
71
|
+
|
72
|
+
## Tests
|
73
|
+
|
74
|
+
This library is tested with Minitest.
|
75
|
+
Please run all tests before submitting a Pull Request, and add new tests for new functionality.
|
76
|
+
|
77
|
+
Running tests:
|
78
|
+
```ruby
|
79
|
+
rake test
|
80
|
+
```
|
data/Rakefile
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
require 'coveralls/rake/task'
|
5
|
+
|
6
|
+
task default: %i[test rubocop]
|
7
|
+
task test_with_coveralls: [:default, 'coveralls:push']
|
3
8
|
|
4
9
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.libs <<
|
10
|
+
t.libs << 'test'
|
11
|
+
t.libs << 'lib'
|
7
12
|
t.test_files = FileList['test/**/*_test.rb']
|
8
13
|
end
|
9
14
|
|
10
|
-
|
15
|
+
RuboCop::RakeTask.new
|
16
|
+
Coveralls::RakeTask.new
|
data/lib/tlopo/retry.rb
CHANGED
@@ -1,70 +1,81 @@
|
|
1
|
-
require
|
1
|
+
require 'tlopo/retry/version'
|
2
2
|
require 'logger'
|
3
3
|
require 'timeout'
|
4
4
|
|
5
|
+
# Simple module so we have a namespace
|
5
6
|
module Tlopo
|
6
|
-
LOGGER ||= proc do
|
7
|
+
LOGGER ||= proc do
|
7
8
|
logger = Logger.new(ENV['TLOPO_LOG_LEVEL'] ? STDERR : '/dev/null')
|
8
|
-
logger.level = ENV['TLOPO_LOG_LEVEL'] ?
|
9
|
+
logger.level = ENV['TLOPO_LOG_LEVEL'] ? ENV['TLOPO_LOG_LEVEL'].upcase.to_s : Logger::ERROR
|
9
10
|
logger
|
10
11
|
end.call
|
11
|
-
|
12
|
-
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return local(opts,&block) unless is_fork
|
20
|
-
return child(opts,&block) if is_fork
|
21
|
-
end
|
13
|
+
# A reusable retry mechanism which supports timeout, cleanup and fork
|
14
|
+
module Retry
|
15
|
+
LOGGER = Tlopo::LOGGER
|
16
|
+
LOGGER.debug("#{self} loaded")
|
17
|
+
VALID_OPTS = %i[tries timeout interval fork cleanup].freeze
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
pid = fork do
|
28
|
-
read.close
|
29
|
-
begin
|
30
|
-
result = local(opts,&block)
|
31
|
-
Marshal.dump({result: result, error: nil}, write)
|
32
|
-
rescue => e
|
33
|
-
Marshal.dump({result:nil, error: e},write)
|
19
|
+
def self.validate_opts(opts)
|
20
|
+
opts.each_key do |k|
|
21
|
+
msg = "Option #{k} is invalid. Valid options: #{VALID_OPTS}"
|
22
|
+
raise msg unless VALID_OPTS.include?(k)
|
34
23
|
end
|
35
24
|
end
|
36
|
-
|
37
|
-
write.close
|
38
|
-
result = Marshal.load(read.read)
|
39
|
-
Process.wait(pid)
|
40
|
-
raise result[:error] if result[:error]
|
41
|
-
result[:result]
|
42
|
-
end
|
43
25
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
26
|
+
def self.retry(opts = {}, &block)
|
27
|
+
validate_opts(opts)
|
28
|
+
is_fork = opts[:fork]
|
29
|
+
return local(opts, &block) unless is_fork
|
30
|
+
return child(opts, &block) if is_fork
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.child(opts = {}, &block)
|
34
|
+
read, write = IO.pipe
|
35
|
+
|
36
|
+
pid = fork do
|
37
|
+
read.close
|
38
|
+
begin
|
39
|
+
result = local(opts, &block)
|
40
|
+
Marshal.dump({ result: result, error: nil }, write)
|
41
|
+
rescue StandardError => e
|
42
|
+
Marshal.dump({ result: nil, error: e }, write)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
write.close
|
47
|
+
result = Marshal.load(read.read)
|
48
|
+
Process.wait(pid)
|
49
|
+
raise result[:error] if result[:error]
|
50
|
+
result[:result]
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.local(opts = {})
|
54
|
+
tries = get_opt_value(opts, :tries, 3)
|
55
|
+
timeout = get_opt_value(opts, :timeout, 0)
|
56
|
+
interval = get_opt_value(opts, :interval, 1)
|
57
|
+
desc = opts[:desc]
|
58
|
+
cleanup = opts[:cleanup]
|
59
|
+
LOGGER.debug("opts: #{opts}")
|
60
|
+
LOGGER.debug("tries: #{tries}")
|
61
|
+
count = 0
|
62
|
+
begin
|
63
|
+
count += 1
|
64
|
+
Timeout.timeout(timeout) { yield }
|
65
|
+
rescue StandardError => e
|
66
|
+
raise e if count > tries
|
58
67
|
msg = "#{self} Retrying to #{desc} #{count} out of #{tries}"
|
59
|
-
LOGGER.info
|
60
|
-
LOGGER.debug
|
68
|
+
LOGGER.info(msg) if desc
|
69
|
+
LOGGER.debug("#{self} Calling cleanup") if cleanup
|
61
70
|
cleanup.call if cleanup
|
62
|
-
sleep
|
63
|
-
retry
|
64
|
-
else
|
65
|
-
raise e
|
71
|
+
sleep(interval)
|
72
|
+
retry
|
66
73
|
end
|
67
74
|
end
|
75
|
+
|
76
|
+
def self.get_opt_value(opts, key, default)
|
77
|
+
return opts[key] unless opts[key].nil?
|
78
|
+
default
|
79
|
+
end
|
68
80
|
end
|
69
81
|
end
|
70
|
-
|
data/lib/tlopo/retry/version.rb
CHANGED
data/tlopo-retry.gemspec
CHANGED
@@ -1,38 +1,39 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'tlopo/retry/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'tlopo-retry'
|
8
8
|
spec.version = Tlopo::Retry::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['tlopo']
|
10
|
+
spec.email = ['tiagolopo@yahoo.com.br']
|
11
11
|
|
12
|
-
spec.summary =
|
12
|
+
spec.summary = '
|
13
13
|
A reusable retry mechanism which supports timeout, cleanup, and fork
|
14
|
-
|
14
|
+
'
|
15
15
|
spec.description = spec.summary
|
16
|
-
spec.homepage =
|
17
|
-
spec.license =
|
16
|
+
spec.homepage = 'https://github.com/tlopo-ruby/tlopo-retry'
|
17
|
+
spec.license = 'MIT'
|
18
18
|
|
19
19
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
20
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
21
|
if spec.respond_to?(:metadata)
|
22
|
-
#spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
23
23
|
else
|
24
|
-
raise
|
25
|
-
|
24
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
25
|
+
'public gem pushes.'
|
26
26
|
end
|
27
27
|
|
28
|
-
spec.files
|
28
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
29
29
|
f.match(%r{^(test|spec|features)/})
|
30
30
|
end
|
31
|
-
spec.bindir =
|
31
|
+
spec.bindir = 'bin'
|
32
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
-
spec.require_paths = [
|
33
|
+
spec.require_paths = ['lib']
|
34
34
|
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
35
|
+
spec.add_development_dependency('bundler', '~> 1.13')
|
36
|
+
spec.add_development_dependency('minitest', '~> 5.0')
|
37
|
+
spec.add_development_dependency('rake', '~> 10.0')
|
38
|
+
spec.add_development_dependency('rubocop', '~> 0.52.1')
|
38
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tlopo-retry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tlopo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,35 +53,34 @@ dependencies:
|
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '10.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: rubocop
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
61
|
+
version: 0.52.1
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
68
|
+
version: 0.52.1
|
55
69
|
description: A reusable retry mechanism which supports timeout, cleanup, and fork
|
56
70
|
email:
|
57
|
-
-
|
71
|
+
- tiagolopo@yahoo.com.br
|
58
72
|
executables: []
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
76
|
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
63
78
|
- ".travis.yml"
|
64
79
|
- Gemfile
|
65
80
|
- LICENSE
|
66
81
|
- LICENSE.txt
|
67
82
|
- README.md
|
68
83
|
- Rakefile
|
69
|
-
- bin/console
|
70
|
-
- bin/setup
|
71
84
|
- lib/tlopo/retry.rb
|
72
85
|
- lib/tlopo/retry/version.rb
|
73
86
|
- tlopo-retry.gemspec
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "tlopo/retry"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|