@@ -17,7 +17,6 @@ import (
17
17
18
18
"github.com/operator-framework/operator-registry/pkg/api"
19
19
"github.com/operator-framework/operator-registry/pkg/lib/dns"
20
- "github.com/operator-framework/operator-registry/pkg/lib/graceful"
21
20
"github.com/operator-framework/operator-registry/pkg/lib/log"
22
21
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
23
22
"github.com/operator-framework/operator-registry/pkg/server"
@@ -54,6 +53,9 @@ func newRegistryServeCmd() *cobra.Command {
54
53
}
55
54
56
55
func serveFunc (cmd * cobra.Command , _ []string ) error {
56
+ ctx , cancel := context .WithCancel (cmd .Context ())
57
+ defer cancel ()
58
+
57
59
// Immediately set up termination log
58
60
terminationLogPath , err := cmd .Flags ().GetString ("termination-log" )
59
61
if err != nil {
@@ -93,19 +95,23 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
93
95
return err
94
96
}
95
97
96
- if _ , err := db .ExecContext (context . TODO () , `PRAGMA soft_heap_limit=1` ); err != nil {
98
+ if _ , err := db .ExecContext (ctx , `PRAGMA soft_heap_limit=1` ); err != nil {
97
99
logger .WithError (err ).Warnf ("error setting soft heap limit for sqlite" )
98
100
}
99
101
100
102
// migrate to the latest version
101
- if err := migrate (cmd , db ); err != nil {
103
+ shouldSkipMigrate , err := cmd .Flags ().GetBool ("skip-migrate" )
104
+ if err != nil {
105
+ return err
106
+ }
107
+ if err := migrate (ctx , shouldSkipMigrate , db ); err != nil {
102
108
logger .WithError (err ).Warnf ("couldn't migrate db" )
103
109
}
104
110
105
111
store := sqlite .NewSQLLiteQuerierFromDb (db , sqlite .OmitManifests (true ))
106
112
107
113
// sanity check that the db is available
108
- tables , err := store .ListTables (context . TODO () )
114
+ tables , err := store .ListTables (ctx )
109
115
if err != nil {
110
116
logger .WithError (err ).Warnf ("couldn't list tables in db" )
111
117
}
@@ -142,19 +148,18 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
142
148
api .RegisterRegistryServer (s , server .NewRegistryServer (store ))
143
149
health .RegisterHealthServer (s , server .NewHealthServer ())
144
150
reflection .Register (s )
145
- logger . Info ( "serving registry" )
146
- return graceful . Shutdown ( logger , func () error {
147
- return s . Serve ( lis )
148
- }, func () {
151
+
152
+ go func () {
153
+ <- ctx . Done ( )
154
+ logger . Info ( "shutting down server" )
149
155
s .GracefulStop ()
150
- })
156
+ }()
157
+
158
+ logger .Info ("serving registry" )
159
+ return s .Serve (lis )
151
160
}
152
161
153
- func migrate (cmd * cobra.Command , db * sql.DB ) error {
154
- shouldSkipMigrate , err := cmd .Flags ().GetBool ("skip-migrate" )
155
- if err != nil {
156
- return err
157
- }
162
+ func migrate (ctx context.Context , shouldSkipMigrate bool , db * sql.DB ) error {
158
163
if shouldSkipMigrate {
159
164
return nil
160
165
}
@@ -167,5 +172,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
167
172
return fmt .Errorf ("failed to load migrator" )
168
173
}
169
174
170
- return migrator .Migrate (context . TODO () )
175
+ return migrator .Migrate (ctx )
171
176
}
0 commit comments