module LoopHard

Have loops with a timeout, that listen to signals to know if they should stop prematurely

Constants

VERSION

Attributes

logger[W]

Public Class Methods

logger() click to toggle source

Set the Logger for LoopHard

# File lib/loop_hard/logger.rb, line 6
def logger
  @logger ||= Logger.new($stdout)
end
loop(options = {}) { || ... } click to toggle source

Loop until the passed in block breaks, or until one of the traps open. Will yield to the block passed in until it decides to stop looping. Your block should break once it's done. @param options

- timeout [Int] Maximum time to run (eg: "10" for 10 seconds, 10.minutes if you are using Rails). Defaults to infinite
- traps [Array of Classes] Which traps to run. Defaults to [SidekiqTrap, SignalTrap, TimeoutTrap] (all of them)
# File lib/loop_hard/loop.rb, line 9
def loop(options = {})
  default_options = { timeout: nil,
                      traps: [SidekiqTrap, SignalTrap, TimeoutTrap] }
  options = default_options.merge(options)

  options[:maximum_end_time] = (options[:timeout] ? Time.now + options[:timeout] : nil)

  while continue_looping?(options)
    yield # Block will call "break" once it's out of rows, which will break this loop
  end
end

Private Class Methods

continue_looping?(options) click to toggle source

Decide whether to continue looping, based on the traps involved.

# File lib/loop_hard/loop.rb, line 24
def continue_looping?(options)
  options[:traps].all?{|trap| trap.continue?(options)}
end