Skip to content

Commit

Permalink
Migrate Handler to new plugin style
Browse files Browse the repository at this point in the history
Note: This is not stable API yet.

Before:

    # lib/droonga/plugin/handler/my_handler.rb

    require "droonga/handler_plugin"

    module Droonga
      class MyHandler < Droonga::HandlerPlugin
        repository.register("my-handler", self)

        command :my_command
        def my_command(message, messenger)
        end
      end
    end

After:

    # lib/droonga/plugins/my_plugin.rb

    require "droonga/plugin"

    module Droonga
      module Plugins
        module MyPlugin
          Plugin.registry.register("my-plugin", self)

          class Handler < Droonga::Handler
            message.type = "my-command"

            def handle(message, messenger)
            end
          end
        end
      end
    end
  • Loading branch information
kou committed Feb 7, 2014
1 parent b8308e4 commit 18b2cb2
Show file tree
Hide file tree
Showing 31 changed files with 819 additions and 708 deletions.
5 changes: 2 additions & 3 deletions lib/droonga/catalog/base.rb
Expand Up @@ -54,8 +54,7 @@ def get_partitions(name)
results = {}
@data["datasets"].each do |key, dataset|
workers = dataset["workers"]
handler = dataset["handler"] || {}
plugins = handler["plugins"] || dataset["plugins"]
plugins = dataset["plugins"]
dataset["ring"].each do |key, part|
part["partitions"].each do |range, partitions|
partitions.each do |partition|
Expand All @@ -65,7 +64,7 @@ def get_partitions(name)
options = {
:database => path,
:n_workers => workers,
:handlers => plugins
:plugins => plugins
}
results[partition] = options
end
Expand Down
37 changes: 21 additions & 16 deletions lib/droonga/plugin/handler/search.rb → lib/droonga/handler.rb
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Droonga Project
# Copyright (C) 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
Expand All @@ -15,23 +13,30 @@
# 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/handler_plugin"
require "droonga/searcher"
require "droonga/pluggable"
require "droonga/plugin/metadata/input_message"
require "droonga/plugin/metadata/handler_action"

module Droonga
class SearchHandler < Droonga::HandlerPlugin
repository.register("search", self)
class Handler
extend Pluggable

class << self
def message
Plugin::Metadata::InputMessage.new(self)
end

command :search
def search(message, messenger)
searcher = Droonga::Searcher.new(@context)
values = {}
request = message.request
raise Droonga::Searcher::NoQuery.new unless request
searcher.search(request["queries"]).each do |output, value|
values[output] = value
def action
Plugin::Metadata::HandlerAction.new(self)
end
messenger.emit(values)
end

def initialize(name, context)
@name = name
@context = context
end

def handle(message, messenger)
end
end
end
41 changes: 23 additions & 18 deletions lib/droonga/handler_runner.rb
Expand Up @@ -18,15 +18,10 @@
require "droonga/forwarder"
require "droonga/handler_message"
require "droonga/handler_messenger"
require "droonga/legacy_pluggable"
require "droonga/handler_plugin"
require "droonga/handler"

module Droonga
class HandlerRunner
include LegacyPluggable

attr_reader :context, :name

def initialize(loop, options={})
@loop = loop
@options = options
Expand All @@ -43,7 +38,6 @@ def start

def shutdown
$log.trace("#{log_tag}: shutdown: start")
super
@forwarder.shutdown
if @database
@database.close
Expand All @@ -54,20 +48,24 @@ def shutdown
end

def prefer_synchronous?(command)
find_plugin(command).prefer_synchronous?(command)
find_handler_class(command).action.synchronous?
end

def processable?(command)
not find_handler_class(command).nil?
end

def process(message)
$log.trace("#{log_tag}: process: start")
command = message["type"]
plugin = find_plugin(command)
if plugin.nil?
$log.trace("#{log_tag}: process: done: no plugin: <#{command}>")
handler_class = find_handler_class(command)
if handler_class.nil?
$log.trace("#{log_tag}: process: done: no handler: <#{command}>")
return
end
process_command(plugin, command, message)
process_command(handler_class, command, message)
$log.trace("#{log_tag}: process: done: <#{command}>",
:plugin => plugin.class)
:handler => handler_class)
end

private
Expand All @@ -76,20 +74,27 @@ def prepare
@context = Groonga::Context.new
@database = @context.open_database(@database_name)
end
load_plugins(@options[:handlers] || [])
@handler_classes = Handler.find_sub_classes(@options[:plugins] || [])
@forwarder = Forwarder.new(@loop)
end

def instantiate_plugin(name)
HandlerPlugin.repository.instantiate(name, self)
def find_handler_class(command)
@handler_classes.find do |handler_class|
handler_class.message.type == command
end
end

def process_command(plugin, command, raw_message)
def process_command(handler_class, command, raw_message)
handler_message = HandlerMessage.new(raw_message)
handler_message.validate

messenger = HandlerMessenger.new(@forwarder, handler_message, @options)
plugin.process(command, handler_message, messenger)
handler = handler_class.new(@name, @context)
begin
handler.handle(handler_message, messenger)
rescue MessageProcessingError => error
messenger.error(error.status_code, error.response_body)
end
end

def log_tag
Expand Down
1 change: 1 addition & 0 deletions lib/droonga/plugin.rb
Expand Up @@ -15,6 +15,7 @@

require "droonga/plugin_registry"
require "droonga/adapter"
require "droonga/handler"

module Droonga
module Plugin
Expand Down
109 changes: 0 additions & 109 deletions lib/droonga/plugin/handler/add.rb

This file was deleted.

75 changes: 0 additions & 75 deletions lib/droonga/plugin/handler/forward.rb

This file was deleted.

0 comments on commit 18b2cb2

Please sign in to comment.