Class: EbisuConnection::Slave

Inherits:
Object
  • Object
show all
Defined in:
lib/ebisu_connection/slave.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf, slave_group) ⇒ Slave

Returns a new instance of Slave.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ebisu_connection/slave.rb', line 5

def initialize(conf, slave_group)
  case conf
  when String
    host, weight = conf.split(/\s*,\s*/)
    @hostname, port = host.split(/\s*:\s*/)
  when Hash
    @hostname = conf["host"] || conf[:host]
    weight = conf["weight"] || conf[:weight]
    port = conf["port"] || conf[:port]
  else
    raise ArgumentError, "slaves config is invalid"
  end

  spec = modify_spec(port)
  @connection_factory = FreshConnection::ConnectionFactory.new(slave_group, spec)
  @weight = (weight || 1).to_i
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



3
4
5
# File 'lib/ebisu_connection/slave.rb', line 3

def hostname
  @hostname
end

#weightObject (readonly)

Returns the value of attribute weight.



3
4
5
# File 'lib/ebisu_connection/slave.rb', line 3

def weight
  @weight
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ebisu_connection/slave.rb', line 27

def active?
  connection.active?
end

#connectionObject



23
24
25
# File 'lib/ebisu_connection/slave.rb', line 23

def connection
  @connection ||= @connection_factory.new_connection
end

#disconnect!Object



31
32
33
34
35
36
37
# File 'lib/ebisu_connection/slave.rb', line 31

def disconnect!
  if @connection
    @connection.disconnect!
    @connection = nil
  end
rescue
end

#modify_spec(port) ⇒ Object



39
40
41
42
43
44
# File 'lib/ebisu_connection/slave.rb', line 39

def modify_spec(port)
  modify_spec = {"host" => @hostname}
  return modify_spec unless port
  modify_spec["port"] = port.to_i if port.is_a?(Integer) || !port.empty?
  modify_spec
end