Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graceful shutdown allows to server to wait for 10 sec to finish work #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 47 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
package main

import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"ukiyo/pkg/apilayer"
"ukiyo/pkg/webhooks"

"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
webhooks.HooksListener(r)
webhooks.HealthCheck(r)
apilayer.AddContainer(r)
apilayer.EditContainerToken(r)
r.Run(":8080")
router := gin.Default()

webhooks.HooksListener(router)
webhooks.HealthCheck(router)
apilayer.AddContainer(router)
apilayer.EditContainerToken(router)

srv := &http.Server{
Addr: ":8080",
Handler: router,
}

// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("ukiyo Shutting down server...")

// The context is used to inform the server it has 10 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("ukiyo Server forced to shutdown:", err)
}

log.Println("ukiyo Server exiting")

}