Navigation Menu

Skip to content

Commit

Permalink
droonga-engine: support re-opening log file by SIGHUP or SIGUSR1
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 5, 2015
1 parent fe14e5e commit 2d2ebe6
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions lib/droonga/command/droonga_engine.rb
Expand Up @@ -55,11 +55,7 @@ def run(command_line_arguments)
Process.daemon
end

open_log_file do
write_pid_file do
run_main_loop
end
end
run_main_loop
end

private
Expand Down Expand Up @@ -91,33 +87,6 @@ def run_main_loop
main_loop.run
end

def open_log_file
if @configuration.log_file_path
@configuration.log_file_path.open("a") do |file|
$stdout.reopen(file)
$stderr.reopen(file)
yield
end
else
yield
end
end

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

class Configuration
attr_reader :ready_notify_fd
def initialize
Expand Down Expand Up @@ -341,9 +310,40 @@ class MainLoop
def initialize(configuration)
@configuration = configuration
@loop = Coolio::Loop.default
@log_file = nil
end

def run
reopen_log_file
write_pid_file do
run_internal
end
end

private
def reopen_log_file
return if @configuration.log_file_path.nil?
@log_file = @configuration.log_file_path.open("a")
$stdout.reopen(@log_file)
$stderr.reopen(@log_file)
end

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

def run_internal
start_serf
@service_runner = run_service
setup_initial_on_ready
Expand All @@ -356,7 +356,6 @@ def run
@service_runner.success?
end

private
def setup_initial_on_ready
return if @configuration.ready_notify_fd.nil?
@service_runner.on_ready = lambda do
Expand Down Expand Up @@ -406,6 +405,7 @@ def stop_immediately

def restart_graceful
old_service_runner = @service_runner
reopen_log_file
@service_runner = run_service
@service_runner.on_ready = lambda do
@service_runner.on_failure = nil
Expand All @@ -419,6 +419,7 @@ def restart_graceful

def restart_immediately
old_service_runner = @service_runner
reopen_log_file
@service_runner = run_service
old_service_runner.stop_immediately
end
Expand Down

0 comments on commit 2d2ebe6

Please sign in to comment.