Skip to content

Commit

Permalink
explicitly handle SIGINT and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanford committed Sep 11, 2024
1 parent 1f9e725 commit 7be452f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 54 deletions.
9 changes: 8 additions & 1 deletion cmd/opm/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"

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

Expand All @@ -12,7 +15,11 @@ import (
func main() {
showAlphaHelp := os.Getenv("HELP_ALPHA") == "true"
cmd := root.NewCmd(showAlphaHelp)
if err := cmd.Execute(); err != nil {

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

if err := cmd.ExecuteContext(ctx); err != nil {
agg, ok := err.(utilerrors.Aggregate)
if !ok {
os.Exit(1)
Expand Down
35 changes: 20 additions & 15 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/lib/tmp"
"github.com/operator-framework/operator-registry/pkg/server"
Expand Down Expand Up @@ -54,6 +53,9 @@ func newRegistryServeCmd() *cobra.Command {
}

func serveFunc(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
Expand Down Expand Up @@ -93,19 +95,23 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
return err
}

if _, err := db.ExecContext(context.TODO(), `PRAGMA soft_heap_limit=1`); err != nil {
if _, err := db.ExecContext(ctx, `PRAGMA soft_heap_limit=1`); err != nil {
logger.WithError(err).Warnf("error setting soft heap limit for sqlite")
}

// migrate to the latest version
if err := migrate(cmd, db); err != nil {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
if err := migrate(ctx, shouldSkipMigrate, db); err != nil {
logger.WithError(err).Warnf("couldn't migrate db")
}

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

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
tables, err := store.ListTables(ctx)
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
Expand Down Expand Up @@ -142,19 +148,18 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)
logger.Info("serving registry")
return graceful.Shutdown(logger, func() error {
return s.Serve(lis)
}, func() {

go func() {
<-ctx.Done()
logger.Info("shutting down server")
s.GracefulStop()
})
}()

logger.Info("serving registry")
return s.Serve(lis)
}

func migrate(cmd *cobra.Command, db *sql.DB) error {
shouldSkipMigrate, err := cmd.Flags().GetBool("skip-migrate")
if err != nil {
return err
}
func migrate(ctx context.Context, shouldSkipMigrate bool, db *sql.DB) error {
if shouldSkipMigrate {
return nil
}
Expand All @@ -167,5 +172,5 @@ func migrate(cmd *cobra.Command, db *sql.DB) error {
return fmt.Errorf("failed to load migrator")
}

return migrator.Migrate(context.TODO())
return migrator.Migrate(ctx)
}
10 changes: 5 additions & 5 deletions cmd/opm/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/cache"
"github.com/operator-framework/operator-registry/pkg/lib/dns"
"github.com/operator-framework/operator-registry/pkg/lib/graceful"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/server"
)
Expand Down Expand Up @@ -169,15 +168,16 @@ func (s *serve) run(ctx context.Context) error {
mainLogger.Info("serving registry")
p.stopCpuProfileCache()

return graceful.Shutdown(s.logger, func() error {
return grpcServer.Serve(lis)
}, func() {
go func() {
<-ctx.Done()
mainLogger.Info("shutting down server")
grpcServer.GracefulStop()
if err := p.stopEndpoint(ctx); err != nil {
mainLogger.Warnf("error shutting down pprof server: %v", err)
}
})
}()

return grpcServer.Serve(lis)
}

// manages an HTTP pprof endpoint served by `server`,
Expand Down
33 changes: 0 additions & 33 deletions pkg/lib/graceful/shutdown.go

This file was deleted.

0 comments on commit 7be452f

Please sign in to comment.