Module: QueueWrangler::Wrangler

Defined in:
lib/queuewrangler/wrangler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/queuewrangler/wrangler.rb', line 5

def queue
  @queue
end

Instance Method Details

#dead?Boolean

Determine whether the runloop is dead or not

Returns:

  • (Boolean)


24
25
26
# File 'lib/queuewrangler/wrangler.rb', line 24

def dead?
  @should_die
end

#die!Object

Instruct the runloop to die and flush our internal queue of events



29
30
31
32
# File 'lib/queuewrangler/wrangler.rb', line 29

def die!
  @should_die = true
  process_queue
end

#enqueue(method, *args) ⇒ Object



68
69
70
# File 'lib/queuewrangler/wrangler.rb', line 68

def enqueue(method, *args)
  @queue << [method, [*args]]
end

#initialize_queuewrangler(enabled, logger = nil) ⇒ Object

Initializes the queue and performs other prepatory work required before

queue processing may begin.

@param [Boolean] enabled True if the queue should be processed
@param [Logger] logger Where to log or no logging if not specified


12
13
14
15
16
17
# File 'lib/queuewrangler/wrangler.rb', line 12

def initialize_queuewrangler(enabled, logger=nil)
  @enabled = enabled
  @queue = Queue.new
  @should_die = false
  @logger = logger || Logger.new(nil)
end

#logObject



19
20
21
# File 'lib/queuewrangler/wrangler.rb', line 19

def log
  @logger
end

#process_queueObject

Flush the outstanding events in the events queue



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/queuewrangler/wrangler.rb', line 35

def process_queue
  until @queue.empty?
    method, args = @queue.pop

    unless method.nil?
      log.info("process_queue - handling #{method} with #{args.inspect}")
      if @enabled
        processed = send(method, *args)

        unless processed
          log.info "Failed to process #{method}"
          enqueue(method, *args)
          # Arbitrary sleep to make sure we don't spin when we cannot
          # process a message infinitly
          sleep 1
        end
      end
    end
  end
end

#run!Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/queuewrangler/wrangler.rb', line 56

def run!
  id = self.class.name.split('::').last
  log.info "#{id}#runloop - starting the runloop, processing: #{@enabled}"
  until dead?
    process_queue
    # Arbitrary sleep just to make sure we don't spin on
    # #process_queue when the queue is completely empty
    sleep 0.5
  end
  log.info "#{id}#runloop - ending the runloop"
end