A code snippet: Controlled retry

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default A code snippet: Controlled retry

    I sometimes find myself retrying operations when in a networked
    situation (e.g., maybe server isn't up, just try again).

    I just wrote this little bit of code.

    Tell me what you think... I'm sure it can be improved.

    Cheers,
    Hal


    def try(quiet=true,times=5,secs=1,&block)
    count = 0
    catch :finished do
    begin
    block.call
    rescue => err
    puts "Error was: #{err}" if not quiet
    count += 1
    throw :finished if count > times
    sleep secs
    retry
    end
    end
    end




    Hal Fulton Guest

  2. Similar Questions and Discussions

    1. Email Site-Wide Error message (with code snippet)
      I would like to create a Site-Wide error message that is emailed to the administrator. I have a lot of the information I want to put in but I can...
    2. retry does not work
      matz: you rejected my rcr on garden saying that retry already does it. but it does not. maybe there's a trick to it? but i could not read your...
    3. ssl bad write retry
      I've got a Ruby script that uploads a file to another Ruby cgi script. It works fine on a non-ssl site, but when I use the SSL site with net/https,...
    4. Code Snippet: Array.shuffle
      Here is another small Ruby code snippet that extends the Array class. Use this to randomize the order of all entries in an Array. class Array...
    5. Ruby Code Snippet: Array.count
      On Sat 05 Jul 2003 at 08:01:38 +0900, Stefan Arentz wrote: This is already available with Enumerable#find_all: a = (1..10).to_a puts...
  3. #2

    Default Re: A code snippet: Controlled retry


    On Nov 23, 2003, at 22:11, Hal Fulton wrote:
    > def try(quiet=true,times=5,secs=1,&block)
    > count = 0
    > catch :finished do
    > begin
    > block.call
    > rescue => err
    > puts "Error was: #{err}" if not quiet
    > count += 1
    > throw :finished if count > times
    > sleep secs
    > retry
    > end
    > end
    > end

    def try(quiet=true,times=5,secs=1,&block)
    return unless block
    times.times do
    begin
    return block.call
    rescue => err
    puts "Error was: #{err}" unless quiet
    sleep secs
    end
    end
    end


    Sincerely,
    Gennady Bystritsky




    Gennady Guest

  4. #3

    Default Re: A code snippet: Controlled retry

    On Nov 23, 2003, at 23:02, Gennady wrote:
    >
    > On Nov 23, 2003, at 22:11, Hal Fulton wrote:
    >
    >> def try(quiet=true,times=5,secs=1,&block)
    >> count = 0
    >> catch :finished do
    >> begin
    >> block.call
    >> rescue => err
    >> puts "Error was: #{err}" if not quiet
    >> count += 1
    >> throw :finished if count > times
    >> sleep secs
    >> retry
    >> end
    >> end
    >> end
    >
    >
    > def try(quiet=true,times=5,secs=1,&block)
    > return unless block
    > times.times do
    > begin
    > return block.call
    > rescue => err
    > puts "Error was: #{err}" unless quiet
    > sleep secs
    > end
    > end
    > end
    >
    Oops, times.times should become times.next.times to function like the
    original. Also, when all attempts are exhausted, the last exception
    must be re-raised or another more appropriate one raised (boolean
    return would also do the trick).



    Gennady Guest

  5. #4

    Default Re: A code snippet: Controlled retry


    "Gennady" <bystr@mac.com> schrieb im Newsbeitrag
    news:4BB0276B-1E4F-11D8-A73D-0003939AEA24@mac.com...
    > On Nov 23, 2003, at 23:02, Gennady wrote:
    >
    > >
    > > On Nov 23, 2003, at 22:11, Hal Fulton wrote:
    > >
    > >> def try(quiet=true,times=5,secs=1,&block)
    > >> count = 0
    > >> catch :finished do
    > >> begin
    > >> block.call
    > >> rescue => err
    > >> puts "Error was: #{err}" if not quiet
    > >> count += 1
    > >> throw :finished if count > times
    > >> sleep secs
    > >> retry
    > >> end
    > >> end
    > >> end
    > >
    > >
    > > def try(quiet=true,times=5,secs=1,&block)
    > > return unless block
    > > times.times do
    > > begin
    > > return block.call
    > > rescue => err
    > > puts "Error was: #{err}" unless quiet
    > > sleep secs
    > > end
    > > end
    > > end
    > >
    >
    > Oops, times.times should become times.next.times to function like the
    > original. Also, when all attempts are exhausted, the last exception
    > must be re-raised or another more appropriate one raised (boolean
    > return would also do the trick).
    Combining the two of them (i.e. times.next and re-raising) by simply
    adding a line,
    plus adding a check to allow for non-sleeping retries,
    plus output parameter that can be an IO or an Array,
    plus changed the first return to an exception in order to let the user
    know he forgot something:

    def try(times=0, secs=nil, log=nil, &block)
    raise LocalJumpError, "no block given" unless block

    times.times do
    begin
    return block.call
    rescue => err
    log << "Error was: #{err}\n" if log
    sleep secs if secs
    end
    end

    block.call
    end

    Cheers

    robert

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139