Navigation Menu

Skip to content

Commit

Permalink
droonga-engine: add --daemon and --pid-file options
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 28, 2014
1 parent a9de7ae commit 28e62ec
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions lib/droonga/engine/command/droonga_engine.rb
Expand Up @@ -16,6 +16,7 @@
require "optparse"
require "socket"
require "ipaddr"
require "fileutils"

require "droonga/engine"
require "droonga/event_loop"
Expand All @@ -34,11 +35,13 @@ class Configuration
DEFAULT_HOST = Socket.gethostname
DEFAULT_PORT = 10031

attr_reader :host, :port, :tag
attr_reader :host, :port, :tag, :pid_file
def initialize
@host = DEFAULT_HOST
@port = DEFAULT_PORT
@tag = "droonga"
@daemon = false
@pid_file = nil
end

def engine_name
Expand All @@ -54,6 +57,10 @@ def log_level
ENV["DROONGA_LOG_LEVEL"] || Logger::Level.default_label
end

def daemon?
@daemon
end

def to_command_line
[
"--host", @host,
Expand All @@ -66,6 +73,7 @@ def to_command_line
def add_command_line_options(parser)
add_connection_options(parser)
add_log_options(parser)
add_process_options(parser)
end

private
Expand Down Expand Up @@ -101,6 +109,19 @@ def add_log_options(parser)
ENV["DROONGA_LOG_LEVEL"] = level
end
end

def add_process_options(parser)
parser.separator("")
parser.separator("Process:")
parser.on("--daemon",
"Run as a daemon") do
@daemon = true
end
parser.on("--pid-file=FILE",
"Put PID to the FILE") do |file|
@pid_file = file
end
end
end

class Supervisor
Expand All @@ -123,7 +144,13 @@ def run(command_line_arguments)
@heartbeat_socket.bind(@configuration.host,
@configuration.port)

run_main_loop
if @configuration.daemon?
Process.daemon
end

write_pid_file do
run_main_loop
end
end

private
Expand Down Expand Up @@ -204,6 +231,21 @@ def run_main_loop

succeeded
end

def write_pid_file
if @configuration.pid_file
File.open(@configuration.pid_file, "w") do |file|
file.puts(Process.pid)
end
begin
yield
ensure
FileUtils.rm_f(@configuration.pid_file)
end
else
yield
end
end
end

class Service
Expand Down

0 comments on commit 28e62ec

Please sign in to comment.