Skip to content

Commit

Permalink
Merge pull request #831 from mr-salty/auto_start_service
Browse files Browse the repository at this point in the history
Enhancements to Windows service install/uninstall.
  • Loading branch information
nurse committed Mar 15, 2016
2 parents 9990222 + cf92402 commit 6c9092d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/fluent/command/fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
opts[:regwinsvc] = s
}

op.on('--[no-]reg-winsvc-auto-start', "Automatically start the Windows Service at boot. (only effective with '--reg-winsvc i') (Windows only)") {|s|
opts[:regwinsvcautostart] = s
}

op.on('--reg-winsvc-fluentdopt OPTION', "specify fluentd option paramters for Windows Service. (Windows only)") {|s|
opts[:fluentdopt] = s
}
Expand Down Expand Up @@ -192,6 +196,8 @@
exit 0
end

early_exit = false
start_service = false
if winsvcinstmode = opts[:regwinsvc]
FLUENTD_WINSVC_NAME="fluentdwinsvc"
FLUENTD_WINSVC_DISPLAYNAME="Fluentd Windows Service"
Expand All @@ -207,33 +213,51 @@
ruby_path = "\0" * 256
GetModuleFileName.call(0,ruby_path,256)
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
start_type = Service::DEMAND_START
if opts[:regwinsvcautostart]
start_type = Service::AUTO_START
start_service = true
end

Service.create(
service_name: FLUENTD_WINSVC_NAME,
host: nil,
service_type: Service::WIN32_OWN_PROCESS,
description: FLUENTD_WINSVC_DESC,
start_type: Service::DEMAND_START,
start_type: start_type,
error_control: Service::ERROR_NORMAL,
binary_path_name: ruby_path+" -C "+binary_path+" winsvc.rb",
load_order_group: "",
dependencies: [""],
display_name: FLUENTD_WINSVC_DISPLAYNAME
)
when 'u'
if Service.status(FLUENTD_WINSVC_NAME).current_state != 'stopped'
begin
Service.stop(FLUENTD_WINSVC_NAME)
rescue => ex
puts "Warning: Failed to stop service: ", ex
end
end
Service.delete(FLUENTD_WINSVC_NAME)
else
# none
end
exit 0
early_exit = true
end

if fluentdopt = opts[:fluentdopt]
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\fluentdwinsvc", Win32::Registry::KEY_ALL_ACCESS) do |reg|
reg['fluentdopt', Win32::Registry::REG_SZ] = fluentdopt
end
exit 0
early_exit = true
end

if start_service
Service.start(FLUENTD_WINSVC_NAME)
end

exit 0 if early_exit

require 'fluent/supervisor'
Fluent::Supervisor.new(opts).start

0 comments on commit 6c9092d

Please sign in to comment.