Skip to content

Commit

Permalink
Merge "Add varz support for serialization data server." into services…
Browse files Browse the repository at this point in the history
…-r10
  • Loading branch information
Chunjie authored and Gerrit Code Review committed Mar 31, 2012
2 parents 38f9ce0 + 41132b6 commit e637210
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions serialization_data_server/Gemfile
Expand Up @@ -6,6 +6,7 @@ gem "thin"
gem "sinatra"
gem "redis"
gem "rake"
gem "sys-filesystem"

gem 'vcap_common', '>= 1.0.8', :require => ['vcap/common', 'vcap/component']
gem 'vcap_logging', '>=0.1.3', :require => ['vcap/logging']
Expand Down
4 changes: 4 additions & 0 deletions serialization_data_server/Gemfile.lock
Expand Up @@ -7,6 +7,7 @@ GEM
daemons (1.1.5)
diff-lcs (1.1.3)
eventmachine (0.12.11.cloudfoundry.3)
ffi (1.0.11)
json_pure (1.6.5)
multi_json (1.0.4)
nats (0.4.22.beta.8)
Expand Down Expand Up @@ -41,6 +42,8 @@ GEM
rack (~> 1.3, >= 1.3.4)
rack-protection (~> 1.1, >= 1.1.2)
tilt (~> 1.3, >= 1.3.3)
sys-filesystem (1.0.0)
ffi (= 1.0.11)
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
Expand Down Expand Up @@ -70,6 +73,7 @@ DEPENDENCIES
simplecov
simplecov-rcov
sinatra
sys-filesystem
thin
uuidtools
vcap_common (>= 1.0.8)
Expand Down
47 changes: 47 additions & 0 deletions serialization_data_server/lib/server.rb
@@ -1,8 +1,13 @@
# Copyright (c) 2009-2011 VMware, Inc.
require "eventmachine"
require "vcap/common"
require "vcap/component"
require "sinatra"
require "nats/client"
require "redis"
require "json"
require "sys/filesystem"
include Sys

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'mysql')

Expand Down Expand Up @@ -40,8 +45,26 @@ def initialize(opts)
end
end
@nats = NATS.connect(:uri => opts[:mbus]) {
VCAP::Component.register(
:nats => @nats,
:type => "SerializationDataServer",
:index => opts[:index] || 0,
:config => opts
)

on_connect_nats
}

z_interval = opts[:z_interval] || 30
EM.add_periodic_timer(z_interval) do
EM.defer { update_varz }
end if @nats

# Defer 5 seconds to give service a change to wake up
EM.add_timer(5) do
EM.defer { update_varz }
end if @nats

Kernel.at_exit do
if EM.reactor_running?
send_deactivation_notice(false)
Expand All @@ -65,6 +88,30 @@ def on_connect_nats()
@redis = connect_redis
end

def varz_details()
varz = {}
# check NFS disk free space
free_space = 0
begin
stats = Filesystem.stat("#{@base_dir}")
avail_blocks = stats.blocks_available
total_blocks = stats.blocks
free_space = format("%.2f", avail_blocks.to_f / total_blocks.to_f * 100)
rescue => e
@logger.error("Failed to get filesystem info of #{@base_dir}: #{e}")
end
varz[:nfs_free_space] = free_space

varz
end

def update_varz()
varz = varz_details
varz.each { |k, v|
VCAP::Component.varz[k] = v
}
end

def connect_redis()
redis_config = %w(host port password).inject({}){|res, o| res[o.to_sym] = @opts[:redis][o]; res}
Redis.new(redis_config)
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit e637210

Please sign in to comment.