Navigation Menu

Skip to content

Commit

Permalink
droonga-engine: make command as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 21, 2014
1 parent d2210e7 commit d33f0e0
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 93 deletions.
95 changes: 2 additions & 93 deletions bin/droonga-engine
Expand Up @@ -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))
145 changes: 145 additions & 0 deletions lib/droonga/engine/command/droonga_engine.rb
@@ -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

0 comments on commit d33f0e0

Please sign in to comment.