Class: WhoCan::Heartbeater::Beat

Inherits:
Object
  • Object
show all
Includes:
Deferred, Logging
Defined in:
lib/who_can/heartbeater/beat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(channel, queue, beat_id, timeout = nil) ⇒ Beat

Returns a new instance of Beat.

Parameters:

  • channel (AMQP::Channel)

    the amqp channel we are to use

  • beat_id (String)

    a unique identifier for this beat instance

  • timeout (Numeric) (defaults to: nil)

    how long do we wait until we decide this beat was a failure. if nil, no timeout will be set. Timeout is started before we open the channel, and is cancelled on successful return message



21
22
23
24
25
26
27
# File 'lib/who_can/heartbeater/beat.rb', line 21

def initialize(channel, queue, beat_id, timeout=nil)
  @channel = channel
  @queue   = queue
  @beat_id = beat_id
  @timeout_after = timeout
  @started = false
end

Instance Attribute Details

#beat_idObject (readonly)

Returns the value of attribute beat_id.



10
11
12
# File 'lib/who_can/heartbeater/beat.rb', line 10

def beat_id
  @beat_id
end

#channelObject (readonly)

Returns the value of attribute channel.



10
11
12
# File 'lib/who_can/heartbeater/beat.rb', line 10

def channel
  @channel
end

#queueObject (readonly)

Returns the value of attribute queue.



10
11
12
# File 'lib/who_can/heartbeater/beat.rb', line 10

def queue
  @queue
end

#timeout_afterObject (readonly)

Returns the value of attribute timeout_after.



10
11
12
# File 'lib/who_can/heartbeater/beat.rb', line 10

def timeout_after
  @timeout_after
end

Instance Method Details

#cancel!Object



37
38
39
40
41
42
43
44
45
# File 'lib/who_can/heartbeater/beat.rb', line 37

def cancel!
  return if fired?

  logger.info { "#{beat_id} cancel called!" }

  cancel_timeout
  @cancelled = true
  succeed
end

#cancelled?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/who_can/heartbeater/beat.rb', line 47

def cancelled?
  @cancelled
end

#fired?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/who_can/heartbeater/beat.rb', line 33

def fired?
  !pending?
end

#pending?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/who_can/heartbeater/beat.rb', line 29

def pending?
  @deferred_status.nil? or (@deferred_status == :unknown)
end

#ping_received!Object



51
52
53
# File 'lib/who_can/heartbeater/beat.rb', line 51

def ping_received!
  succeed if pending? and not cancelled?
end

#start!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/who_can/heartbeater/beat.rb', line 55

def start!
  return if @started
  @started = true

  timeout(timeout_after) if timeout_after

  channel.once_opened do
    errback_on_exception do
      channel.default_exchange.publish('BEAT', :routing_key => queue.name, :message_id => beat_id) do
        logger.debug { "message #{beat_id} sent" }
        after_publish.succeed
      end
    end
  end
end