Module: UAE

Defined in:
lib/uae/common.rb,
lib/uae/component.rb

Defined Under Namespace

Classes: Component, Healthz, PidFile, Varz

Constant Summary collapse

RACK_JSON_HDR =
{ 'Content-Type' => 'application/json' }
RACK_TEXT_HDR =
{ 'Content-Type' => 'text/plaintext' }

Class Method Summary collapse

Class Method Details

.grab_ephemeral_port(range = (40000..50000)) ⇒ Object

获取一个随机端口



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uae/common.rb', line 20

def self.grab_ephemeral_port(range = (40000..50000))
  range.each do |i|
    socket = nil
    begin
      socket = TCPServer.new('0.0.0.0', i)
      socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
      Socket.do_not_reverse_lookup = true
      port = socket.addr[1]
      socket.close
      return port
    rescue Exception => e
      socket.close if socket
      next
    end
  end
end

.local_ip(route) ⇒ Object

获取本机ip



7
8
9
10
11
12
# File 'lib/uae/common.rb', line 7

def self.local_ip(route)
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
  UDPSocket.open {|s| s.connect(route, 1); s.addr.last }
ensure
  Socket.do_not_reverse_lookup = orig
end

.process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/uae/common.rb', line 37

def self.process_running?(pid)
  return false unless pid && (pid > 0)
  output = %x[ps -o rss= -p #{pid}]
  return true if ($? == 0 && !output.empty?)
  # fail otherwise..
  return false
end

.secure_uuidObject

获取一个uuid



15
16
17
# File 'lib/uae/common.rb', line 15

def self.secure_uuid
  result = File.open('/dev/urandom') { |x| x.read(16).unpack('H*')[0] }
end