Skip to content

Commit

Permalink
periodically (given the router.start message) announce the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregg Van Hove and Sarah Chandler committed Jun 26, 2013
1 parent 5e9e431 commit 120b5ba
Show file tree
Hide file tree
Showing 48 changed files with 281 additions and 79 deletions.
11 changes: 8 additions & 3 deletions router_registrar/Gemfile
@@ -1,6 +1,11 @@
source :rubygems
source 'https://rubygems.org'

gem "eventmachine"
gem "nats"
gem "vcap_logging"
gem "yajl-ruby"
gem "yajl-ruby"
gem "vcap-concurrency", :git => "https://github.com/cloudfoundry/vcap-concurrency.git"
gem "cf-message-bus", :github => "cloudfoundry/cf-message-bus"

group :development, :test do
gem "rspec"
end
55 changes: 45 additions & 10 deletions router_registrar/Gemfile.lock
@@ -1,21 +1,56 @@
GIT
remote: git://github.com/cloudfoundry/cf-message-bus.git
revision: 88a9fc0fa75fca29a8fd1fb47c6504a252020a0f
specs:
cf-message-bus (0.0.1)
eventmachine (~> 1.0.0)
nats (= 0.4.26)
vcap-concurrency
yajl-ruby

GIT
remote: https://github.com/cloudfoundry/vcap-concurrency.git
revision: dfddfb0bc0e599b94390ef8c026f71c4ddc323f7
specs:
vcap-concurrency (0.1.0)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
daemons (1.1.4)
eventmachine (0.12.10)
json_pure (1.5.3)
nats (0.4.10)
daemons (>= 1.1.0)
daemons (1.1.9)
diff-lcs (1.2.4)
eventmachine (1.0.3)
json_pure (1.8.0)
nats (0.4.26)
daemons (>= 1.1.5)
eventmachine (>= 0.12.10)
json_pure (>= 1.5.1)
vcap_logging (0.1.0)
yajl-ruby (0.8.2)
json_pure (>= 1.7.3)
thin (>= 1.4.1)
rack (1.5.2)
rake (10.1.0)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
vcap_logging (0.1.4)
rake
yajl-ruby (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
cf-message-bus!
eventmachine
nats
rspec
vcap-concurrency!
vcap_logging
yajl-ruby
35 changes: 16 additions & 19 deletions router_registrar/lib/router_registrar.rb
Expand Up @@ -4,20 +4,19 @@
require "bundler/setup"

require "eventmachine"
require "nats/client"
require "vcap/logging"
require "cf_message_bus/message_bus"

module RouterRegistrar

class Config
class << self
[:logger, :nats_uri, :uri, :host, :port, :tags].each { |option| attr_accessor option }
attr_accessor :logger, :message_bus_uri, :uri, :host, :port, :tags

def configure(config)
VCAP::Logging.setup_from_config(config["logging"])
@logger = VCAP::Logging.logger("router_registrar")

@nats_uri = config["mbus"]
@message_bus_uri = config["mbus"]

@uri = config["uri"]
@host = config["host"]
Expand All @@ -35,26 +34,24 @@ class RouterRegistrar
def initialize
@logger = Config.logger

@registration_message = Yajl::Encoder.encode({
@registration_message = {
:host => Config.host,
:port => Config.port,
:uris => [Config.uri],
:uris => Array(Config.uri),
:tags => Config.tags
})

NATS.on_error do |e|
@logger.fatal("Exiting, NATS error")
@logger.fatal(e)
exit
end
}

@nats = NATS.connect(:uri => Config.nats_uri) do
@logger.info("Connected to NATS")
@nats.subscribe(ROUTER_START_TOPIC) do
@message_bus = CfMessageBus::MessageBus.new(uri: Config.message_bus_uri)

@logger.info("Connected to NATS")
@message_bus.subscribe(ROUTER_START_TOPIC) do |message|
send_registration_message
EM.cancel_timer(@registration_timer) if @registration_timer
@registration_timer = EM.add_periodic_timer(message[:minimumRegisterIntervalInSeconds]) do
send_registration_message
end
send_registration_message
end
send_registration_message
end

def shutdown(&block)
Expand All @@ -63,12 +60,12 @@ def shutdown(&block)

def send_registration_message
@logger.info("Sending registration: #{@registration_message}")
@nats.publish(ROUTER_REGISTER_TOPIC, @registration_message)
@message_bus.publish(ROUTER_REGISTER_TOPIC, @registration_message)
end

def send_unregistration_message(&block)
@logger.info("Sending unregistration: #{@registration_message}")
@nats.publish(ROUTER_UNREGISTER_TOPIC, @registration_message, &block)
@message_bus.publish(ROUTER_UNREGISTER_TOPIC, @registration_message, &block)
end

end
Expand Down
66 changes: 66 additions & 0 deletions router_registrar/spec/router_registrar_spec.rb
@@ -0,0 +1,66 @@
require_relative '../lib/router_registrar'
require 'cf_message_bus/mock_message_bus'

module RouterRegistrar
describe RouterRegistrar do
let(:message_bus) { CfMessageBus::MockMessageBus.new }
let(:bus_uri) { "a message bus uri" }
let(:config) do
{
'mbus' => bus_uri,
'host' => 'registrar.host',
'port' => 98765,
'uri' => 'fancyuri',
'tags' => 'taggy goodness',
'logging' => {}
}
end
before do
EM.stub(:add_periodic_timer)
EM.stub(:cancel_timer)
Config.configure(config)
Config.stub(:logger).and_return(double(:logger, info: nil, error: nil, debug: nil))
CfMessageBus::MessageBus.stub(:new).with(uri: bus_uri).and_return(message_bus)
end

describe "#register_with_router" do
let(:registration_message) do
{
host: config['host'],
port: config['port'],
uris: Array(config['uri']),
tags: config['tags']
}
end

before do
subject
end

it 'should register routes immediately' do
expect(message_bus).to have_published_with_message("router.register", registration_message)
end

it 'should registers upon a router.start message' do
message_bus.clear_published_messages
message_bus.publish("router.start", {minimumRegisterIntervalInSeconds: 33})

expect(message_bus).to have_published_with_message("router.register", registration_message)
end

it 'should periodically register with the router' do
EM.should_receive(:add_periodic_timer).with(33).and_return(:periodic_timer)
message_bus.publish("router.start", {minimumRegisterIntervalInSeconds: 33})
end

it 'should clear an existing timer when registering a new one' do
EM.should_receive(:add_periodic_timer).with(33).and_return(:periodic_timer)
message_bus.publish("router.start", {minimumRegisterIntervalInSeconds: 33})

EM.should_receive(:cancel_timer).with(:periodic_timer)
EM.should_receive(:add_periodic_timer).with(24)
message_bus.publish("router.start", {minimumRegisterIntervalInSeconds: 24})
end
end
end
end
Binary file removed router_registrar/vendor/cache/daemons-1.1.4.gem
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed router_registrar/vendor/cache/json_pure-1.5.3.gem
Binary file not shown.
Binary file not shown.
Binary file removed router_registrar/vendor/cache/nats-0.4.10.gem
Binary file not shown.
Binary file added router_registrar/vendor/cache/nats-0.4.26.gem
Binary file not shown.
Binary file added router_registrar/vendor/cache/rack-1.5.2.gem
Binary file not shown.
Binary file added router_registrar/vendor/cache/rake-10.1.0.gem
Binary file not shown.
Binary file added router_registrar/vendor/cache/rspec-2.13.0.gem
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added router_registrar/vendor/cache/thin-1.5.1.gem
Binary file not shown.
Binary file removed router_registrar/vendor/cache/vcap_logging-0.1.0.gem
Binary file not shown.
Binary file not shown.
Binary file removed router_registrar/vendor/cache/yajl-ruby-0.8.2.gem
Binary file not shown.
Binary file not shown.
9 changes: 7 additions & 2 deletions vcap_registrar/Gemfile
@@ -1,6 +1,11 @@
source :rubygems
source 'https://rubygems.org'

gem "eventmachine"
gem "nats"
gem "vcap_logging"
gem "yajl-ruby"
gem "vcap-concurrency", :git => "https://github.com/cloudfoundry/vcap-concurrency.git"
gem "cf-message-bus", :github => "cloudfoundry/cf-message-bus"

group :development, :test do
gem "rspec"
end
55 changes: 45 additions & 10 deletions vcap_registrar/Gemfile.lock
@@ -1,21 +1,56 @@
GIT
remote: git://github.com/cloudfoundry/cf-message-bus.git
revision: 88a9fc0fa75fca29a8fd1fb47c6504a252020a0f
specs:
cf-message-bus (0.0.1)
eventmachine (~> 1.0.0)
nats (= 0.4.26)
vcap-concurrency
yajl-ruby

GIT
remote: https://github.com/cloudfoundry/vcap-concurrency.git
revision: dfddfb0bc0e599b94390ef8c026f71c4ddc323f7
specs:
vcap-concurrency (0.1.0)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
daemons (1.1.4)
eventmachine (0.12.10)
json_pure (1.5.3)
nats (0.4.10)
daemons (>= 1.1.0)
daemons (1.1.9)
diff-lcs (1.2.4)
eventmachine (1.0.3)
json_pure (1.8.0)
nats (0.4.26)
daemons (>= 1.1.5)
eventmachine (>= 0.12.10)
json_pure (>= 1.5.1)
vcap_logging (0.1.0)
yajl-ruby (0.8.2)
json_pure (>= 1.7.3)
thin (>= 1.4.1)
rack (1.5.2)
rake (10.1.0)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
vcap_logging (0.1.4)
rake
yajl-ruby (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
cf-message-bus!
eventmachine
nats
rspec
vcap-concurrency!
vcap_logging
yajl-ruby

0 comments on commit 120b5ba

Please sign in to comment.