X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b49229f98012d7c08ce02b8d28dbcc165c8a6c53..e6769d20505e2c8c74b2d7e3f9c2f33f2a2db092:/lib/service/cmd.go diff --git a/lib/service/cmd.go b/lib/service/cmd.go index f1f3fd91db..880799b348 100644 --- a/lib/service/cmd.go +++ b/lib/service/cmd.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -// package service provides a cmd.Handler that brings up a system service. +// Package service provides a cmd.Handler that brings up a system service. package service import ( @@ -12,6 +12,8 @@ import ( "io" "net" "net/http" + _ "net/http/pprof" + "net/url" "os" "strings" @@ -28,6 +30,7 @@ import ( type Handler interface { http.Handler CheckHealth() error + Done() <-chan struct{} } type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, token string, registry *prometheus.Registry) Handler @@ -58,7 +61,7 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout var err error defer func() { if err != nil { - log.WithError(err).Info("exiting") + log.WithError(err).Error("exiting") } }() @@ -68,16 +71,19 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout loader := config.NewLoader(stdin, log) loader.SetupFlags(flags) versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0") - err = flags.Parse(args) - if err == flag.ErrHelp { - err = nil - return 0 - } else if err != nil { - return 2 + pprofAddr := flags.String("pprof", "", "Serve Go profile data at `[addr]:port`") + if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok { + return code } else if *versionFlag { return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr) } + if *pprofAddr != "" { + go func() { + log.Println(http.ListenAndServe(*pprofAddr, nil)) + }() + } + if strings.HasSuffix(prog, "controller") { // Some config-loader checks try to make API calls via // controller. Those can't be expected to work if this @@ -116,13 +122,14 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout } instrumented := httpserver.Instrument(reg, log, - httpserver.HandlerWithContext(ctx, + httpserver.HandlerWithDeadline(cluster.API.RequestTimeout.Duration(), httpserver.AddRequestIDs( httpserver.LogRequests( httpserver.NewRequestLimiter(cluster.API.MaxConcurrentRequests, handler, reg))))) srv := &httpserver.Server{ Server: http.Server{ - Handler: instrumented.ServeAPI(cluster.ManagementToken, instrumented), + Handler: instrumented.ServeAPI(cluster.ManagementToken, instrumented), + BaseContext: func(net.Listener) context.Context { return ctx }, }, Addr: listenURL.Host, } @@ -147,9 +154,15 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout logger.WithError(err).Errorf("error notifying init daemon") } go func() { + // Shut down server if caller cancels context <-ctx.Done() srv.Close() }() + go func() { + // Shut down server if handler dies + <-handler.Done() + srv.Close() + }() err = srv.Wait() if err != nil { return 1 @@ -157,13 +170,22 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout return 0 } -const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" - func getListenAddr(svcs arvados.Services, prog arvados.ServiceName, log logrus.FieldLogger) (arvados.URL, error) { svc, ok := svcs.Map()[prog] if !ok { return arvados.URL{}, fmt.Errorf("unknown service name %q", prog) } + + if want := os.Getenv("ARVADOS_SERVICE_INTERNAL_URL"); want == "" { + } else if url, err := url.Parse(want); err != nil { + return arvados.URL{}, fmt.Errorf("$ARVADOS_SERVICE_INTERNAL_URL (%q): %s", want, err) + } else { + if url.Path == "" { + url.Path = "/" + } + return arvados.URL(*url), nil + } + errors := []string{} for url := range svc.InternalURLs { listener, err := net.Listen("tcp", url.Host)