diff --git a/lib/fluent/winsvc.rb b/lib/fluent/winsvc.rb index 15de68d661..d47493b98e 100644 --- a/lib/fluent/winsvc.rb +++ b/lib/fluent/winsvc.rb @@ -63,10 +63,12 @@ def service_main end def service_stop - set_event(@service_name) - if @pid > 0 - Process.waitpid(@pid) + if @pid <= 0 + set_event(@service_name) + return end + + wait_supervisor_finished end def service_paramchange @@ -91,6 +93,29 @@ def set_event(event_name) ev.set ev.close end + + def repeat_set_event_several_times_until_success(event_name) + retries = 0 + max_retries = 10 + delay_sec = 3 + + begin + set_event(event_name) + rescue Errno::ENOENT + # This error occurs when the supervisor process has not yet created the event. + # If STOP is immediately executed, this state will occur. + # Retry `set_event' to wait for the initialization of the supervisor. + retries += 1 + raise if max_retries < retries + sleep(delay_sec) + retry + end + end + + def wait_supervisor_finished + repeat_set_event_several_times_until_success(@service_name) + Process.waitpid(@pid) + end end FluentdService.new(opts[:service_name]).mainloop