lib/redis_ring/application.rb in redis_ring-0.0.2 vs lib/redis_ring/application.rb in redis_ring-0.1.0
- old
+ new
@@ -1,52 +1,52 @@
module RedisRing
class Application
- attr_reader :shards, :configuration, :process_manager
+ attr_reader :shards, :configuration, :process_manager, :zookeeper_observer, :master, :slave, :zookeeper_connection, :master_rpc, :http_client, :node_provider, :slave_rpc
- def initialize(configuration)
- @configuration = configuration
+ def initialize(config)
+ @configuration = config
@process_manager = ProcessManager.new
- @shards = {}
+ @http_client = HttpClient.new
+ @master_rpc = MasterRPC.new(http_client)
+ @slave_rpc = SlaveRPC.new(http_client)
+ @node_provider = NodeProvider.new(slave_rpc)
+ @zookeeper_connection = ZookeeperConnection.new(config.host_name,
+ config.base_port,
+ config.zookeeper_address)
+ @master = Master.new(zookeeper_connection, config.ring_size, node_provider)
+ @slave = Slave.new(configuration, master_rpc, process_manager)
+ @zookeeper_observer = ZookeeperObserver.new(zookeeper_connection, master, slave)
+ @web_interface_runner = WebInterfaceRunner.new(config.base_port, master, slave)
end
def start
self.stop
- @configuration.ring_size.times do |shard_number|
- shard_conf = ShardConfig.new(shard_number, configuration)
- @shards[shard_number] = Shard.new(shard_conf)
- end
+ @web_thread = @web_interface_runner.run
- @shards.each do |shard_no, shard|
- @process_manager.start_shard(shard)
- end
+ @zookeeper_connection.connect
+ @slave.node_id = @zookeeper_connection.current_node
- @process_manager.run
- end
+ @zookeeper_thread = @zookeeper_observer.run
+ @pm_thread = @process_manager.run
- def stop
- @process_manager.halt
-
- @shards.each do |shard_no, shard|
- @process_manager.stop_shard(shard)
+ [:INT, :TERM, :QUIT].each do |sig|
+ trap(sig) { self.stop }
end
-
- @shards = {}
end
- def shards_hash
- shards_hash = {}
- shards.each do |shard_no, shard|
- shards_hash[shard_no] = { :host => shard.host, :port => shard.port, :status => shard.status }
- end
-
- return { :count => configuration.ring_size, :shards => shards_hash }
+ def wait
+ @pm_thread.join if @pm_thread
+ @zookeeper_thread.join if @zookeeper_thread
+ @web_thread.join if @web_thread
end
- class << self
- attr_accessor :instance
+ def stop
+ @process_manager.halt
+ @zookeeper_observer.halt
+ @web_interface_runner.halt
end
end
end