Module: VCAP

Defined in:
lib/vcap/common.rb,
lib/vcap/quota.rb,
lib/vcap/config.rb,
lib/vcap/spec/em.rb,
lib/vcap/component.rb,
lib/vcap/subprocess.rb,
lib/vcap/json_schema.rb,
lib/services/api/util.rb,
lib/services/api/const.rb,
lib/vcap/process_utils.rb,
lib/vcap/priority_queue.rb,
lib/vcap/rolling_metric.rb,
lib/services/api/messages.rb,
lib/vcap/user_pools/user_ops.rb,
lib/vcap/user_pools/user_pool.rb,
lib/services/api/async_requests.rb,
lib/vcap/user_pools/user_pool_util.rb,
lib/vcap/spec/forked_component/base.rb,
lib/vcap/spec/forked_component/nats_server.rb,
lib/services/api/clients/service_gateway_client.rb

Overview

Copyright © 2009-2011 VMware, Inc.

Defined Under Namespace

Modules: JsonSchema, ProcessUtils, Quota, Services, Spec, UserOps, UserPoolUtil Classes: Component, Config, Healthz, PidFile, PriorityQueueFIFO, PrioritySet, RollingMetric, Subprocess, SubprocessError, SubprocessReadError, SubprocessStatusError, SubprocessTimeoutError, ThreadSafeRollingMetric, UserPool, Varz

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.defer(*args, &blk) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vcap/common.rb', line 59

def self.defer(*args, &blk)
  if args[0].kind_of?(Hash)
    op = blk
    opts = args[0]
  else
    op = args[0] || blk
    opts = args[1] || {}
  end

  callback = opts[:callback]
  logger = opts[:logger]
  nobacktrace = opts[:nobacktrace]

  wrapped_operation = exception_wrap_block(op, logger, nobacktrace)
  wrapped_callback = callback ? exception_wrap_block(callback, logger, nobacktrace) : nil
  EM.defer(wrapped_operation, wrapped_callback)
end

.exception_wrap_block(op, logger, nobacktrace = false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vcap/common.rb', line 77

def self.exception_wrap_block(op, logger, nobacktrace=false)
  Proc.new do |*args|
    begin
      op.call(*args)
    rescue => e
      err_str = "#{e} - #{e.backtrace.join("\n")}" unless nobacktrace
      err_str = "#{e}" if nobacktrace
      if logger
        logger.fatal(err_str)
      else
        $stderr.puts(err_str)
      end
    end
  end
end

.grab_ephemeral_portObject



23
24
25
26
27
28
29
30
# File 'lib/vcap/common.rb', line 23

def self.grab_ephemeral_port
  socket = TCPServer.new('0.0.0.0', 0)
  socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
  Socket.do_not_reverse_lookup = true
  port = socket.addr[1]
  socket.close
  return port
end

.local_ip(route = A_ROOT_SERVER) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/vcap/common.rb', line 11

def self.local_ip(route = A_ROOT_SERVER)
  route ||= A_ROOT_SERVER
  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

.num_coresObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vcap/common.rb', line 43

def self.num_cores
  if RUBY_PLATFORM =~ /linux/
    return `cat /proc/cpuinfo | grep processor | wc -l`.to_i
  elsif RUBY_PLATFORM =~ /darwin/
    `hwprefs cpu_count`.strip.to_i
  elsif RUBY_PLATFORM =~ /freebsd|netbsd/
    `sysctl hw.ncpu`.strip.to_i
  else
    return 1 # unknown..
  end
rescue
  # hwprefs doesn't always exist, and so the block above can fail.
  # In any case, let's always assume that there is 1 core
  1
end

.pp_bytesize(bsize) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vcap/common.rb', line 101

def self.pp_bytesize(bsize)
  units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
  base  = 1000
  bsize = bsize.to_f()
  quotient = unit = nil
  units.each_with_index do |u, i|
    unit = u
    quotient = bsize / (base ** i)
    break if quotient < base
  end
  "%0.2f%s" % [quotient, unit]
end

.process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/vcap/common.rb', line 93

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



19
20
21
# File 'lib/vcap/common.rb', line 19

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

.symbolize_keys(hash) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/vcap/common.rb', line 114

def self.symbolize_keys(hash)
  if hash.is_a? Hash
    new_hash = {}
    hash.each {|k, v| new_hash[k.to_sym] = symbolize_keys(v) }
    new_hash
  else
    hash
  end
end

.uptime_string(delta) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/vcap/common.rb', line 32

def self.uptime_string(delta)
  num_seconds = delta.to_i
  days = num_seconds / (60 * 60 * 24);
  num_seconds -= days * (60 * 60 * 24);
  hours = num_seconds / (60 * 60);
  num_seconds -= hours * (60 * 60);
  minutes = num_seconds / 60;
  num_seconds -= minutes * 60;
  "#{days}d:#{hours}h:#{minutes}m:#{num_seconds}s"
end