Module: LoopHard

Defined in:
lib/loop_hard.rb,
lib/loop_hard/loop.rb,
lib/loop_hard/logger.rb,
lib/loop_hard/version.rb,
lib/loop_hard/signal_trap.rb,
lib/loop_hard/sidekiq_trap.rb,
lib/loop_hard/timeout_trap.rb

Overview

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

Defined Under Namespace

Modules: SidekiqTrap, SignalTrap, TimeoutTrap

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Set the Logger for LoopHard



6
7
8
# File 'lib/loop_hard/logger.rb', line 6

def logger
  @logger ||= Logger.new($stdout)
end

Class Method Details

.loop(options = {}) ⇒ Object

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.

Parameters:

  • options (defaults to: {})
    • 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)



9
10
11
12
13
14
15
16
17
18
19
# 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