[Groonga-commit] droonga/droonga-engine at d33f0e0 [master] droonga-engine: make command as a library

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Apr 21 15:10:05 JST 2014


Kouhei Sutou	2014-04-21 15:10:05 +0900 (Mon, 21 Apr 2014)

  New Revision: d33f0e02f59d1b809ad1cc80e9673dcc2cf3a4c1
  https://github.com/droonga/droonga-engine/commit/d33f0e02f59d1b809ad1cc80e9673dcc2cf3a4c1

  Message:
    droonga-engine: make command as a library

  Added files:
    lib/droonga/engine/command/droonga_engine.rb
  Modified files:
    bin/droonga-engine

  Modified: bin/droonga-engine (+2 -93)
===================================================================
--- bin/droonga-engine    2014-04-17 18:55:11 +0900 (cf6efd8)
+++ bin/droonga-engine    2014-04-21 15:10:05 +0900 (c39bb7e)
@@ -15,97 +15,6 @@
 # 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 "socket"
-require "ostruct"
-require "optparse"
+require "droonga/engine/command/droonga_engine"
 
-require "droonga/engine"
-require "droonga/event_loop"
-require "droonga/fluent_message_receiver"
-require "droonga/plugin_loader"
-
-options = OpenStruct.new
-options.host = Droonga::FluentMessageReceiver::DEFAULT_HOST
-options.port = Droonga::FluentMessageReceiver::DEFAULT_PORT
-options.tag = "droonga"
-options.log_level = Droonga::Logger::Level.default_label
-
-parser = OptionParser.new
-parser.on("--host=HOST",
-          "The host name of the Droonga engine",
-          "(#{options.host})") do |host|
-  options.host = host
-end
-parser.on("--port=PORT", Integer,
-          "The port number of the Droonga engine",
-          "(#{options.port})") do |port|
-  options.port = port
-end
-parser.on("--tag=TAG",
-          "The tag of the Droonga engine",
-          "(#{options.tag})") do |tag|
-  options.tag = tag
-end
-levels = Droonga::Logger::Level::LABELS
-levels_label = levels.join(",")
-parser.on("--log-level=LEVEL", levels,
-          "The log level of the Droonga engine",
-          "[#{levels_label}]",
-          "(#{options.log_level})") do |level|
-  options.log_level = level
-end
-parser.parse!(ARGV)
-
-ENV["DROOGNA_LOG_LEVEL"] = options.log_level
-
-Droonga::PluginLoader.load_all
-
-raw_loop = Coolio::Loop.default
-loop = Droonga::EventLoop.new(raw_loop)
-
-name = "#{options.host}:#{options.port}/#{options.tag}"
-engine = Droonga::Engine.new(loop, name)
-engine.start
-
-receiver_options = {
-  :host => options.host,
-  :port => options.port,
-}
-on_message = lambda do |tag, time, record|
-  prefix, type, *arguments = tag.split(/\./)
-  if type.nil? or type.empty? or type == "message"
-    message = record
-  else
-    message = {
-      "type" => type,
-      "arguments" => arguments,
-      "body" => record
-    }
-  end
-  reply_to = message["replyTo"]
-  if reply_to.is_a? String
-    message["replyTo"] = {
-      "type" => "#{message["type"]}.result",
-      "to" => reply_to
-    }
-  end
-  message
-
-  engine.process(message)
-end
-receiver = Droonga::FluentMessageReceiver.new(loop,
-                                              receiver_options,
-                                              &on_message)
-receiver.start
-
-trap(:INT) do
-  raw_loop.stop
-end
-trap(:TERM) do
-  raw_loop.stop
-end
-
-raw_loop.run
-
-receiver.shutdown
-engine.shutdown
+exit(Droonga::Engine::Command::DroongaEngine.run(ARGV))

  Added: lib/droonga/engine/command/droonga_engine.rb (+145 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/engine/command/droonga_engine.rb    2014-04-21 15:10:05 +0900 (c69b9e5)
@@ -0,0 +1,145 @@
+# 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
+# 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 "optparse"
+
+require "droonga/engine"
+require "droonga/event_loop"
+require "droonga/fluent_message_receiver"
+require "droonga/plugin_loader"
+
+module Droonga
+  class Engine
+    module Command
+      class DroongaEngine
+        class << self
+          def run(command_line_arguments)
+            new.run(command_line_arguments)
+          end
+        end
+
+        def initialize
+          @host = FluentMessageReceiver::DEFAULT_HOST
+          @port = FluentMessageReceiver::DEFAULT_PORT
+          @tag = "droonga"
+          @log_level = Logger::Level.default_label
+        end
+
+        def run(command_line_arguments)
+          parse_command_line_arguments!(command_line_arguments)
+          Droonga::PluginLoader.load_all
+
+          raw_loop = Coolio::Loop.default
+          loop = EventLoop.new(raw_loop)
+
+          run_engine(loop) do |engine|
+            run_receiver(loop, engine) do |receiver|
+              trap(:INT) do
+                raw_loop.stop
+              end
+              trap(:TERM) do
+                raw_loop.stop
+              end
+              raw_loop.run
+            end
+          end
+        end
+
+        private
+        def parse_command_line_arguments!(command_line_arguments)
+          parser = OptionParser.new
+          parser.on("--host=HOST",
+                    "The host name of the Droonga engine",
+                    "(#{@host})") do |host|
+            @host = host
+          end
+          parser.on("--port=PORT", Integer,
+                    "The port number of the Droonga engine",
+                    "(#{@port})") do |port|
+            @port = port
+          end
+          parser.on("--tag=TAG",
+                    "The tag of the Droonga engine",
+                    "(#{@tag})") do |tag|
+            @tag = tag
+          end
+          levels = Droonga::Logger::Level::LABELS
+          levels_label = levels.join(",")
+          parser.on("--log-level=LEVEL", levels,
+                    "The log level of the Droonga engine",
+                    "[#{levels_label}]",
+                    "(#{@log_level})") do |level|
+            @log_level = level
+          end
+          parser.parse!(command_line_arguments)
+
+          ENV["DROOGNA_LOG_LEVEL"] = @log_level
+        end
+
+        def run_engine(loop)
+          engine = Droonga::Engine.new(loop, engine_name)
+          begin
+            engine.start
+            yield(engine)
+          ensure
+            engine.shutdown
+          end
+        end
+
+        def engine_name
+          "#{@host}:#{@port}/#{@tag}"
+        end
+
+        def run_receiver(loop, engine)
+          receiver_options = {
+            :host => options.host,
+            :port => options.port,
+          }
+          on_message = lambda do |tag, time, record|
+            prefix, type, *arguments = tag.split(/\./)
+            if type.nil? or type.empty? or type == "message"
+              message = record
+            else
+              message = {
+                "type" => type,
+                "arguments" => arguments,
+                "body" => record
+              }
+            end
+            reply_to = message["replyTo"]
+            if reply_to.is_a? String
+              message["replyTo"] = {
+                "type" => "#{message["type"]}.result",
+                "to" => reply_to
+              }
+            end
+            message
+
+            engine.process(message)
+          end
+          receiver = Droonga::FluentMessageReceiver.new(loop,
+                                                        receiver_options,
+                                                        &on_message)
+          begin
+            receiver.start
+            yield(receiver)
+          ensure
+            receiver.shutdown
+          end
+        end
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index