diff options
author | Valery Piashchynski <[email protected]> | 2023-09-10 16:09:50 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2023-09-10 16:09:50 +0200 |
commit | a1adbbacac9f5d05495884a99c700b8b72da833a (patch) | |
tree | 4e838db5b7981399d26714890c8df9f486b45eb0 /internal/cli/root.go | |
parent | 1ff0014cdb6cf50d8dd60993ed0ced814579cc4d (diff) |
chore: improve debug (pprof) server error handling
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal/cli/root.go')
-rw-r--r-- | internal/cli/root.go | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/internal/cli/root.go b/internal/cli/root.go index 3f69e807..1de50c43 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -2,7 +2,16 @@ package cli import ( "context" + stderr "errors" "fmt" + "net/http" + "os" + "os/signal" + "path/filepath" + "runtime" + "strconv" + "syscall" + "github.com/joho/godotenv" "github.com/roadrunner-server/errors" "github.com/roadrunner-server/roadrunner/v2023/internal/cli/jobs" @@ -13,12 +22,6 @@ import ( dbg "github.com/roadrunner-server/roadrunner/v2023/internal/debug" "github.com/roadrunner-server/roadrunner/v2023/internal/meta" "github.com/spf13/cobra" - "os" - "os/signal" - "path/filepath" - "runtime" - "strconv" - "syscall" ) const ( @@ -92,13 +95,32 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen,gocognit if debug { srv := dbg.NewServer() + exit := make(chan os.Signal, 1) + stpErr := make(chan error, 1) + signal.Notify(exit, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGABRT) + go func() { - exit := make(chan os.Signal, 1) - signal.Notify(exit, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGABRT) - go srv.Start(":6061") + errS := srv.Start(":6061") + // errS is always non-nil, this is just double check + if errS != nil && stderr.Is(errS, http.ErrServerClosed) { + return + } + // if we have other type of error - record it + stpErr <- errS + }() - <-exit - _ = srv.Stop(context.Background()) + go func() { + for { + select { + case e := <-stpErr: + // no need to stop the server + fmt.Println(fmt.Errorf("[ERROR] debug server stopped with error: %w", e)) + + return + case <-exit: + _ = srv.Stop(context.Background()) + } + } }() } |