Module: Leeroy::Helpers::Polling

Includes:
Leeroy::Helpers, Env
Included in:
State, Task::Image, Task::Instantiate
Defined in:
lib/leeroy/helpers/polling.rb

Constant Summary collapse

POLL_CALLBACK =
lambda {|x| raise 'this is the default callback, did you forget to set the poll_callback attribute?'}
POLL_TIMEOUT =

seconds

600
POLL_INTERVAL =

seconds

10

Instance Attribute Summary collapse

Attributes included from Env

#env

Instance Method Summary collapse

Methods included from Env

#checkEnv

Instance Attribute Details

#poll_callbackObject

Returns the value of attribute poll_callback.



17
18
19
# File 'lib/leeroy/helpers/polling.rb', line 17

def poll_callback
  @poll_callback
end

#poll_intervalObject

Returns the value of attribute poll_interval.



17
18
19
# File 'lib/leeroy/helpers/polling.rb', line 17

def poll_interval
  @poll_interval
end

#poll_responseObject

Returns the value of attribute poll_response.



17
18
19
# File 'lib/leeroy/helpers/polling.rb', line 17

def poll_response
  @poll_response
end

#poll_timeoutObject

Returns the value of attribute poll_timeout.



17
18
19
# File 'lib/leeroy/helpers/polling.rb', line 17

def poll_timeout
  @poll_timeout
end

Instance Method Details

#initialize(*args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/leeroy/helpers/polling.rb', line 52

def initialize(*args, &block)
  begin
    super

    @poll_callback = POLL_CALLBACK
    @poll_timeout = POLL_TIMEOUT
    @poll_interval = POLL_INTERVAL

  rescue StandardError => e
    raise e
  end
end

#poll(*args) ⇒ Object



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
# File 'lib/leeroy/helpers/polling.rb', line 19

def poll(*args)
  begin
    logger.debug "beginning to poll"

    callback = self.poll_callback
    raise "callback must be a Proc" unless callback.kind_of?(Proc)

    timeout = self.poll_timeout
    interval = self.poll_interval

    logger.debug "callback: #{callback.inspect}"
    logger.debug "polling every #{interval} seconds for #{timeout} seconds"

    SmartPolling.poll(timeout: timeout, interval: interval) do
      poll_arg = args[0]
      logger.debug "poll_arg: #{poll_arg.inspect}"
      self.poll_response = callback.call(poll_arg)
    end

    response = self.poll_response
    logger.debug "response: #{response.inspect}"

    response

  rescue Interrupt => e
    logger.fatal "Keyboard interrupt"
    raise e

  rescue StandardError => e
    raise e
  end
end