Kouhei Sutou
null+****@clear*****
Sun Nov 24 23:08:35 JST 2013
Kouhei Sutou 2013-11-24 23:08:35 +0900 (Sun, 24 Nov 2013) New Revision: a0383f7d3e23efcd5bff18762c18d756e6287470 https://github.com/droonga/fluent-plugin-droonga/commit/a0383f7d3e23efcd5bff18762c18d756e6287470 Message: Share using plugin code Copied files: lib/droonga/pluggable.rb (from lib/droonga/adapter.rb) Modified files: lib/droonga/adapter.rb lib/droonga/collector.rb lib/droonga/distributor.rb lib/droonga/distributor_plugin.rb lib/droonga/handler.rb lib/fluent/plugin/out_droonga.rb Modified: lib/droonga/adapter.rb (+5 -31) =================================================================== --- lib/droonga/adapter.rb 2013-11-24 22:46:12 +0900 (6f11ec8) +++ lib/droonga/adapter.rb 2013-11-24 23:08:35 +0900 (74819c7) @@ -15,47 +15,21 @@ # 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/pluggable" require "droonga/adapter_plugin" module Droonga class Adapter + include Pluggable + def initialize(executor, options={}) @executor = executor load_plugins(options[:adapters] || []) end - def shutdown - $log.trace("#{log_tag}: shutdown: start") - @plugins.each do |plugin| - plugins.shutdown - end - $log.trace("#{log_tag}: shutdown: done") - end - - def processable?(command) - not find_plugin(command).nil? - end - - def process(command, body) - plugin = find_plugin(command) - $log.trace("#{log_tag}: process: start: <#{command}>", - :plugin => plugin.class) - plugin.process(command, body) - $log.trace("#{log_tag}: process: done: <#{command}>", - :plugin => plugin.class) - end - private - def load_plugins(names) - @plugins = names.collect do |name| - AdapterPlugin.repository.instantiate(name, @executor) - end - end - - def find_plugin(command) - @plugins.find do |plugin| - plugin.processable?(command) - end + def instantiate_plugin(name) + AdapterPlugin.repository.instantiate(name, @executor) end def log_tag Modified: lib/droonga/collector.rb (+8 -18) =================================================================== --- lib/droonga/collector.rb 2013-11-24 22:46:12 +0900 (d61db86) +++ lib/droonga/collector.rb 2013-11-24 23:08:35 +0900 (efaec31) @@ -15,10 +15,13 @@ # 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/pluggable" require "droonga/collector_plugin" module Droonga class Collector + include Pluggable + def initialize(id, dispatcher, components, tasks, inputs) @id = id @dispatcher = dispatcher @@ -26,7 +29,7 @@ module Droonga @tasks = tasks @n_dones = 0 @inputs = inputs - @plugins = load_plugins(["basic"]) # TODO: make customizable + load_plugins(["basic"]) # TODO: make customizable end def handle(name, value) @@ -94,25 +97,12 @@ module Droonga end private - def process(command, message) - plugin = find_plugin(command) - if plugin.nil? - raise "unknown collector plugin: <#{command}>: " + - "TODO: improve error hndling" - end - plugin.process(command, message) - end - - def load_plugins(names) - names.collect do |name| - CollectorPlugin.repository.instantiate(name, @dispatcher) - end + def instantiate_plugin(name) + CollectorPlugin.repository.instantiate(name, @dispatcher) end - def find_plugin(command) - @plugins.find do |plugin| - plugin.processable?(command) - end + def log_tag + "collector" end end end Modified: lib/droonga/distributor.rb (+6 -29) =================================================================== --- lib/droonga/distributor.rb 2013-11-24 22:46:12 +0900 (9d25398) +++ lib/droonga/distributor.rb 2013-11-24 23:08:35 +0900 (bb79c0d) @@ -15,10 +15,13 @@ # 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/pluggable" require "droonga/distributor_plugin" module Droonga class Distributor + include Pluggable + def initialize(dispatcher, options={}) @dispatcher = dispatcher @plugins = [] @@ -27,22 +30,9 @@ module Droonga load_plugins(options[:distributors] || ["search", "crud", "groonga", "watch"]) end - def shutdown - $log.trace("#{log_tag}: shutdown: start") - @plugins.each do |plugin| - plugin.shutdown - end - $log.trace("#{log_tag}: shutdown: done") - end - def distribute(envelope) command = envelope["type"] - plugin = find_plugin(command) - if plugin.nil? - raise "unknown distributor plugin: <#{command}>: " + - "TODO: improve error hndling" - end - plugin.process(envelope) + process(command, envelope) end def post(message) @@ -50,21 +40,8 @@ module Droonga end private - def load_plugins(plugin_names) - plugin_names.each do |plugin_name| - add_plugin(plugin_name) - end - end - - def add_plugin(name) - plugin = DistributorPlugin.repository.instantiate(name, self) - @plugins << plugin - end - - def find_plugin(command) - @plugins.find do |plugin| - plugin.processable?(command) - end + def instantiate_plugin(name) + DistributorPlugin.repository.instantiate(name, self) end def log_tag Modified: lib/droonga/distributor_plugin.rb (+0 -5) =================================================================== --- lib/droonga/distributor_plugin.rb 2013-11-24 22:46:12 +0900 (66e44c5) +++ lib/droonga/distributor_plugin.rb 2013-11-24 23:08:35 +0900 (37ab744) @@ -31,11 +31,6 @@ module Droonga @distributor.post(message) end - def process(envelope, *arguments) - command = envelope["type"] - super(command, envelope, *arguments) - end - def scatter_all(envelope, key) message = [{ "command"=> envelope["type"], Modified: lib/droonga/handler.rb (+7 -31) =================================================================== --- lib/droonga/handler.rb 2013-11-24 22:46:12 +0900 (b7315fe) +++ lib/droonga/handler.rb 2013-11-24 23:08:35 +0900 (1b895ac) @@ -20,31 +20,27 @@ require "fluent/logger/fluent_logger" require "groonga" require "droonga/job_queue" +require "droonga/pluggable" require "droonga/handler_plugin" -require "droonga/plugin_loader" module Droonga class Handler + include Pluggable + attr_reader :context, :envelope, :name def initialize(options={}) - @plugins = [] @outputs = {} @options = options @name = options[:name] @database_name = options[:database] @queue_name = options[:queue_name] || "DroongaQueue" - @plugin_names = options[:handlers] || [] -# load_plugins - Droonga::PluginLoader.load_all prepare end def shutdown $log.trace("#{log_tag}: shutdown: start") - @plugins.each do |plugin| - plugins.shutdown - end + super @outputs.each do |dest, output| output[:logger].close if output[:logger] end @@ -60,11 +56,6 @@ module Droonga $log.trace("#{log_tag}: shutdown: done") end - def add_plugin(name) - plugin = HandlerPlugin.repository.instantiate(name, self) - @plugins << plugin - end - def execute_one $log.trace("#{log_tag}: execute_one: start") message = @job_queue.pull_message @@ -76,10 +67,6 @@ module Droonga $log.trace("#{log_tag}: execute_one: done") end - def processable?(command) - not find_plugin(command).nil? - end - def prefer_synchronous?(command) find_plugin(command).prefer_synchronous?(command) end @@ -243,28 +230,17 @@ module Droonga [envelope["body"], envelope["type"], envelope["arguments"]] end - def load_plugins - @plugin_names.each do |plugin_name| - loader = Droonga::PluginLoader.new("handler", plugin_name) - loader.load - end - end - def prepare if @database_name && !@database_name.empty? @context = Groonga::Context.new @database =****@conte*****_database(@database_name) @job_queue = JobQueue.open(@database_name, @queue_name) end - @plugin_names.each do |plugin_name| - add_plugin(plugin_name) - end + load_plugins(@options[:handlers] || []) end - def find_plugin(command) - @plugins.find do |plugin| - plugin.processable?(command) - end + def instantiate_plugin(name) + HandlerPlugin.repository.instantiate(name, self) end def get_output(host, port, params) Copied: lib/droonga/pluggable.rb (+15 -18) 72% =================================================================== --- lib/droonga/adapter.rb 2013-11-24 22:46:12 +0900 (6f11ec8) +++ lib/droonga/pluggable.rb 2013-11-24 23:08:35 +0900 (3680a35) @@ -15,32 +15,29 @@ # 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/adapter_plugin" - module Droonga - class Adapter - def initialize(executor, options={}) - @executor = executor - load_plugins(options[:adapters] || []) - end - + module Pluggable def shutdown - $log.trace("#{log_tag}: shutdown: start") + $log.trace("#{log_tag}: shutdown: plugin: start") @plugins.each do |plugin| - plugins.shutdown + plugin.shutdown end - $log.trace("#{log_tag}: shutdown: done") + $log.trace("#{log_tag}: shutdown: plugin: done") end def processable?(command) not find_plugin(command).nil? end - def process(command, body) + def process(command, *arguments) plugin = find_plugin(command) $log.trace("#{log_tag}: process: start: <#{command}>", :plugin => plugin.class) - plugin.process(command, body) + if plugin.nil? + raise "unknown plugin: <#{command}>: " + + "TODO: improve error handling" + end + plugin.process(command, *arguments) $log.trace("#{log_tag}: process: done: <#{command}>", :plugin => plugin.class) end @@ -48,7 +45,11 @@ module Droonga private def load_plugins(names) @plugins = names.collect do |name| - AdapterPlugin.repository.instantiate(name, @executor) + plugin = instantiate_plugin(name) + if plugin.nil? + raise "unknown plugin: <#{name}>: TODO: improve error handling" + end + plugin end end @@ -57,9 +58,5 @@ module Droonga plugin.processable?(command) end end - - def log_tag - "adapter" - end end end Modified: lib/fluent/plugin/out_droonga.rb (+2 -0) =================================================================== --- lib/fluent/plugin/out_droonga.rb 2013-11-24 22:46:12 +0900 (39285bd) +++ lib/fluent/plugin/out_droonga.rb 2013-11-24 23:08:35 +0900 (58fd5a6) @@ -16,6 +16,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA require "droonga/engine" +require "droonga/plugin_loader" module Fluent class DroongaOutput < Output @@ -28,6 +29,7 @@ module Fluent def start super + Droonga::PluginLoader.load_all @engine = Droonga::Engine.new(:name => @name) @engine.start end -------------- next part -------------- HTML����������������������������...Download