Navigation Menu

Skip to content

Commit

Permalink
Implement "status" command
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jun 24, 2014
1 parent 1469901 commit d36a989
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/droonga/dispatcher.rb
Expand Up @@ -51,7 +51,7 @@ def initialize(type, dataset)
def initialize(engine_state, catalog)
@engine_state = engine_state
@catalog = catalog
@live_nodes = catalog.all_nodes
@live_nodes = all_nodes
@adapter_runners = create_adapter_runners
@farm = Farm.new(@engine_state.name, @catalog, @engine_state.loop,
:dispatcher => self)
Expand Down Expand Up @@ -210,6 +210,10 @@ def local?(route)
@engine_state.local_route?(route)
end

def all_nodes
@catalog.all_nodes
end

private
def farm_path(route)
@engine_state.farm_path(route)
Expand Down
2 changes: 1 addition & 1 deletion lib/droonga/handler_messenger.rb
Expand Up @@ -18,7 +18,7 @@

module Droonga
class HandlerMessenger
attr_reader :database_name
attr_reader :database_name, :dispatcher

def initialize(forwarder, message, options={})
@forwarder = forwarder
Expand Down
51 changes: 51 additions & 0 deletions lib/droonga/plugins/status.rb
@@ -0,0 +1,51 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/plugin"

module Droonga
module Plugins
module Status
extend Plugin
register("status")

class Handler < Droonga::Handler
action.synchronous = true

def handle(message)
dispatcher = @messenger.dispatcher

live_nodes = dispatcher.live_nodes
nodes = {}
dispatcher.all_nodes.collect do |identifier|
nodes[identifier] = {
"live" => live_nodes.include?(identifier),
}
end

{
"nodes" => nodes,
}
end
end

define_single_step do |step|
step.name = "status"
step.handler = Handler
step.collector = Collectors::Or
end
end
end
end
3 changes: 3 additions & 0 deletions lib/droonga/test/stub_handler_messenger.rb
Expand Up @@ -17,9 +17,12 @@ module Droonga
module Test
class StubHandlerMessenger
attr_reader :values, :messages
attr_accessor :dispatcher

def initialize
@values = []
@messages = []
@dispatcher = nil
end

def emit(value)
Expand Down
80 changes: 80 additions & 0 deletions test/unit/plugins/test_status.rb
@@ -0,0 +1,80 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/plugins/status"

class StatusHandlerTest < Test::Unit::TestCase
def setup
setup_handler
end

def teardown
teardown_handler
end

private
def setup_handler
@worker = StubWorker.new
@messenger = Droonga::Test::StubHandlerMessenger.new
@dispatcher = StubDispatcher.new
@messenger.dispatcher = @dispatcher
@loop = nil
@handler = Droonga::Plugins::Status::Handler.new("name",
@worker.context,
@messenger,
@loop)
end

def teardown_handler
@handler = nil
end

def process(request)
message = Droonga::Test::StubHandlerMessage.new(request)
@handler.handle(message)
end

class StubDispatcher
def all_nodes
[
"127.0.0.1:10031/droonga",
"127.0.0.1:10032/droonga",
]
end

def live_nodes
[
"127.0.0.1:10031/droonga",
]
end
end

public
def test_request
request = {}
response = process(request)
status = {
"nodes" => {
"127.0.0.1:10031/droonga" => {
"live" => true,
},
"127.0.0.1:10032/droonga" => {
"live" => false,
},
},
}
assert_equal(status, response)
end
end

0 comments on commit d36a989

Please sign in to comment.