Skip to content

Commit e590015

Browse files
authoredJul 31, 2020
Merge pull request operator-framework#370 from Jobava/timeout
Add timeout to opm registry serve
2 parents 19378f2 + 8de32fb commit e590015

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed
 

‎Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ static: build
3636
unit:
3737
$(GO) test $(SPECIFIC_UNIT_TEST) $(TAGS) $(TEST_RACE) -count=1 -v ./pkg/...
3838

39+
.PHONY: sanity-check
40+
sanity-check:
41+
# Build a container with the most recent binaries for this project.
42+
# Does not include the database, which needs to be added separately.
43+
docker build -f upstream-builder.Dockerfile -t sanity-container .
44+
45+
# TODO: add more invocations of the opm binary here
46+
47+
# serve the container for a second, using the bundles.db in testdata
48+
docker run --rm -it -v "$(shell pwd)"/pkg/lib/indexer/testdata/:/database sanity-container \
49+
./bin/opm registry serve --database /database/bundles.db --timeout-seconds 1
50+
3951
.PHONY: image
4052
image:
4153
docker build .

‎cmd/opm/registry/serve.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"net"
88
"os"
9+
"strconv"
10+
"time"
911

1012
"github.com/sirupsen/logrus"
1113
"github.com/spf13/cobra"
@@ -42,8 +44,9 @@ func newRegistryServeCmd() *cobra.Command {
4244
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
4345
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
4446
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.")
4648

49+
return rootCmd
4750
}
4851

4952
func serveFunc(cmd *cobra.Command, args []string) error {
@@ -106,7 +109,27 @@ func serveFunc(cmd *cobra.Command, args []string) error {
106109
if err != nil {
107110
logger.Fatalf("failed to listen: %s", err)
108111
}
112+
113+
timeout, err := cmd.Flags().GetString("timeout-seconds")
114+
if err != nil {
115+
return err
116+
}
117+
109118
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+
}
110133

111134
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
112135
health.RegisterHealthServer(s, server.NewHealthServer())

0 commit comments

Comments
 (0)
Please sign in to comment.