tretry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (>= 1.0.0)
29
+ jeweler (~> 1.8.4)
30
+ rdoc (~> 3.12)
31
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kasper Johansen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = tretry
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to tretry
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Kasper Johansen. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tretry"
18
+ gem.homepage = "http://github.com/kaspernj/tretry"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.}
21
+ gem.description = %Q{A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.}
22
+ gem.email = "k@spernj.org"
23
+ gem.authors = ["Kasper Johansen"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "tretry #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/tretry.rb ADDED
@@ -0,0 +1,93 @@
1
+ #A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
2
+ class Tretry
3
+ #Valid keys that can be given as argument for the method 'try'.
4
+ VALID_KEYS = [:tries, :timeout, :wait, :interrupt, :exit, :errors, :return_error]
5
+
6
+ #===Runs a block of code a given amount of times until it succeeds.
7
+ #===Examples
8
+ # res = Tretry.try(:tries => 3) do
9
+ # #something that often fails
10
+ # end
11
+ #
12
+ # puts "Tries: '#{res[:tries]}'."
13
+ # puts "Result: '#{res[:result}'."
14
+ def self.try(args = {}, &block)
15
+ #Validate given arguments and set various variables.
16
+ raise "No block was given." if !block
17
+ raise "Expected argument to be a hash." if !args.is_a?(Hash)
18
+
19
+ args.each do |key, val|
20
+ raise "Invalid key: '#{key}'." if !VALID_KEYS.include?(key)
21
+ end
22
+
23
+ args[:tries] = 3 if !args[:tries]
24
+ tries = []
25
+ error = nil
26
+ res = nil
27
+
28
+ args[:tries].to_i.downto(1) do |count|
29
+ error = nil
30
+ dobreak = false
31
+
32
+ begin
33
+ if args[:timeout]
34
+ #If a timeout-argument has been given, then run the code through the timeout.
35
+ begin
36
+ require "timeout"
37
+ Timeout.timeout(args[:timeout]) do
38
+ res = block.call
39
+ dobreak = true
40
+ break
41
+ end
42
+ rescue Timeout::Error => e
43
+ doraise = e if count <= 1
44
+ error = e
45
+ sleep(args[:wait]) if args[:wait] and !doraise
46
+ end
47
+ else
48
+ #Else call block normally.
49
+ res = block.call
50
+ dobreak = true
51
+ break
52
+ end
53
+ rescue Exception => e
54
+ if e.class == Interrupt
55
+ raise e if !args.key?(:interrupt) or args[:interrupt]
56
+ elsif e.class == SystemExit
57
+ raise e if !args.key?(:exit) or args[:exit]
58
+ elsif count <= 1 or (args.key?(:errors) and args[:errors].index(e.class) == nil)
59
+ doraise = e
60
+ elsif args.key?(:errors) and args[:errors].index(e.class) != nil
61
+ #given error was in the :errors-array - do nothing. Maybe later it should be logged and returned in a stats-hash or something? - knj
62
+ end
63
+
64
+ error = e
65
+
66
+ #Sleep for a given amount of time if the 'wait'-argument is given.
67
+ sleep(args[:wait]) if args[:wait] and !doraise
68
+ end
69
+
70
+ if doraise
71
+ if args[:return_error]
72
+ tries << {:error => error}
73
+ return {
74
+ :tries => tries,
75
+ :error => true
76
+ }
77
+ else
78
+ raise e
79
+ end
80
+ elsif error
81
+ tries << {:error => error}
82
+ end
83
+
84
+ break if dobreak
85
+ end
86
+
87
+ return {
88
+ :tries => tries,
89
+ :result => res,
90
+ :error => false
91
+ }
92
+ end
93
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'tretry'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Tretry" do
4
+ it "should be able to run blocks" do
5
+ try = 0
6
+ res = Tretry.try(:tries => 5) do
7
+ try += 1
8
+ raise "Test #{try}" if try < 5
9
+ "kasper"
10
+ end
11
+
12
+ raise "Expected error to be false but it wasnt: '#{res[:error]}'." if res[:error] != false
13
+ raise "Expected result to be 'kasper' but it wasnt: '#{res[:result]}'." if res[:result] != "kasper"
14
+ end
15
+
16
+ it "should be able to do waits between tries" do
17
+ try = 0
18
+ time_start = Time.now.to_f
19
+ res = Tretry.try(:tries => 5, :wait => 0.1) do
20
+ try += 1
21
+ raise "Test #{try}" if try < 5
22
+ "kasper"
23
+ end
24
+
25
+ time_end = Time.now.to_f
26
+ time_elap = time_end - time_start
27
+
28
+ raise "Expected time to be more than 0.4 sec but it wasnt: '#{time_elap}'." if time_elap < 0.4
29
+ end
30
+
31
+ it "should be able to do timeouts with tries" do
32
+ try = 0
33
+ res = Tretry.try(:tries => 5, :timeout => 0.1) do
34
+ try += 1
35
+ sleep 0.5 if try < 5
36
+ "kasper"
37
+ end
38
+
39
+ raise "Expected error to be false but it wasnt: '#{res[:error]}'." if res[:error] != false
40
+ raise "Expected result to be 'kasper' but it wasnt: '#{res[:result]}'." if res[:result] != "kasper"
41
+ raise "Expected number of errors to be 4 but it wasnt: '#{res[:tries].length}'." if res[:tries].length != 4
42
+
43
+ res[:tries].each do |err|
44
+ raise "Expected error to be 'Timeout::Error' but it wasnt: '#{err[:error].class.name}'." if !err[:error].is_a?(Timeout::Error)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tretry
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Kasper Johansen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-08-30 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.8.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "3.12"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.8.4
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
61
+ email: k@spernj.org
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/tretry.rb
79
+ - spec/spec_helper.rb
80
+ - spec/tretry_spec.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/kaspernj/tretry
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3966091872588483067
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.6.2
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: A library for doing retries in Ruby with timeouts, analysis of errors, waits between tries and more.
112
+ test_files: []
113
+