[PHASE-9] Fix graceful shutdown
This commit is contained in:
parent
997859db84
commit
6bf918dca8
|
|
@ -8,7 +8,6 @@ import (
|
|||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"hash-of-wisdom/internal/config"
|
||||
"hash-of-wisdom/internal/lib/sl"
|
||||
|
|
@ -18,8 +17,6 @@ import (
|
|||
"hash-of-wisdom/internal/service"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
_ "net/http/pprof"
|
||||
)
|
||||
|
||||
|
|
@ -66,9 +63,7 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
// Register Go runtime metrics
|
||||
prometheus.MustRegister(collectors.NewGoCollector())
|
||||
prometheus.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
||||
// Go runtime metrics are automatically registered by default registry
|
||||
|
||||
// Start metrics and pprof HTTP server
|
||||
go func() {
|
||||
|
|
@ -79,31 +74,31 @@ func main() {
|
|||
}
|
||||
}()
|
||||
|
||||
// Create context that cancels on interrupt signals
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
// Create server
|
||||
srv := server.NewTCPServer(wisdomService, serverConfig,
|
||||
server.WithLogger(logger))
|
||||
|
||||
// Start server
|
||||
ctx := context.Background()
|
||||
if err := srv.Start(ctx); err != nil {
|
||||
logger.Error("failed to start server", sl.Err(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Wait for interrupt
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
logger.Info("server ready - press ctrl+c to stop")
|
||||
<-sigChan
|
||||
|
||||
// Wait for context cancellation (signal received)
|
||||
<-ctx.Done()
|
||||
|
||||
// Graceful shutdown
|
||||
logger.Info("shutting down server")
|
||||
if err := srv.Stop(); err != nil {
|
||||
logger.Error("error during shutdown", sl.Err(err))
|
||||
} else {
|
||||
logger.Info("server stopped gracefully")
|
||||
}
|
||||
|
||||
// Give connections time to close
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
logger.Info("server stopped")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue