Skip to content

Commit

Permalink
pass registry instance rather than its name
Browse files Browse the repository at this point in the history
this allows to easily plug unoficial registries
  • Loading branch information
niamster committed Jan 9, 2015
1 parent 9f44deb commit 74572da
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
4 changes: 3 additions & 1 deletion examples/itchy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env ruby
require 'dcell'
require 'dcell/registries/redis_adapter'

DCell.start :id => "itchy"
registry = DCell::Registry::RedisAdapter.new :server => 'localhost'
DCell.start :id => "itchy", :registry => registry

class Itchy
include Celluloid
Expand Down
4 changes: 3 additions & 1 deletion examples/scratchy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env ruby
require 'dcell'
require 'dcell/registries/redis_adapter'

DCell.start
registry = DCell::Registry::RedisAdapter.new :server => 'localhost'
DCell.start :registry => registry
itchy_node = DCell::Node["itchy"]
itchy = itchy_node[:itchy]

Expand Down
16 changes: 2 additions & 14 deletions lib/dcell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
require 'dcell/server'
require 'dcell/info_service'

require 'dcell/registries/redis_adapter'

require 'dcell/celluloid_ext'

# Distributed Celluloid
Expand Down Expand Up @@ -50,23 +48,13 @@ def setup(options = {})
@config_lock.synchronize do
@configuration = {
'addr' => "tcp://127.0.0.1:*",
'registry' => {'adapter' => 'redis', 'server' => 'localhost'},
'heartbeat_rate' => 5,
'heartbeat_timeout' => 10,
}.merge(options)

registry_adapter = @configuration['registry'][:adapter] || @configuration['registry']['adapter']
raise ArgumentError, "no registry adapter given in config" unless registry_adapter

registry_class_name = registry_adapter.split("_").map(&:capitalize).join << "Adapter"

begin
registry_class = DCell::Registry.const_get registry_class_name
rescue NameError
raise ArgumentError, "invalid registry adapter: #{registry_adapter}"
end
@registry = @configuration['registry']
raise ArgumentError, "no registry adapter given in config" unless @registry

@registry = registry_class.new(@configuration['registry'])
@configuration['id'] ||= generate_node_id
@me = Node.new @configuration['id'], nil
ObjectSpace.define_finalizer(me, proc {Directory.remove @configuration['id']})
Expand Down
12 changes: 7 additions & 5 deletions spec/dcell/dcell_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
describe DCell do
it "raises exception on unknown registry provider" do
expect {DCellMock.setup :registry => {:adapter => nil}}.to raise_error(ArgumentError, "no registry adapter given in config")
expect {DCellMock.setup :registry => {:adapter => 'invalid'}}.to raise_error(ArgumentError, "invalid registry adapter: invalid")
expect {DCellMock.setup}.to raise_error(ArgumentError, "no registry adapter given in config")
end

it "uses unique method of registry to generate node ID" do
DCellMock.setup :registry => {:adapter => 'dummy', :seed => Math::PI}
registry = DCell::Registry::DummyAdapter.new :seed => Math::PI
DCellMock.setup :registry => registry
DCellMock.id.should == Math::PI
end

it "accepts node ID as optional setup parameter" do
DCellMock.setup :id => Math::E, :registry => {:adapter => 'dummy'}
registry = DCell::Registry::DummyAdapter.new({})
DCellMock.setup :id => Math::E, :registry => registry
DCellMock.id.should == Math::E
end

it "tries to generate node ID if registry does not define :unique method and no explicit setup parameter given" do
DCellMock.setup :registry => {:adapter => 'noop'}
registry = DCell::Registry::NoopAdapter.new({})
DCellMock.setup :registry => registry
DCellMock.id.should_not == nil
end
end
2 changes: 2 additions & 0 deletions spec/dcell/registries/redis_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'dcell/registries/redis_adapter'

describe DCell::Registry::RedisAdapter, :pending => TEST_ADEPTER != 'redis' && "no redis" do
subject { DCell::Registry::RedisAdapter.new TEST_DB[:redis] }
it_behaves_like "a DCell registry"
Expand Down
14 changes: 10 additions & 4 deletions spec/options/99-options.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
def test_options
options = {}
adapter = TEST_ADEPTER
if adapter
options[:registry] = {:adapter => adapter}
options[:registry].merge! TEST_DB[adapter.to_sym]
case TEST_ADEPTER
when 'redis'
registry = DCell::Registry::RedisAdapter.new TEST_DB[:redis]
when 'mongodb'
registry = DCell::Registry::MongodbAdapter.new TEST_DB[:mongodb]
when 'cassandra'
registry = DCell::Registry::CassandraAdapter.new TEST_DB[:cassandra]
when 'zk'
registry = DCell::Registry::ZkAdapter.new TEST_DB[:zk]
end
options[:registry] = registry
options['heartbeat_rate'] = 1
options['heartbeat_timeout'] = 2
options
Expand Down

0 comments on commit 74572da

Please sign in to comment.