Class: Discover::Registration

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/discover.rb

Constant Summary collapse

HEARTBEAT_INTERVAL =
5

Instance Method Summary collapse

Constructor Details

#initialize(client, name, address, attributes = {}, standby = false) ⇒ Registration

Returns a new instance of Registration.



59
60
61
62
63
64
65
# File 'lib/discover.rb', line 59

def initialize(client, name, address, attributes = {}, standby = false)
  @client     = client
  @name       = name
  @address    = address
  @attributes = attributes
  @standby    = standby
end

Instance Method Details

#registerObject



67
68
69
70
71
# File 'lib/discover.rb', line 67

def register
  send_register_request
  start_heartbeat
  wait_for_election if @standby
end

#send_register_requestObject



79
80
81
82
83
84
85
86
87
# File 'lib/discover.rb', line 79

def send_register_request
  args = {
    "Name"  => @name,
    "Addr"  => @address,
    "Attrs" => @attributes
  }

  @client.request("Agent.Register", args).value
end

#send_unregister_requestObject



89
90
91
92
93
94
95
96
# File 'lib/discover.rb', line 89

def send_unregister_request
  args = {
    "Name"  => @name,
    "Addr"  => @address
  }

  @client.request("Agent.Unregister", args).value
end

#start_heartbeatObject



98
99
100
101
102
103
104
105
106
# File 'lib/discover.rb', line 98

def start_heartbeat
  @heartbeat = every(HEARTBEAT_INTERVAL) do
    @client.request(
      "Agent.Heartbeat",
      "Name" => @name,
      "Addr" => @address
    )
  end
end

#stop_heartbeatObject



108
109
110
# File 'lib/discover.rb', line 108

def stop_heartbeat
  @heartbeat.cancel
end

#unregisterObject



73
74
75
76
77
# File 'lib/discover.rb', line 73

def unregister
  stop_heartbeat
  send_unregister_request
  @client.remove_registration(self)
end

#wait_for_electionObject



112
113
114
115
# File 'lib/discover.rb', line 112

def wait_for_election
  async.watch_leaders
  wait :elected
end

#watch_leadersObject



117
118
119
120
121
122
123
# File 'lib/discover.rb', line 117

def watch_leaders
  @client.service(@name).each_leader do |leader|
    if leader.address == @address
      signal :elected
    end
  end
end