diff options
| author | Paul Bütow <pbuetow@mimecast.com> | 2020-02-16 18:07:36 +0000 |
|---|---|---|
| committer | Paul Bütow <pbuetow@mimecast.com> | 2020-02-16 18:07:36 +0000 |
| commit | e0f4ccc46c8601f322640b72e100f973a837ef02 (patch) | |
| tree | 61a1fcf66daea222da19500b0b6ae60d1e89a5d9 /cmd/dserver/main.go | |
| parent | 6bca637513e065a33cadaccad97ada25eb7a6b00 (diff) | |
server kills subprocesses correctly on cancel
Diffstat (limited to 'cmd/dserver/main.go')
| -rw-r--r-- | cmd/dserver/main.go | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go index 03834f1..94f1a87 100644 --- a/cmd/dserver/main.go +++ b/cmd/dserver/main.go @@ -4,12 +4,13 @@ import ( "context" "flag" "os" + "os/signal" + "syscall" "time" "github.com/mimecast/dtail/internal/color" "github.com/mimecast/dtail/internal/config" "github.com/mimecast/dtail/internal/io/logger" - "github.com/mimecast/dtail/internal/pprof" "github.com/mimecast/dtail/internal/server" "github.com/mimecast/dtail/internal/user" "github.com/mimecast/dtail/internal/version" @@ -21,7 +22,6 @@ func main() { var debugEnable bool var displayVersion bool var noColor bool - var pprofEnable bool var shutdownAfter int var sshPort int @@ -30,7 +30,6 @@ func main() { flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages") flag.BoolVar(&displayVersion, "version", false, "Display version") flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors") - flag.BoolVar(&pprofEnable, "pprofEnable", false, "Enable pprof server") flag.IntVar(&shutdownAfter, "shutdownAfter", 0, "Automatically shutdown after so many seconds") flag.IntVar(&sshPort, "port", 2222, "SSH server port") flag.StringVar(&cfgFile, "cfg", "", "Config file path") @@ -44,30 +43,28 @@ func main() { version.PrintAndExit() } - ctx := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + if shutdownAfter > 0 { + logger.Info("Enabling auto shutdown timer", shutdownAfter) + ctx, cancel = context.WithTimeout(ctx, time.Duration(shutdownAfter)*time.Second) + } + + sigCh := make(chan os.Signal) + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) + + go func() { + select { + case <-sigCh: + cancel() + case <-ctx.Done(): + } + }() serverEnable := true silentEnable := false nothingEnable := false logger.Start(ctx, serverEnable, debugEnable, silentEnable, nothingEnable) - if shutdownAfter > 0 { - go func() { - defer os.Exit(1) - - logger.Info("Enabling auto shutdown timer", shutdownAfter) - select { - case <-time.After(time.Duration(shutdownAfter) * time.Second): - logger.Info("Auto shutdown timer reached, shutting down now") - case <-ctx.Done(): - } - }() - } - - if pprofEnable || config.Common.PProfEnable { - pprof.Start() - } - serv := server.New() status := serv.Start(ctx) logger.Flush() |
