feat: Add autorestart and restartdelay flags to Windows service (#10559)

This commit is contained in:
R290 2022-02-03 17:23:52 +01:00 committed by GitHub
parent 4ae8c60178
commit 48682ff5f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 1 deletions

View File

@ -96,6 +96,14 @@ var fServiceName = flag.String("service-name", "telegraf",
var fServiceDisplayName = flag.String("service-display-name", "Telegraf Data Collector Service",
"service display name (windows only)")
//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fServiceAutoRestart = flag.Bool("service-auto-restart", false,
"auto restart service on failure (windows only)")
//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fServiceRestartDelay = flag.String("service-restart-delay", "5m",
"delay before service auto restart, default is 5m (windows only)")
//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows
var fRunAsConsole = flag.Bool("console", false,
"run as console application (windows only)")

View File

@ -94,6 +94,10 @@ func runAsWindowsService(inputFilters, outputFilters []string) {
//set servicename to service cmd line, to have a custom name after relaunch as a service
svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName)
if *fServiceAutoRestart {
svcConfig.Option = service.KeyValue{"OnFailure": "restart", "OnFailureDelayDuration": *fServiceRestartDelay}
}
err := service.Control(s, *fService)
if err != nil {
log.Fatal("E! " + err.Error())

View File

@ -61,6 +61,10 @@ on a single system, you can install the service with the `--service-name` and
> C:\"Program Files"\Telegraf\telegraf.exe --service install --service-name telegraf-2 --service-display-name "Telegraf 2"
```
## Auto restart and restart delay
By default the service will not automatically restart on failure. Providing the `--service-auto-restart` flag during installation will always restart the service with a default delay of 5 minutes. To modify this to for example 3 minutes, provide the additional flag `--service-restart-delay 3m`. The delay can be any valid `time.Duration` string.
## Troubleshooting
When Telegraf runs as a Windows service, Telegraf logs messages to Windows events log before configuration file with logging settings is loaded.

View File

@ -44,6 +44,8 @@ The commands & flags are:
--service <service> operate on the service (windows only)
--service-name service name (windows only)
--service-display-name service display name (windows only)
--service-auto-restart auto restart service on failure (windows only)
--service-restart-delay delay before service auto restart, default is 5m (windows only)
Examples:
@ -73,4 +75,7 @@ Examples:
# install telegraf service with custom name
telegraf --service install --service-name=my-telegraf --service-display-name="My Telegraf"
`
# install telegraf service with auto restart and restart delay of 3 minutes
telegraf --service install --service-auto-restart --service-restart-delay 3m
`