|
6 | 6 | "fmt"
|
7 | 7 | "net"
|
8 | 8 | "os"
|
| 9 | + "strconv" |
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/sirupsen/logrus"
|
11 | 13 | "github.com/spf13/cobra"
|
@@ -42,8 +44,9 @@ func newRegistryServeCmd() *cobra.Command {
|
42 | 44 | rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
|
43 | 45 | rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
|
44 | 46 | rootCmd.Flags().Bool("skip-migrate", false, "do not attempt to migrate to the latest db revision when starting")
|
45 |
| - return rootCmd |
| 47 | + rootCmd.Flags().String("timeout-seconds", "infinite", "Timeout in seconds. This flag will be removed later.") |
46 | 48 |
|
| 49 | + return rootCmd |
47 | 50 | }
|
48 | 51 |
|
49 | 52 | func serveFunc(cmd *cobra.Command, args []string) error {
|
@@ -106,7 +109,27 @@ func serveFunc(cmd *cobra.Command, args []string) error {
|
106 | 109 | if err != nil {
|
107 | 110 | logger.Fatalf("failed to listen: %s", err)
|
108 | 111 | }
|
| 112 | + |
| 113 | + timeout, err := cmd.Flags().GetString("timeout-seconds") |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
109 | 118 | s := grpc.NewServer()
|
| 119 | + logger.Printf("Keeping server open for %s seconds", timeout) |
| 120 | + if timeout != "infinite" { |
| 121 | + timeoutSeconds, err := strconv.ParseUint(timeout, 10, 16) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + timeoutDuration := time.Duration(timeoutSeconds) * time.Second |
| 127 | + timer := time.AfterFunc(timeoutDuration, func() { |
| 128 | + logger.Info("Timeout expired. Gracefully stopping.") |
| 129 | + s.GracefulStop() |
| 130 | + }) |
| 131 | + defer timer.Stop() |
| 132 | + } |
110 | 133 |
|
111 | 134 | api.RegisterRegistryServer(s, server.NewRegistryServer(store))
|
112 | 135 | health.RegisterHealthServer(s, server.NewHealthServer())
|
|
0 commit comments