Skip to content

Commit

Permalink
image-builder-maintenance: implement graceful shutdown on signal
Browse files Browse the repository at this point in the history
graceful shutdown on SIGTERM and SIGINT even when currently
executing SQL statements
  • Loading branch information
schuellerf committed Nov 25, 2024
1 parent ef9f5b7 commit e07fe65
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions cmd/image-builder-maintenance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/ilyakaznacheev/cleanenv"
Expand All @@ -11,8 +14,28 @@ import (
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Hour)
defer cancel()
ctx, applicationCancel := context.WithCancel(context.Background())
defer applicationCancel()

terminationSignal := make(chan os.Signal, 1)
signal.Notify(terminationSignal, syscall.SIGTERM, syscall.SIGINT)
defer signal.Stop(terminationSignal)

// Channel to track graceful application shutdown
shutdownSignal := make(chan struct{})

go func() {
select {
case <-terminationSignal:
fmt.Println("Received termination signal, cancelling context...")
applicationCancel()
case <-shutdownSignal:
// Normal application shutdown, no logging
}
}()

ctx, cancelTimeout := context.WithTimeout(ctx, 2*time.Hour)
defer cancelTimeout()

logrus.SetReportCaller(true)

Expand Down Expand Up @@ -44,4 +67,5 @@ func main() {
logrus.Fatalf("Error during DBCleanup: %v", err)
}
logrus.Info("🦀🦀🦀 dbqueue cleanup done 🦀🦀🦀")
close(shutdownSignal)
}

0 comments on commit e07fe65

Please sign in to comment.