Skip to content

Commit

Permalink
Make collector shutdown timeout configurable
Browse files Browse the repository at this point in the history
`collector_shutdown_timeout: "10s"`
  • Loading branch information
mpfz0r committed Jan 9, 2023
1 parent e9fc465 commit 4aab929
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cfgfile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type SidecarConfig struct {
CollectorValidationTimeoutString string `config:"collector_validation_timeout"`
CollectorValidationTimeout time.Duration // set from CollectorValidationTimeoutString
CollectorConfigurationDirectory string `config:"collector_configuration_directory"`
CollectorShutdownTimeoutString string `config:"collector_shutdown_timeout"`
CollectorShutdownTimeout time.Duration // set from CollectorShutdownTimeoutString
LogRotateMaxFileSizeString string `config:"log_rotate_max_file_size"`
LogRotateMaxFileSize int64 // set from LogRotateMaxFileSizeString
LogRotateKeepFiles int `config:"log_rotate_keep_files"`
Expand All @@ -54,6 +56,7 @@ log_path: "/var/log/graylog-sidecar"
log_rotate_max_file_size: "10MiB"
log_rotate_keep_files: 10
collector_validation_timeout: "1m"
collector_shutdown_timeout: "10s"
collector_configuration_directory: "/var/lib/graylog-sidecar/generated"
collector_binaries_accesslist:
- "/usr/bin/filebeat"
Expand Down
4 changes: 4 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (ctx *Ctx) LoadConfig(path *string) error {
if err != nil {
log.Fatal("Cannot parse validation timeout duration: ", err)
}
ctx.UserConfig.CollectorShutdownTimeout, err = time.ParseDuration(ctx.UserConfig.CollectorShutdownTimeoutString)
if err != nil {
log.Fatal("Cannot parse shutdown timeout duration: ", err)
}

// collector_configuration_directory
if ctx.UserConfig.CollectorConfigurationDirectory == "" {
Expand Down
4 changes: 2 additions & 2 deletions daemon/exec_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Setpgid(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

func KillProcess(r *ExecRunner, timeout time.Duration) {
func KillProcess(r *ExecRunner) {
pid := r.cmd.Process.Pid

if pid == -1 {
Expand All @@ -48,7 +48,7 @@ func KillProcess(r *ExecRunner, timeout time.Duration) {
log.Infof("[%s] Failed to SIGTERM process group %s", r.Name(), err)
}

limit := timeout.Milliseconds()
limit := r.context.UserConfig.CollectorShutdownTimeout.Milliseconds()
tick := 100 * time.Millisecond
for t := tick.Milliseconds(); r.Running() && t < limit; t += tick.Milliseconds() {
log.Debugf("[%s] Waiting for process group to finish (%vms / %vms)", r.Name(), t, limit)
Expand Down
2 changes: 1 addition & 1 deletion daemon/exec_helper_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import (
func Setpgid(cmd *exec.Cmd) {
// nop on windows
}
func KillProcess(r *ExecRunner, _ time.Duration) {
func KillProcess(r *ExecRunner) {
err := r.cmd.Process.Kill()
if err != nil {
log.Debugf("[%s] Failed to kill process %s", r.Name(), err)
Expand Down
2 changes: 1 addition & 1 deletion daemon/exec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (r *ExecRunner) stop() error {

log.Infof("[%s] Stopping", r.name)

KillProcess(r, 5*time.Second)
KillProcess(r)

if !r.Running() {
r.backend.SetStatus(backends.StatusStopped, "Stopped", "")
Expand Down
4 changes: 4 additions & 0 deletions sidecar-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ server_api_token: ""
# How long to wait for the config validation command.
#collector_validation_timeout: "1m"

# How long to wait for the collector to gracefully shutdown.
# After this timeout the sidecar tries to terminate the collector with SIGKILL
#collector_shutdown_timeout: "10s"

# Directory where the sidecar generates configurations for collectors.
#collector_configuration_directory: "/var/lib/graylog-sidecar/generated"

Expand Down

0 comments on commit 4aab929

Please sign in to comment.