Skip to content

Commit

Permalink
Improve signal handling for shutdown
Browse files Browse the repository at this point in the history
- Initiates graceful shutdown upon receiving SIGINT or SIGTERM.
- Logs "Initiating graceful shutdown, repeat signal to force shutdown"
  after canceling the context.
- Adds logic to handle repeated signals to trigger a forced shutdown.
- Implements a 30-second timeout for graceful shutdown before exiting
  with an error.
- Ensures clarity in logs and robust shutdown handling.
  • Loading branch information
anurag4DSB committed Dec 22, 2024
1 parent c77b58b commit a922908
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions cmd/scality-cosi-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ func main() {
go func() {
sig := <-sigs
klog.InfoS("Signal received", "type", sig)
cancel() // Trigger context cancellation
cancel()

klog.InfoS("Initiating graceful shutdown, repeat signal to force shutdown")

select {
case <-ctx.Done():
klog.InfoS("Scality COSI driver shutdown initiated successfully, context canceled")
case sig = <-sigs:
klog.ErrorS(nil, "Force shutdown due to repeated signal", "type", sig)
os.Exit(1)
case <-time.After(30 * time.Second):
klog.ErrorS(nil, "Scality COSI driver graceful shutdown timed out, forcing application exit after 30 seconds")
klog.ErrorS(nil, "Force shutdown due to timeout", "timeout", 30*time.Second)
os.Exit(1)
}
}()

// Call the run function (defined in cmd.go)
if err := run(ctx); err != nil {
klog.ErrorS(err, "Scality COSI driver encountered an error, shutting down")
os.Exit(1)
}
}
7 changes: 4 additions & 3 deletions pkg/grpcfactory/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
"github.com/scality/cosi-driver/pkg/grpcfactory"
cosi "sigs.k8s.io/container-object-storage-interface-spec"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
Expect(server).NotTo(BeNil())

go func() {
err := server.Run(ctx)
err := server.Run(ctx, prometheus.NewRegistry())
if errors.Is(err, context.Canceled) {
return // Expected when the context is canceled
}
Expand All @@ -81,7 +82,7 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
Expect(server2).NotTo(BeNil())

// Run the second server and expect it to fail
err = server2.Run(ctx)
err = server2.Run(ctx, prometheus.NewRegistry())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("address already in use"))
}, SpecTimeout(1*time.Second))
Expand All @@ -94,7 +95,7 @@ var _ = Describe("gRPC Factory Server", Ordered, func() {
Expect(server).NotTo(BeNil())

// Wait for server.Run to return an error
err = server.Run(ctx)
err = server.Run(ctx, prometheus.NewRegistry())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unsupported scheme: expected 'unix'"))
}, SpecTimeout(1*time.Second))
Expand Down

0 comments on commit a922908

Please sign in to comment.