Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package main

import (
"context"
"flag"
"fmt"
"github.com/joho/godotenv"
Expand All @@ -31,7 +32,10 @@ import (
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)

func initDatabaseFromConfig(config *config.Config) {
Expand Down Expand Up @@ -97,11 +101,32 @@ func main() {
logger.Info(fmt.Sprintf("WSO2 CDS started in server address: %s", serverAddr))
server1 := &http.Server{Handler: mux}

if err := server1.Serve(ln); err != nil {
logger.Error("Failed to serve requests. ", log.Error(err))
// Start serving requests in background
go func() {
if err := server1.Serve(ln); err != nil && err != http.ErrServerClosed {
logger.Error("Failed to serve requests.", log.Error(err))
}
}()

// Listen for termination signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop // Wait for shutdown signal

logger.Info("Shutdown signal received. Shutting down CDS gracefully...")

// Graceful shutdown with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // optionally use 5 * time.Second
defer cancel()
if err := server1.Shutdown(ctx); err != nil {
logger.Error("Graceful shutdown failed. Forcing the server to shut down.", log.Error(err))
_ = server1.Close()
} else {
logger.Info("Server gracefully shut down.")
}

logger.Info("identity-customer-data-service component has started.")
_ = server1.Close()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to always invoke close()? If so we can keep this invocation and remove the above one.

logger.Info("CDS service shut down completed.")

}

Expand Down
Loading