Skip to content

Commit

Permalink
Merge pull request #1155 from unak/winsvc-support
Browse files Browse the repository at this point in the history
Handle Windows event correctly when using winsvc.rb
  • Loading branch information
tagomoris authored Aug 15, 2016
2 parents f9d1715 + 43a518f commit 340ea4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 25 additions & 0 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

if Fluent.windows?
require 'windows/library'
require 'windows/synchronize'
require 'windows/system_info'
include Windows::Library
include Windows::Synchronize
include Windows::SystemInfo
require 'win32/ipc'
require 'win32/event'
Expand All @@ -48,6 +50,11 @@ def before_run
end
install_supervisor_signal_handlers

if config[:signame]
@signame = config[:signame]
install_windows_event_handler
end

socket_manager_path = ServerEngine::SocketManager::Server.generate_path
ServerEngine::SocketManager::Server.open(socket_manager_path)
ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
Expand Down Expand Up @@ -135,6 +142,21 @@ def install_supervisor_signal_handlers
end unless Fluent.windows?
end

def install_windows_event_handler
Thread.new do
ev = Win32::Event.new(@signame)
begin
ev.reset
until WaitForSingleObject(ev.handle, 1000) == WAIT_OBJECT_0
end
kill_worker
stop(true)
ensure
ev.close
end
end
end

def supervisor_sighup_handler
kill_worker
end
Expand Down Expand Up @@ -224,6 +246,7 @@ def self.load_config(path, params = {})
pid_path = params['daemonize']
daemonize = !!params['daemonize']
main_cmd = params['main_cmd']
signame = params['signame']

se_config = {
worker_type: 'spawn',
Expand Down Expand Up @@ -253,6 +276,7 @@ def self.load_config(path, params = {})
JSON.dump(params)],
fluentd_conf: fluentd_conf,
main_cmd: main_cmd,
signame: signame,
}
if daemonize
se_config[:pid_path] = pid_path
Expand Down Expand Up @@ -490,6 +514,7 @@ def supervise
params['chgroup'] = @chgroup
params['use_v1_config'] = @use_v1_config
params['suppress_repeated_stacktrace'] = @suppress_repeated_stacktrace
params['signame'] = @signame

se = ServerEngine.create(ServerModule, WorkerModule){
Fluent::Supervisor.load_config(@config_path, params)
Expand Down
3 changes: 1 addition & 2 deletions lib/fluent/winsvc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def read_fluentdopt
require 'win32/Registry'
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\fluentdwinsvc") do |reg|
reg.read("fluentdopt")[1]
reg.read("fluentdopt")[1] rescue ""
end
end

Expand All @@ -47,7 +47,6 @@ class FluentdService < Daemon
@pid = 0

def service_main
opt = read_fluentdopt
@pid = service_main_start
while running?
sleep 10
Expand Down

0 comments on commit 340ea4f

Please sign in to comment.