Module: Regurgitator::Device

Includes:
ServerSettings
Included in:
FileInfo
Defined in:
lib/regurgitator/device.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServerSettings

#refresh_zone, #refresh_zone_unlocked, #server_settings_init, #zone_for

Class Method Details

.extended(obj) ⇒ Object



16
17
18
# File 'lib/regurgitator/device.rb', line 16

def self.extended(obj)
  obj.device_init
end

Instance Method Details

#device_initObject



9
10
11
12
13
14
# File 'lib/regurgitator/device.rb', line 9

def device_init
  server_settings_init
  @dev_cache_mtime = 0
  @dev_cache = nil
  @dev_cache_lock = Mutex.new
end

#device_uris!(opts, get_port) ⇒ Object



20
21
22
23
# File 'lib/regurgitator/device.rb', line 20

def device_uris!(opts, get_port)
  opts[:port] = get_port if get_port
  [ URI::HTTP.build(opts) ]
end

#refresh_device(force = false) ⇒ Object

Returns a hash of device info with the Integer devid as the hash key.



27
28
29
# File 'lib/regurgitator/device.rb', line 27

def refresh_device(force = false) # :nodoc:
  @dev_cache_lock.synchronize { refresh_device_unlocked(force) }
end

#refresh_device_unlocked(force) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/regurgitator/device.rb', line 31

def refresh_device_unlocked(force) # :nodoc:
  return @dev_cache if !force && ((Regurgitator.now - @dev_cache_mtime) < 60)
  tmp = {}.compare_by_identity
  refresh_zone(force)

  sql = <<-EOS.freeze
SELECT d.devid, h.hostip, h.http_port, h.http_get_port
FROM device d
LEFT JOIN host h ON d.hostid = h.hostid
WHERE d.status IN ('readonly','alive','drain') AND h.status = 'alive'
  EOS
  @db[sql].each do |x|
    # devices in "drain" status may hit raciness try those as a last resort
    x[:preferred] = !!(x[:d_status] =~ %r{\A(?:readonly|alive)\z})
    hostip = x[:hostip]
    port = x[:http_port] || 80
    get_port = x[:http_get_port]
    x[:ipaddr] = { :hostip => hostip }
    x[:zone] = zone_for(hostip)
    devid = x[:devid]
    o = { :path => "/dev#{devid}", :port => port, :host => hostip }
    x[:uris] = { :pri => device_uris!(o, get_port) }
    tmp[devid] = x
  end
  Regurgitator::Local.refresh_addrs!
  @dev_cache_mtime = Regurgitator.now
  @dev_cache = tmp
end