Skip to content

Commit fdefe60

Browse files
committed
explicitly handle SIGINT and SIGTERM
1 parent 1f9e725 commit fdefe60

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

cmd/opm/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"os"
6+
"os/signal"
7+
"syscall"
58

69
utilerrors "k8s.io/apimachinery/pkg/util/errors"
710

@@ -12,7 +15,11 @@ import (
1215
func main() {
1316
showAlphaHelp := os.Getenv("HELP_ALPHA") == "true"
1417
cmd := root.NewCmd(showAlphaHelp)
15-
if err := cmd.Execute(); err != nil {
18+
19+
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
20+
defer cancel()
21+
22+
if err := cmd.ExecuteContext(ctx); err != nil {
1623
agg, ok := err.(utilerrors.Aggregate)
1724
if !ok {
1825
os.Exit(1)

cmd/opm/registry/serve.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/operator-framework/operator-registry/pkg/api"
1919
"github.com/operator-framework/operator-registry/pkg/lib/dns"
20-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2120
"github.com/operator-framework/operator-registry/pkg/lib/log"
2221
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
2322
"github.com/operator-framework/operator-registry/pkg/server"
@@ -54,6 +53,9 @@ func newRegistryServeCmd() *cobra.Command {
5453
}
5554

5655
func serveFunc(cmd *cobra.Command, _ []string) error {
56+
ctx, cancel := context.WithCancel(cmd.Context())
57+
defer cancel()
58+
5759
// Immediately set up termination log
5860
terminationLogPath, err := cmd.Flags().GetString("termination-log")
5961
if err != nil {
@@ -93,19 +95,23 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
9395
return err
9496
}
9597

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 {
9799
logger.WithError(err).Warnf("error setting soft heap limit for sqlite")
98100
}
99101

100102
// 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 {
102108
logger.WithError(err).Warnf("couldn't migrate db")
103109
}
104110

105111
store := sqlite.NewSQLLiteQuerierFromDb(db, sqlite.OmitManifests(true))
106112

107113
// sanity check that the db is available
108-
tables, err := store.ListTables(context.TODO())
114+
tables, err := store.ListTables(ctx)
109115
if err != nil {
110116
logger.WithError(err).Warnf("couldn't list tables in db")
111117
}
@@ -142,19 +148,18 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
142148
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
143149
health.RegisterHealthServer(s, server.NewHealthServer())
144150
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")
149155
s.GracefulStop()
150-
})
156+
}()
157+
158+
logger.Info("serving registry")
159+
return s.Serve(lis)
151160
}
152161

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 {
158163
if shouldSkipMigrate {
159164
return nil
160165
}
@@ -167,5 +172,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
167172
return fmt.Errorf("failed to load migrator")
168173
}
169174

170-
return migrator.Migrate(context.TODO())
175+
return migrator.Migrate(ctx)
171176
}

cmd/opm/serve/serve.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/operator-framework/operator-registry/pkg/api"
2424
"github.com/operator-framework/operator-registry/pkg/cache"
2525
"github.com/operator-framework/operator-registry/pkg/lib/dns"
26-
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
2726
"github.com/operator-framework/operator-registry/pkg/lib/log"
2827
"github.com/operator-framework/operator-registry/pkg/server"
2928
)
@@ -91,6 +90,9 @@ will not be reflected in the served content.
9190
}
9291

9392
func (s *serve) run(ctx context.Context) error {
93+
ctx, cancel := context.WithCancel(ctx)
94+
defer cancel()
95+
9496
mainLogger := s.logger.Dup()
9597
p := newProfilerInterface(s.pprofAddr, mainLogger)
9698
if err := p.startEndpoint(); err != nil {
@@ -169,15 +171,16 @@ func (s *serve) run(ctx context.Context) error {
169171
mainLogger.Info("serving registry")
170172
p.stopCpuProfileCache()
171173

172-
return graceful.Shutdown(s.logger, func() error {
173-
return grpcServer.Serve(lis)
174-
}, func() {
174+
go func() {
175+
<-ctx.Done()
176+
mainLogger.Info("shutting down server")
175177
grpcServer.GracefulStop()
176178
if err := p.stopEndpoint(ctx); err != nil {
177179
mainLogger.Warnf("error shutting down pprof server: %v", err)
178180
}
179-
})
181+
}()
180182

183+
return grpcServer.Serve(lis)
181184
}
182185

183186
// manages an HTTP pprof endpoint served by `server`,

pkg/lib/graceful/shutdown.go

-33
This file was deleted.

0 commit comments

Comments
 (0)