Module: VCAP

Defined in:
lib/vcap/common.rb,
lib/vcap/quota.rb,
lib/vcap/stats.rb,
lib/vcap/config.rb,
lib/vcap/spec/em.rb,
lib/vcap/component.rb,
lib/vcap/subprocess.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/services/api/async_requests.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: ProcessUtils, Quota, Services, Spec Classes: Component, Config, Healthz, PidFile, PriorityQueueFIFO, PrioritySet, RollingMetric, Stats, Subprocess, SubprocessError, SubprocessReadError, SubprocessStatusError, SubprocessTimeoutError, ThreadSafeRollingMetric, Varz

Constant Summary collapse

WINDOWS =
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
A_ROOT_SERVER =
'1.2.3.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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vcap/common.rb', line 71

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vcap/common.rb', line 89

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/vcap/common.rb', line 25

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

.local_ip(route = A_ROOT_SERVER) ⇒ Object



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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vcap/common.rb', line 54

def self.num_cores
  if RUBY_PLATFORM =~ /linux/
    return `cat /proc/cpuinfo | grep processor | wc -l`.to_i
  elsif RUBY_PLATFORM =~ /darwin/
    `sysctl -n hw.ncpu`.strip.to_i
  elsif RUBY_PLATFORM =~ /freebsd|netbsd/
    `sysctl hw.ncpu`.strip.to_i
  elsif WINDOWS
    (ENV['NUMBER_OF_PROCESSORS'] || 1).to_i
  else
    return 1 # unknown..
  end
rescue
  # In any case, let's always assume that there is 1 core
  1
end

.pp_bytesize(bsize) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vcap/common.rb', line 117

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)


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vcap/common.rb', line 105

def self.process_running?(pid)
  return false unless pid && (pid > 0)
  if WINDOWS
    output = %x[tasklist /nh /fo csv /fi "pid eq #{pid}"]
  else
    output = %x[ps -o rss= -p #{pid}]
  end
  return true if ($? == 0 && !output.empty?)
  # fail otherwise..
  return false
end

.secure_uuidObject



21
22
23
# File 'lib/vcap/common.rb', line 21

def self.secure_uuid
  result = UUIDTools::UUID.random_create.to_s.delete('-')
end

.symbolize_keys(hash) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/vcap/common.rb', line 130

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/vcap/common.rb', line 36

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

.uptime_string_to_seconds(string) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# File 'lib/vcap/common.rb', line 47

def self.uptime_string_to_seconds(string)
  parts = string.split(":", 4).map { |i| i.to_i}
  raise ArgumentError.new("Invalid format") unless parts.size == 4
  days, hours, mins, secs = parts
  secs + (mins * 60) + (hours * 3600) + (days * 24 * 3600)
end