move setupRestart

This commit is contained in:
c9s 2021-02-04 16:54:24 +08:00
parent e1b4ff3450
commit 5f84c847c1
2 changed files with 44 additions and 41 deletions

View File

@ -13,7 +13,6 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/gin-contrib/cors"
@ -248,46 +247,6 @@ func (s *Server) ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "pong"})
}
func (s *Server) setupRestart(c *gin.Context) {
if s.srv == nil {
logrus.Error("nil srv")
return
}
go func() {
logrus.Info("shutting down web server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil {
logrus.WithError(err).Error("server forced to shutdown")
}
logrus.Info("web server shutdown completed")
bin := os.Args[0]
args := os.Args[0:]
// filter out setup parameters
args = filterStrings(args, "--setup")
envVars := os.Environ()
logrus.Infof("%s %v %+v", bin, args, envVars)
if err := syscall.Exec(bin, args, envVars); err != nil {
logrus.WithError(err).Errorf("failed to restart %s", bin)
}
s.Setup.Cancel()
}()
c.JSON(http.StatusOK, gin.H{"success": true})
}
func (s *Server) listClosedOrders(c *gin.Context) {
if s.Environ.OrderService == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "database is not configured"})

View File

@ -1,7 +1,11 @@
package server
import (
"context"
"net/http"
"os"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
@ -95,3 +99,43 @@ func (s *Server) setupAddStrategy(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true})
}
func (s *Server) setupRestart(c *gin.Context) {
if s.srv == nil {
logrus.Error("nil srv")
return
}
go func() {
logrus.Info("shutting down web server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil {
logrus.WithError(err).Error("server forced to shutdown")
}
logrus.Info("web server shutdown completed")
bin := os.Args[0]
args := os.Args[0:]
// filter out setup parameters
args = filterStrings(args, "--setup")
envVars := os.Environ()
logrus.Infof("%s %v %+v", bin, args, envVars)
if err := syscall.Exec(bin, args, envVars); err != nil {
logrus.WithError(err).Errorf("failed to restart %s", bin)
}
s.Setup.Cancel()
}()
c.JSON(http.StatusOK, gin.H{"success": true})
}