get.porter.sh/porter@v1.3.0/pkg/signals/shutdown.go (about) 1 package signals 2 3 import ( 4 "context" 5 "time" 6 7 "get.porter.sh/porter/pkg/tracing" 8 "github.com/spf13/viper" 9 oteltrace "go.opentelemetry.io/otel/sdk/trace" 10 "google.golang.org/grpc" 11 ) 12 13 type Shutdown struct { 14 tracerProvider *oteltrace.TracerProvider 15 serverShutdownTimeout time.Duration 16 } 17 18 func NewShutdown(serverShutdownTimeout time.Duration, ctx context.Context) (*Shutdown, error) { 19 srv := &Shutdown{ 20 serverShutdownTimeout: serverShutdownTimeout, 21 } 22 23 return srv, nil 24 } 25 26 func (s *Shutdown) Graceful(stopCh <-chan struct{}, grpcServer *grpc.Server, ctx context.Context) { 27 ctx, log := tracing.StartSpan(ctx) 28 29 // wait for SIGTERM or SIGINT 30 <-stopCh 31 ctx, cancel := context.WithTimeout(ctx, s.serverShutdownTimeout) 32 defer cancel() 33 34 // wait for Kubernetes readiness probe to remove this instance from the load balancer 35 // the readiness check interval must be lower than the timeout 36 if viper.GetString("level") != "debug" { 37 time.Sleep(3 * time.Second) 38 } 39 40 // stop OpenTelemetry tracer provider 41 if s.tracerProvider != nil { 42 if err := s.tracerProvider.Shutdown(ctx); err != nil { 43 log.Warnf("stopping tracer provider: ", err) 44 } 45 } 46 47 // determine if the GRPC was started 48 if grpcServer != nil { 49 log.Infof("Shutting down GRPC server: ", s.serverShutdownTimeout) 50 grpcServer.GracefulStop() 51 } 52 53 }