Skip to content

Commit

Permalink
fix: handle signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvis8x7b committed Oct 17, 2024
1 parent cee8d85 commit 086310a
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,40 @@ func main() {
Handler: router,
}

go func() {
// service connections
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("listen")
}
}()
done := make(chan bool, 1)

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
sig := <-quit
log.Info().Msgf("Received signal: %s. Shutting down...", sig)

numSeconds := 2
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(numSeconds)*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("Server Shutdown:")
// shutdown tasks

go func() {
sig := <-quit
log.Info().Msgf("Received signal: %s. Shutting down...", sig)

numSeconds := 5 // Increased timeout for graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(numSeconds)*time.Second)
defer cancel()

server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Server Shutdown:")
}

onShutdown()

close(done)
}()

log.Info().Msgf("Server starting on port %s", port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("Server startup failed")
}
// catching ctx.Done(). timeout of 5 seconds.
<-ctx.Done()

<-done
log.Info().Msg("Server exiting")
}

func onShutdown() {
log.Info().Msg("Shutting down server")
log.Info().Msg("Performing shutting down server")
connHandler := orm.GetConnHandler()
connHandler.OnShutdown()
cache := cache.GetCacheInstance()
Expand Down

0 comments on commit 086310a

Please sign in to comment.