Created
October 7, 2011 06:42
Revisions
-
raul revised this gist
Oct 7, 2011 . 1 changed file with 13 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ # # retry_upto(5) do ... end # # - retry up to 5 times, waiting 2 seconds between retries # # retry_upto(5, :wait => 2) do ... end # @@ -16,22 +16,33 @@ # # retry_upto(5, :wait => 1, :patience => 0.5) do ... end # # - retry up to 5 times, waiting 1 second initially and retrying with a randomly increasing patience # # retry_upto(5, :wait => 1, :patience => lambda{ |x| x + rand(3) } ) do ... end # # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError # # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end # # - retry up to 5 times, waiting 2 seconds between retries, retrying only after a ZeroDivisionError # # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end # def retry_upto(max_retries = 1, options = {}) yield rescue (options[:rescue_only] || Exception) raise if (max_retries -= 1) == 0 if options[:patience].respond_to?('*') options[:wait] = options[:wait] * options[:patience] elsif options[:patience].respond_to?(:call) options[:wait] = options[:patience].call(options[:wait]) end sleep(options[:wait] || 0) retry end # Extends enumerator to allow usage like: # # 5.times.retry do -
raul revised this gist
Oct 7, 2011 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,8 +28,7 @@ def retry_upto(max_retries = 1, options = {}) yield rescue (options[:rescue_only] || Exception) raise if (max_retries -= 1) == 0 sleep((options[:wait] || 0) * (options[:patience] || 1)) retry end -
raul revised this gist
Oct 7, 2011 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,14 @@ # # retry_upto(5, :wait => 2) do ... end # # - retry up to 5 times, waiting 1 second initially and having increasing patience (2, 4, 8, ...) # # retry_upto(5, :wait => 1, :patience => 2) do ... end # # - retry up to 5 times, waiting 1 second initially and having decreasing patience (0.5, 0.25, 0.125, ...) # # retry_upto(5, :wait => 1, :patience => 0.5) do ... end # # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError # # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end @@ -20,6 +28,7 @@ def retry_upto(max_retries = 1, options = {}) yield rescue (options[:rescue_only] || Exception) raise if (max_retries -= 1) == 0 options[:wait] *= options[:patience] if options[:wait] && options[:patience] sleep(options[:wait] || 0) retry end -
raul revised this gist
Oct 7, 2011 . 1 changed file with 17 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,11 +17,22 @@ # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end def retry_upto(max_retries = 1, options = {}) yield rescue (options[:rescue_only] || Exception) raise if (max_retries -= 1) == 0 sleep(options[:wait] || 0) retry end # Extends enumerator to allow usage like: # # 5.times.retry do # ... # end # class Enumerator def retry(options = {}, &blk) retry_upto(self.count, options, &blk) end end -
raul revised this gist
Oct 7, 2011 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Ruby `retry` with steroids: # # - retry up to 5 times without waiting between them and retrying after any exception # @@ -17,12 +17,10 @@ # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end def retry_upto(max_retries = 1, options = {}) begin return yield if ((max_retries -= 1) > 0) rescue (options[:rescue_only] || Exception) sleep(options[:wait] || 0) retry end yield -
raul revised this gist
Oct 7, 2011 . 1 changed file with 23 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,29 @@ # Ruby's `retry` with steroids: # # - retry up to 5 times without waiting between them and retrying after any exception # # retry_upto(5) do ... end # # - retry up to 5 times, waiting 2 seconds between retries and retrying after any exception # # retry_upto(5, :wait => 2) do ... end # # - retry up to 5 times without waiting between retries, retrying only after a ZeroDivisionError # # retry_upto(5, :rescue_only => ZeroDivisionError) do ... end # # - retry up to 5 times, waiting 2 seconds between retries, retrying only after a ZeroDivisionError # # retry_upto(5, :wait => 2, :rescue_only => ZeroDivisionError) do ... end def retry_upto(max_retries = 1, options = {}) wait_secs = options[:wait] || 0 exception_class = options[:rescue_only] || Exception begin return yield if ((max_retries -= 1) > 0) rescue exception_class => e sleep(wait_secs) retry end yield end -
raul created this gist
Oct 7, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ def retry_upto(max_retries = 1, exception_class = Exception) begin return yield if ((max_retries -= 1) > 0) rescue exception_class => e retry end yield end