fix: 修复优雅退出超时问题
This commit is contained in:
@@ -10,6 +10,7 @@ services:
|
|||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
restart: always
|
restart: always
|
||||||
|
stop_grace_period: ${SERVER_GRACEFUL_SHUTDOWN_TIMEOUT:-60}s
|
||||||
depends_on:
|
depends_on:
|
||||||
mysql:
|
mysql:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
@@ -169,10 +169,26 @@ func (a *App) Start() error {
|
|||||||
func (a *App) Stop(ctx context.Context) {
|
func (a *App) Stop(ctx context.Context) {
|
||||||
logrus.Info("Shutting down server...")
|
logrus.Info("Shutting down server...")
|
||||||
|
|
||||||
if err := a.httpServer.Shutdown(ctx); err != nil {
|
serverConfig := a.configManager.GetEffectiveServerConfig()
|
||||||
logrus.Errorf("Server forced to shutdown: %v", err)
|
totalTimeout := time.Duration(serverConfig.GracefulShutdownTimeout) * time.Second
|
||||||
}
|
|
||||||
|
|
||||||
|
// 动态计算 HTTP 关机超时时间,为后台服务固定预留 5 秒
|
||||||
|
httpShutdownTimeout := totalTimeout - 5*time.Second
|
||||||
|
|
||||||
|
// 为 HTTP 服务器的优雅关闭创建一个独立的 context
|
||||||
|
httpShutdownCtx, cancelHttpShutdown := context.WithTimeout(context.Background(), httpShutdownTimeout)
|
||||||
|
defer cancelHttpShutdown()
|
||||||
|
|
||||||
|
logrus.Debugf("Attempting to gracefully shut down HTTP server (max %v)...", httpShutdownTimeout)
|
||||||
|
if err := a.httpServer.Shutdown(httpShutdownCtx); err != nil {
|
||||||
|
logrus.Debugf("HTTP server graceful shutdown timed out as expected, forcing remaining connections to close.")
|
||||||
|
if closeErr := a.httpServer.Close(); closeErr != nil {
|
||||||
|
logrus.Errorf("Error forcing HTTP server to close: %v", closeErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logrus.Info("HTTP server has been shut down.")
|
||||||
|
|
||||||
|
// 使用原始的总超时 context 继续关闭其他后台服务
|
||||||
stoppableServices := []func(context.Context){
|
stoppableServices := []func(context.Context){
|
||||||
a.cronChecker.Stop,
|
a.cronChecker.Stop,
|
||||||
a.leaderLock.Stop,
|
a.leaderLock.Stop,
|
||||||
@@ -192,7 +208,6 @@ func (a *App) Stop(ctx context.Context) {
|
|||||||
}(stopFunc)
|
}(stopFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all services to stop, or for the context to be done.
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
@@ -164,6 +164,12 @@ func (m *Manager) Validate() error {
|
|||||||
validationErrors = append(validationErrors, "AUTH_KEY is required and cannot be empty")
|
validationErrors = append(validationErrors, "AUTH_KEY is required and cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate GracefulShutdownTimeout and reset if necessary
|
||||||
|
if m.config.Server.GracefulShutdownTimeout < 10 {
|
||||||
|
logrus.Warnf("SERVER_GRACEFUL_SHUTDOWN_TIMEOUT value %ds is too short, resetting to minimum 10s.", m.config.Server.GracefulShutdownTimeout)
|
||||||
|
m.config.Server.GracefulShutdownTimeout = 10
|
||||||
|
}
|
||||||
|
|
||||||
if len(validationErrors) > 0 {
|
if len(validationErrors) > 0 {
|
||||||
logrus.Error("Configuration validation failed:")
|
logrus.Error("Configuration validation failed:")
|
||||||
for _, err := range validationErrors {
|
for _, err := range validationErrors {
|
||||||
|
Reference in New Issue
Block a user