Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package application
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"time"

Expand Down Expand Up @@ -92,6 +94,9 @@ func (a *Application) Run(ctx context.Context) error {
ctx = context.Background()
}

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
defer cancel()

log.InfoContext(ctx, "starting application", "startupTasks", len(a.startupTasks))

for dbName, db := range a.databases {
Expand Down
16 changes: 6 additions & 10 deletions httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/mishankov/platforma/log"
Expand Down Expand Up @@ -42,20 +39,19 @@ func (s *HTTPServer) Run(ctx context.Context) error {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.ErrorContext(ctx, "HTTP server error", "error", err)
}
log.InfoContext(ctx, "stopped serving new connections.")
log.InfoContext(ctx, "stopped serving new connections")
}()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
<-ctx.Done()

shutdownCtx, shutdownRelease := context.WithTimeout(ctx, s.shutdownTimeout)
defer shutdownRelease()
shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, s.shutdownTimeout)
defer cancel()

if err := server.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("failed to gracefully shutdown HTTP server: %w", err)
}
log.InfoContext(ctx, "graceful shutdown completed.")
log.InfoContext(ctx, "graceful shutdown completed")

return nil
}
Expand Down
Loading