cmd/worker: catch when the server is about to get killed And log how long it was alive. This is achieved by catching a SIGTERM signal. Cloud Run shuts down a container 10s after this signal is sent, so this allows us to print the state of the server. The hope is that we can find out why our instance are being killed often. https://cloud.google.com/run/docs/container-contract#lifecycle Change-Id: I6df4a96843cf4e82e2c485464afbf440263abcb7 Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/539775 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/cmd/worker/main.go b/cmd/worker/main.go index 6c9b212..bca9f99 100644 --- a/cmd/worker/main.go +++ b/cmd/worker/main.go
@@ -11,6 +11,8 @@ "fmt" "net/http" "os" + "os/signal" + "syscall" "time" "golang.org/x/exp/slog" @@ -68,10 +70,24 @@ cfg.Insecure = *insecure cfg.Dump(os.Stdout) log.Infof(ctx, "config: project=%s, dataset=%s", cfg.ProjectID, cfg.BigQueryDataset) + if _, err := worker.NewServer(ctx, cfg); err != nil { return err } + go monitor(ctx) + addr := ":" + *port log.Infof(ctx, "Listening on addr http://localhost%s", addr) return fmt.Errorf("listening: %v", http.ListenAndServe(addr, nil)) } + +// monitor measures details of server execution from +// the moment is starts listening to the moment it +// gets a SIGTERM signal. +func monitor(ctx context.Context) { + start := time.Now() + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGTERM) + <-signals + log.Infof(ctx, "server stopped listening after: %v\n", time.Since(start)) +}