goran 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +84 -0
  3. data/Rakefile +2 -0
  4. data/lib/goran.rb +47 -0
  5. data/lib/goran/version.rb +3 -0
  6. metadata +6 -1
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nitesh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ # Goran
2
+
3
+ Named after Goran Invanisevic, the tennis legend who won the Wimbledon after losing the final 3 times, Goran provides a simple syntax to run a block of code multiple times. E.g.
4
+
5
+ * run block 'x' number of times
6
+ * run until the block returns a non-nil value
7
+ * run until the block does not raise an exception
8
+ * run until the block returns a non-zero value, to a maximum of 3 times, and return nil if all runs return a 0
9
+
10
+ Goran is especially useful for running network calls which have unexpected outputs like 404, timeouts. It is an easy way to build in retry logic into these calls and handle cases where these calls do not succeed at all.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'goran'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install goran
25
+
26
+ ## Usage
27
+
28
+ require 'goran'
29
+
30
+ Run a block until it stops returning `0`
31
+
32
+ Goran.serve :retry_if => 0 do
33
+ #block
34
+ end
35
+
36
+ Run a block until it returns a value > 0; run it a maximum of 3 times
37
+
38
+ Goran.serve :max_tries => 3, :retry_if => { |x| x <= 0 } do
39
+ #block
40
+ end
41
+
42
+ Run a block until it stops returning `0`; run it a maximum of 3 times; wait for a second between calls
43
+
44
+ Goran.serve :max_tries => 3, :retry_if => 0, :interval => 1 do
45
+ #block
46
+ end
47
+
48
+ Run a block until it stops returning `0`; run it a maximum of 3 times; return `nil` if it doesn't return a non-zero value even after 3 tries
49
+
50
+ Goran.serve :max_tries => 3, :retry_if => 0, :fallback => nil do
51
+ #block
52
+ end
53
+
54
+ Run a block until it doesn't raise a `StandardError`
55
+
56
+ Goran.serve :rescue_from => StandardError do
57
+ #block
58
+ end
59
+
60
+ Run a block until it doesn't raise a `StandardError`; run it a maximum of 3 times; supress error all three times
61
+
62
+ Goran.serve :max_tries => 3, :rescue_from => StandardError do
63
+ #block
64
+ end
65
+
66
+ Run a block until it doesn't raise a `StandardError`; run it a maximum of 3 times; do not supress an error in the last run
67
+
68
+ Goran.serve :max_tries => 3, :rescue_from => StandardError, :rescue_last => false
69
+ #block
70
+ end
71
+
72
+ Run a block until it doesn't raise a `StandardError`; run it a maximum of 3 times; run a block when an error happens (say for logging the error)
73
+
74
+ Goran.serve :max_tries => 3, :rescue_from => StandardError, :on_rescue => lambda { |e| logger.error e }
75
+ #block
76
+ end
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ require "goran/version"
2
+
3
+ module Goran
4
+ def self.serve(options)
5
+ # set max tries to infinity if none is given
6
+ max_tries = options[:max_tries] || 1.0/0
7
+ # if no retry is given, assume that the caller is just retrying to
8
+ # handle exceptions. hence, make sure it always returns false.
9
+ retry_if = if options.has_key?(:retry_if) then options[:retry_if] else lambda { |x| false } end
10
+ # set fallback value only if a fallback is defined
11
+ fallback = options[:fallback] if options.has_key?(:fallback)
12
+ # set exceptions to rescue from. set to fake exception if none provided
13
+ rescue_from = options[:rescue_from] || Goran::DoubleFault
14
+ # should the last exception in the iteration be rescued from?
15
+ rescue_last = options[:rescue_last] != false
16
+ # what do do on rescue
17
+ on_rescue = options[:on_rescue]
18
+ # time between successive calls.
19
+ interval = options[:interval] || 0
20
+
21
+ result = nil
22
+ 1.upto(max_tries) do |i|
23
+ begin
24
+ result = yield
25
+ if retry_if.kind_of?(Proc)
26
+ break unless retry_if.call(result)
27
+ elsif retry_if != result
28
+ break
29
+ end
30
+ # the flow should not come here if the result was good
31
+ result = fallback if options.has_key?(:fallback)
32
+ rescue *rescue_from => e
33
+ if i == max_tries && !rescue_last
34
+ raise e
35
+ else
36
+ result = fallback if options.has_key?(:fallback)
37
+ on_rescue.call(e) if on_rescue.kind_of?(Proc)
38
+ end
39
+ ensure
40
+ sleep interval unless interval.zero?
41
+ end
42
+ end
43
+ result
44
+ end
45
+
46
+ class DoubleFault < StandardError; end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Goran
2
+ VERSION = '0.6'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goran
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: '0.6'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -26,6 +26,11 @@ executables: []
26
26
  extensions: []
27
27
  extra_rdoc_files: []
28
28
  files:
29
+ - README.md
30
+ - Rakefile
31
+ - LICENSE
32
+ - lib/goran/version.rb
33
+ - lib/goran.rb
29
34
  - spec/goran_spec.rb
30
35
  - spec/spec_helper.rb
31
36
  homepage: http://github.com/wallwisher/goran