File tree Expand file tree Collapse file tree 5 files changed +57
-18
lines changed Expand file tree Collapse file tree 5 files changed +57
-18
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313 health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
1414 "github.com/operator-framework/operator-registry/pkg/appregistry"
1515 "github.com/operator-framework/operator-registry/pkg/lib/dns"
16+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
1617 "github.com/operator-framework/operator-registry/pkg/lib/log"
1718 "github.com/operator-framework/operator-registry/pkg/registry"
1819 "github.com/operator-framework/operator-registry/pkg/server"
@@ -128,9 +129,9 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
128129 reflection .Register (s )
129130
130131 logger .Info ("serving registry" )
131- if err := s . Serve ( lis ); err != nil {
132- logger . Fatalf ( "failed to serve: %s" , err )
133- }
134-
135- return nil
132+ return graceful . Shutdown ( logger , func () error {
133+ return s . Serve ( lis )
134+ }, func () {
135+ s . GracefulStop ()
136+ })
136137}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import (
1818 "github.com/operator-framework/operator-registry/pkg/api"
1919 health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
2020 "github.com/operator-framework/operator-registry/pkg/lib/dns"
21+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
2122 "github.com/operator-framework/operator-registry/pkg/lib/log"
2223 "github.com/operator-framework/operator-registry/pkg/registry"
2324 "github.com/operator-framework/operator-registry/pkg/server"
@@ -155,10 +156,11 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
155156 reflection .Register (s )
156157
157158 logger .Info ("serving registry" )
158- if err := s .Serve (lis ); err != nil {
159- logger .Fatalf ("failed to serve: %s" , err )
160- }
161- return nil
159+ return graceful .Shutdown (logger , func () error {
160+ return s .Serve (lis )
161+ }, func () {
162+ s .GracefulStop ()
163+ })
162164}
163165
164166// NewClient creates a kubernetes client or bails out on on failures.
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import (
1717 "github.com/operator-framework/operator-registry/pkg/api"
1818 health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
1919 "github.com/operator-framework/operator-registry/pkg/lib/dns"
20+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
2021 "github.com/operator-framework/operator-registry/pkg/lib/log"
2122 "github.com/operator-framework/operator-registry/pkg/lib/tmp"
2223 "github.com/operator-framework/operator-registry/pkg/server"
@@ -135,11 +136,11 @@ func serveFunc(cmd *cobra.Command, args []string) error {
135136 health .RegisterHealthServer (s , server .NewHealthServer ())
136137 reflection .Register (s )
137138 logger .Info ("serving registry" )
138- if err := s . Serve ( lis ); err != nil {
139- logger . Fatalf ( "failed to serve: %s" , err )
140- }
141-
142- return nil
139+ return graceful . Shutdown ( logger , func () error {
140+ return s . Serve ( lis )
141+ }, func () {
142+ s . GracefulStop ()
143+ })
143144}
144145
145146func migrate (cmd * cobra.Command , db * sql.DB ) error {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import (
1515 "github.com/operator-framework/operator-registry/pkg/api"
1616 health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
1717 "github.com/operator-framework/operator-registry/pkg/lib/dns"
18+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
1819 "github.com/operator-framework/operator-registry/pkg/lib/log"
1920 "github.com/operator-framework/operator-registry/pkg/lib/tmp"
2021 "github.com/operator-framework/operator-registry/pkg/server"
@@ -116,11 +117,12 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
116117 health .RegisterHealthServer (s , server .NewHealthServer ())
117118 reflection .Register (s )
118119 logger .Info ("serving registry" )
119- if err := s .Serve (lis ); err != nil {
120- logger .Fatalf ("failed to serve: %s" , err )
121- }
122120
123- return nil
121+ return graceful .Shutdown (logger , func () error {
122+ return s .Serve (lis )
123+ }, func () {
124+ s .GracefulStop ()
125+ })
124126}
125127
126128func migrate (cmd * cobra.Command , db * sql.DB ) error {
Original file line number Diff line number Diff line change 1+ package graceful
2+
3+ import (
4+ "context"
5+ "os"
6+ "os/signal"
7+ "syscall"
8+
9+ "github.com/sirupsen/logrus"
10+ "golang.org/x/sync/errgroup"
11+ )
12+
13+ func Shutdown (logger logrus.FieldLogger , run func () error , cleanup func ()) error {
14+ interrupt := make (chan os.Signal , 1 )
15+ signal .Notify (interrupt , os .Interrupt , syscall .SIGTERM )
16+ defer signal .Stop (interrupt )
17+
18+ g , ctx := errgroup .WithContext (context .Background ())
19+ g .Go (run )
20+
21+ select {
22+ case <- interrupt :
23+ break
24+ case <- ctx .Done ():
25+ break
26+ }
27+
28+ logger .Info ("shutting down..." )
29+
30+ cleanup ()
31+
32+ return g .Wait ()
33+ }
You can’t perform that action at this time.
0 commit comments