File tree 5 files changed +57
-18
lines changed
5 files changed +57
-18
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
14
14
"github.com/operator-framework/operator-registry/pkg/appregistry"
15
15
"github.com/operator-framework/operator-registry/pkg/lib/dns"
16
+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
16
17
"github.com/operator-framework/operator-registry/pkg/lib/log"
17
18
"github.com/operator-framework/operator-registry/pkg/registry"
18
19
"github.com/operator-framework/operator-registry/pkg/server"
@@ -128,9 +129,9 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
128
129
reflection .Register (s )
129
130
130
131
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
+ })
136
137
}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import (
18
18
"github.com/operator-framework/operator-registry/pkg/api"
19
19
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
20
20
"github.com/operator-framework/operator-registry/pkg/lib/dns"
21
+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
21
22
"github.com/operator-framework/operator-registry/pkg/lib/log"
22
23
"github.com/operator-framework/operator-registry/pkg/registry"
23
24
"github.com/operator-framework/operator-registry/pkg/server"
@@ -155,10 +156,11 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
155
156
reflection .Register (s )
156
157
157
158
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
+ })
162
164
}
163
165
164
166
// NewClient creates a kubernetes client or bails out on on failures.
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import (
15
15
"github.com/operator-framework/operator-registry/pkg/api"
16
16
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
17
17
"github.com/operator-framework/operator-registry/pkg/lib/dns"
18
+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
18
19
"github.com/operator-framework/operator-registry/pkg/lib/log"
19
20
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
20
21
"github.com/operator-framework/operator-registry/pkg/server"
@@ -112,11 +113,11 @@ func serveFunc(cmd *cobra.Command, args []string) error {
112
113
health .RegisterHealthServer (s , server .NewHealthServer ())
113
114
reflection .Register (s )
114
115
logger .Info ("serving registry" )
115
- if err := s . Serve ( lis ); err != nil {
116
- logger . Fatalf ( "failed to serve: %s" , err )
117
- }
118
-
119
- return nil
116
+ return graceful . Shutdown ( logger , func () error {
117
+ return s . Serve ( lis )
118
+ }, func () {
119
+ s . GracefulStop ()
120
+ })
120
121
}
121
122
122
123
func migrate (cmd * cobra.Command , db * sql.DB ) error {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import (
15
15
"github.com/operator-framework/operator-registry/pkg/api"
16
16
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
17
17
"github.com/operator-framework/operator-registry/pkg/lib/dns"
18
+ "github.com/operator-framework/operator-registry/pkg/lib/graceful"
18
19
"github.com/operator-framework/operator-registry/pkg/lib/log"
19
20
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
20
21
"github.com/operator-framework/operator-registry/pkg/server"
@@ -116,11 +117,12 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
116
117
health .RegisterHealthServer (s , server .NewHealthServer ())
117
118
reflection .Register (s )
118
119
logger .Info ("serving registry" )
119
- if err := s .Serve (lis ); err != nil {
120
- logger .Fatalf ("failed to serve: %s" , err )
121
- }
122
120
123
- return nil
121
+ return graceful .Shutdown (logger , func () error {
122
+ return s .Serve (lis )
123
+ }, func () {
124
+ s .GracefulStop ()
125
+ })
124
126
}
125
127
126
128
func 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