Nisha Vijayakumar
Nisha Vijayakumar

Reputation: 31

SIGTERM signal logs for Go application in EKS

func ServeRequest(configuredRoutes http.Handler) {
    air_logger.SERVER_STARTUP("########## SERVER STARTED ##########")
    server := &http.Server{
        Addr: config.GetConfig().Server.Port,
        Handler: handlers.CORS(
            handlers.AllowedMethods([]string{"OPTIONS", "GET", "POST", "PUT", "PATCH"}),
            handlers.AllowedHeaders([]string{"DNT", "User-Agent", "X-Requested-With", "If-Modified-Since", "Cache-Control",
                "Content-Type", "Range", "Pragma", "X-FourKitesUserId", "X-FourKitesDeviceId", "Authorization", "Expires"}),
            handlers.MaxAge(600),
        )(configuredRoutes),
    }
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
    <-quit
    test_logger.Info(context.TODO(), "test-service server is shutting down")

    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    server.SetKeepAlivesEnabled(false)
    if err := server.Shutdown(ctx); err != nil {
        test_logger.Fatal(ctx, "error in graceful shutdown of the test-service server:", err.Error())
    }
    close(done)
}()

if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
    test_logger.Fatal(context.TODO(), "test-service server shutdown with error: ", err.Error())
}

test_logger.Info(context.TODO(), "test-service before server shutdown gracefully")
<-done
test_logger.Info(context.TODO(), "test-service server shutdown gracefully")
}

The above is the initialiser file for my go application . I am trying to capture logs that my server is gracefully shutting down when a pod is deleted in EKS . But when the SIGTERM signal is passed the server is immediately shutting down and we are not seeing any logs which should have been captured before shutting down indicating a gracefull shutdown. I have set the "terminationGracePeriodSeconds" as 180

Upvotes: 1

Views: 162

Answers (1)

zeg
zeg

Reputation: 586

Try to add this snippet:

    c := make(chan os.Signal)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM) // 15
    go func() {
        sig := <-c
        fmt.Println(time.Now().Format("2006-01-02T15:04:05Z") + " received SIGTERM Shutdown...")
        os.Exit(1)
    }()

Upvotes: -1

Related Questions