-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: server managing improvements - RK-19210 (#88)
* feat: add gracefull shutdown Signed-off-by: Gosha <gosha@rookout.com> * feat: add logs Signed-off-by: Gosha <gosha@rookout.com> * fix: update git signture in tests Signed-off-by: Gosha <gosha@rookout.com> * fix: better server handler Signed-off-by: Gosha <gosha@rookout.com> * fix: improved server managment Signed-off-by: Gosha <gosha@rookout.com> * fix: improved server managment Signed-off-by: Gosha <gosha@rookout.com> * fix: improved server managment Signed-off-by: Gosha <gosha@rookout.com> * fix: improved server managment Signed-off-by: Gosha <gosha@rookout.com> * fix: improved server managment Signed-off-by: Gosha <gosha@rookout.com> * fix: graceful shutdown handler Signed-off-by: Gosha <gosha@rookout.com> * fix: graceful shutdown handler Signed-off-by: Gosha <gosha@rookout.com> * fix: graceful shutdown handler Signed-off-by: Gosha <gosha@rookout.com> --------- Signed-off-by: Gosha <gosha@rookout.com>
- Loading branch information
Showing
9 changed files
with
148 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/rookout/piper/pkg/clients" | ||
"github.com/rookout/piper/pkg/conf" | ||
"golang.org/x/net/context" | ||
"log" | ||
) | ||
|
||
func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig, clients *clients.Clients) { | ||
|
||
srv := NewServer(cfg, clients) | ||
gracefulShutdownHandler := NewGracefulShutdown(ctx, stop) | ||
httpServer := srv.ListenAndServe() | ||
|
||
err := clients.GitProvider.SetWebhook() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
gracefulShutdownHandler.Shutdown(httpServer, clients) | ||
|
||
log.Println("Server exiting") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,60 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/gin-gonic/gin" | ||
"github.com/rookout/piper/pkg/clients" | ||
"github.com/rookout/piper/pkg/conf" | ||
"github.com/rookout/piper/pkg/server/routes" | ||
"log" | ||
"net/http" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func Init() *gin.Engine { | ||
engine := gin.New() | ||
engine.Use( | ||
gin.LoggerWithConfig(gin.LoggerConfig{ | ||
SkipPaths: []string{"/healthz"}, | ||
}), | ||
gin.Recovery(), | ||
) | ||
return engine | ||
} | ||
|
||
func Start(cfg *conf.GlobalConfig, clients *clients.Clients) { | ||
// Create context that listens for the interrupt signal from the OS. | ||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
|
||
router := Init() | ||
func NewServer(config *conf.GlobalConfig, clients *clients.Clients) *Server { | ||
srv := &Server{ | ||
router: gin.New(), | ||
config: config, | ||
clients: clients, | ||
} | ||
|
||
getRoutes(cfg, clients, router) | ||
return srv | ||
} | ||
|
||
func (s *Server) startServer() *http.Server { | ||
srv := &http.Server{ | ||
Addr: ":8080", | ||
Handler: router, | ||
Handler: s.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) | ||
} | ||
}() | ||
|
||
err := clients.GitProvider.SetWebhook() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Listen for the interrupt signal. | ||
<-ctx.Done() | ||
|
||
// Restore default behavior on the interrupt signal and notify user of shutdown. | ||
stop() | ||
log.Println("shutting down gracefully...") | ||
|
||
// 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() | ||
|
||
_ = gracefulShutdownHandler(&ctx, clients) | ||
return srv | ||
} | ||
|
||
err = srv.Shutdown(ctx) | ||
if err != nil { | ||
log.Fatal("Server forced to shutdown: ", err) | ||
} | ||
func (s *Server) registerMiddlewares() { | ||
s.router.Use( | ||
gin.LoggerWithConfig(gin.LoggerConfig{ | ||
SkipPaths: []string{"/healthz"}, | ||
}), | ||
gin.Recovery(), | ||
) | ||
|
||
log.Println("Server exiting") | ||
} | ||
|
||
func getRoutes(cfg *conf.GlobalConfig, clients *clients.Clients, router *gin.Engine) { | ||
v1 := router.Group("/") | ||
routes.AddHealthRoutes(cfg, v1) | ||
routes.AddWebhookRoutes(cfg, clients, v1) | ||
func (s *Server) getRoutes() { | ||
v1 := s.router.Group("/") | ||
routes.AddHealthRoutes(v1) | ||
routes.AddWebhookRoutes(s.config, s.clients, v1) | ||
} | ||
|
||
func gracefulShutdownHandler(ctx *context.Context, clients *clients.Clients) error { | ||
err := clients.GitProvider.UnsetWebhook(ctx) | ||
if err != nil { | ||
log.Println("Unset webhook error: ", err) // ERROR | ||
return err | ||
} | ||
func (s *Server) ListenAndServe() *http.Server { | ||
|
||
s.registerMiddlewares() | ||
|
||
s.getRoutes() | ||
|
||
return nil | ||
return s.startServer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/rookout/piper/pkg/clients" | ||
"golang.org/x/net/context" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type gracefulShutdown struct { | ||
ctx context.Context | ||
stop context.CancelFunc | ||
} | ||
|
||
func NewGracefulShutdown(ctx context.Context, stop context.CancelFunc) *gracefulShutdown { | ||
return &gracefulShutdown{ | ||
ctx: ctx, | ||
stop: stop, | ||
} | ||
} | ||
|
||
func (s *gracefulShutdown) Shutdown(httpServer *http.Server, clients *clients.Clients) { | ||
// Listen for the interrupt signal. | ||
<-s.ctx.Done() | ||
|
||
// Restore default behavior on the interrupt signal and notify user of shutdown. | ||
s.stop() | ||
|
||
log.Println("shutting down gracefully...") | ||
// 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() | ||
|
||
err := clients.GitProvider.UnsetWebhook(&ctx) | ||
if err != nil { | ||
log.Println("Unset webhook error: ", err) // ERROR | ||
} | ||
|
||
err = httpServer.Shutdown(ctx) | ||
if err != nil { | ||
log.Fatal("Server forced to shutdown: ", err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/rookout/piper/pkg/clients" | ||
"github.com/rookout/piper/pkg/conf" | ||
"net/http" | ||
) | ||
|
||
type Server struct { | ||
router *gin.Engine | ||
config *conf.GlobalConfig | ||
clients *clients.Clients | ||
} | ||
|
||
type Interface interface { | ||
startServer() *http.Server | ||
registerMiddlewares() | ||
getRoutes() | ||
ListenAndServe() *http.Server | ||
} |