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.



51
52
53
54
55
56
57
# File 'lib/discover.rb', line 51

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

Instance Method Details

#registerObject



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

def register
  send_register_request
  start_heartbeat
  wait_for_election if @standby
end

#send_register_requestObject



71
72
73
74
75
76
77
78
79
# File 'lib/discover.rb', line 71

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

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

#send_unregister_requestObject



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

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

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

#start_heartbeatObject



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

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

#stop_heartbeatObject



100
101
102
# File 'lib/discover.rb', line 100

def stop_heartbeat
  @heartbeat.cancel
end

#unregisterObject



65
66
67
68
69
# File 'lib/discover.rb', line 65

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

#wait_for_electionObject



104
105
106
107
# File 'lib/discover.rb', line 104

def wait_for_election
  async.watch_leaders
  wait :elected
end

#watch_leadersObject



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

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