Class: EbDeployer::EventPoller

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/eb_deployer/event_poller.rb

Constant Summary collapse

POLL_INTERVAL =
15

Constants included from Utils

Utils::BACKOFF_INITIAL_SLEEP

Instance Method Summary collapse

Methods included from Utils

#backoff, #reject_nil, #symbolize_keys

Constructor Details

#initialize(event_source) ⇒ EventPoller

Returns a new instance of EventPoller.



6
7
8
# File 'lib/eb_deployer/event_poller.rb', line 6

def initialize(event_source)
  @event_source = event_source
end

Instance Method Details

#get_anchorObject



10
11
12
# File 'lib/eb_deployer/event_poller.rb', line 10

def get_anchor
  @event_source.get_anchor
end

#poll(from_anchor, &block) ⇒ Object



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
# File 'lib/eb_deployer/event_poller.rb', line 14

def poll(from_anchor, &block)
  handled = Set.new
  loop do
    @event_source.fetch_events(from_anchor) do |events|
      # events from event source is latest first order
      to_be_handled = []
      reached_anchor = false

      events.each do |event|
        if digest(event) == digest(from_anchor)
          reached_anchor = true
        end

        if !handled.include?(digest(event)) && !reached_anchor
          to_be_handled << event
        end
      end

      to_be_handled.reverse.each do |event|
        yield(event)
        handled << digest(event)
      end

      !reached_anchor
    end
    sleep POLL_INTERVAL
  end
end