em_retry 0.0.1
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.
- data/lib/em_retry.rb +32 -0
 - data/lib/em_retry/version.rb +3 -0
 - data/spec/em_retry_spec.rb +51 -0
 - metadata +91 -0
 
    
        data/lib/em_retry.rb
    ADDED
    
    | 
         @@ -0,0 +1,32 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'eventmachine'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            # Tries to evaluate the given block with the given timers
         
     | 
| 
      
 4 
     | 
    
         
            +
            # until it's not nil or false.
         
     | 
| 
      
 5 
     | 
    
         
            +
            # 
         
     | 
| 
      
 6 
     | 
    
         
            +
            #   EM.with_retries(1,2,3){false}
         
     | 
| 
      
 7 
     | 
    
         
            +
            # 
         
     | 
| 
      
 8 
     | 
    
         
            +
            # is executed immediatelly, after 1 second,
         
     | 
| 
      
 9 
     | 
    
         
            +
            # after another 2 secondes and after another 3 seconds.
         
     | 
| 
      
 10 
     | 
    
         
            +
            # 
         
     | 
| 
      
 11 
     | 
    
         
            +
            module EventMachine
         
     | 
| 
      
 12 
     | 
    
         
            +
              def self.with_retries(*timers, &block)
         
     | 
| 
      
 13 
     | 
    
         
            +
                block.call || !timers.empty? && EM.add_timer(timers.shift) do
         
     | 
| 
      
 14 
     | 
    
         
            +
                  with_retries *timers, &block
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
            end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            # A convenience wrapper for existing methods to indicate they should be
         
     | 
| 
      
 20 
     | 
    
         
            +
            # executed in a EM.with_retries block.
         
     | 
| 
      
 21 
     | 
    
         
            +
            # 
         
     | 
| 
      
 22 
     | 
    
         
            +
            module EMRetryMethod
         
     | 
| 
      
 23 
     | 
    
         
            +
              def retry_method(meth, *timers)
         
     | 
| 
      
 24 
     | 
    
         
            +
                orig_method = instance_method(meth)
         
     | 
| 
      
 25 
     | 
    
         
            +
                
         
     | 
| 
      
 26 
     | 
    
         
            +
                define_method meth do |*args, &block|
         
     | 
| 
      
 27 
     | 
    
         
            +
                  EM.with_retries *timers do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    orig_method.bind(self).call *args, &block
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
              end
         
     | 
| 
      
 32 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,51 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            $LOAD_PATH << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'em_retry'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            describe EventMachine do
         
     | 
| 
      
 6 
     | 
    
         
            +
              describe ".with_retries" do
         
     | 
| 
      
 7 
     | 
    
         
            +
                it "should retry the block" do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  EM.run do
         
     | 
| 
      
 9 
     | 
    
         
            +
                    EM.add_timer(0.8){EM.stop}
         
     | 
| 
      
 10 
     | 
    
         
            +
                    
         
     | 
| 
      
 11 
     | 
    
         
            +
                    Kernel.should_receive(:puts).with("trial 1")
         
     | 
| 
      
 12 
     | 
    
         
            +
                    Kernel.should_receive(:puts).with("trial 2")
         
     | 
| 
      
 13 
     | 
    
         
            +
                    Kernel.should_receive(:puts).with("trial 3")
         
     | 
| 
      
 14 
     | 
    
         
            +
                    Kernel.should_receive(:puts).with("trial 4")
         
     | 
| 
      
 15 
     | 
    
         
            +
                    
         
     | 
| 
      
 16 
     | 
    
         
            +
                    i = 0
         
     | 
| 
      
 17 
     | 
    
         
            +
                    EM.with_retries 0.01, 0.02, 0.03 do
         
     | 
| 
      
 18 
     | 
    
         
            +
                      Kernel.puts "trial #{i+=1}"
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
                it "should use the given timers" do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  EM.run do
         
     | 
| 
      
 24 
     | 
    
         
            +
                    EM.add_timer(0.1){EM.stop}
         
     | 
| 
      
 25 
     | 
    
         
            +
                    
         
     | 
| 
      
 26 
     | 
    
         
            +
                    EM.should_receive(:add_timer).with(0.01)
         
     | 
| 
      
 27 
     | 
    
         
            +
                    EM.with_retries 0.01 do
         
     | 
| 
      
 28 
     | 
    
         
            +
                      Kernel.puts "trial"
         
     | 
| 
      
 29 
     | 
    
         
            +
                    end
         
     | 
| 
      
 30 
     | 
    
         
            +
                  end
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
      
 32 
     | 
    
         
            +
              end
         
     | 
| 
      
 33 
     | 
    
         
            +
            end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            class A
         
     | 
| 
      
 36 
     | 
    
         
            +
              extend EMRetryMethod
         
     | 
| 
      
 37 
     | 
    
         
            +
              def foo; end
         
     | 
| 
      
 38 
     | 
    
         
            +
              retry_method :foo, 1,2,3
         
     | 
| 
      
 39 
     | 
    
         
            +
            end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            describe EMRetryMethod do
         
     | 
| 
      
 42 
     | 
    
         
            +
              describe "#retry_method" do
         
     | 
| 
      
 43 
     | 
    
         
            +
                before(:each) do
         
     | 
| 
      
 44 
     | 
    
         
            +
                  @a = A.new
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
                it "should description" do
         
     | 
| 
      
 47 
     | 
    
         
            +
                  EM.should_receive(:with_retries).with(1,2,3)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  @a.foo
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,91 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: em_retry
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 5 
     | 
    
         
            +
              segments: 
         
     | 
| 
      
 6 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 7 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 1
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 10 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 11 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 12 
     | 
    
         
            +
            - Niko Dittmann
         
     | 
| 
      
 13 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 14 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 15 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2010-11-21 00:00:00 +01:00
         
     | 
| 
      
 18 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 19 
     | 
    
         
            +
            dependencies: 
         
     | 
| 
      
 20 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 21 
     | 
    
         
            +
              name: eventmachine
         
     | 
| 
      
 22 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 23 
     | 
    
         
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 24 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 25 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 26 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 27 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 28 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 29 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 30 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 31 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 32 
     | 
    
         
            +
              version_requirements: *id001
         
     | 
| 
      
 33 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 34 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 35 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 36 
     | 
    
         
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 37 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 38 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 39 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 40 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 41 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 42 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 43 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 44 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 45 
     | 
    
         
            +
              version_requirements: *id002
         
     | 
| 
      
 46 
     | 
    
         
            +
            description: ""
         
     | 
| 
      
 47 
     | 
    
         
            +
            email: mail+git@niko-dittmann.com
         
     | 
| 
      
 48 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib/em_retry/version.rb
         
     | 
| 
      
 56 
     | 
    
         
            +
            - lib/em_retry.rb
         
     | 
| 
      
 57 
     | 
    
         
            +
            - spec/em_retry_spec.rb
         
     | 
| 
      
 58 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 59 
     | 
    
         
            +
            homepage: http://github.com/niko/em_retry
         
     | 
| 
      
 60 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 63 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 66 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 67 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 68 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 69 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 70 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 71 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 72 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 73 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 74 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 75 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 76 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 77 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 78 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 79 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 80 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 81 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 82 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 83 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
            rubyforge_project: "[none]"
         
     | 
| 
      
 86 
     | 
    
         
            +
            rubygems_version: 1.3.7
         
     | 
| 
      
 87 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 88 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 89 
     | 
    
         
            +
            summary: A retry-method for Eventmachine and a convenience wrapper.
         
     | 
| 
      
 90 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 91 
     | 
    
         
            +
            - spec/em_retry_spec.rb
         
     |