Skip to content

Commit ddc8caf

Browse files
committed
refactor(scheduler): 优化调度器停止逻辑与WebSocket连接管理
- 在调度器停止时,增加对WebSocket客户端连接的关闭处理,并记录相关日志 - 修改停止调度器的日志信息,提供更清晰的状态反馈 - 在启动调度器时,使用goroutine以实现非阻塞的调度器启动 - 增加对调度器停止的超时处理,确保在超时情况下强制退出
1 parent a5164bb commit ddc8caf

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

cmd/start.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/signal"
88
"path/filepath"
99
"syscall"
10+
"time"
1011

1112
"github.com/https-cert/deploy/internal/config"
1213
"github.com/https-cert/deploy/internal/scheduler"
@@ -41,15 +42,28 @@ func CreateStartCmd() *cobra.Command {
4142
ctx, cancel := context.WithCancel(context.Background())
4243
defer cancel()
4344

44-
scheduler.Start(ctx)
45+
// 在 goroutine 中启动调度器
46+
done := make(chan struct{})
47+
go func() {
48+
scheduler.Start(ctx)
49+
close(done)
50+
}()
4551

4652
sigChan := make(chan os.Signal, 1)
4753
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
4854

4955
<-sigChan
50-
logger.Info("停止中...")
56+
logger.Info("收到停止信号,正在关闭...")
5157
cancel()
52-
logger.Info("已停止")
58+
59+
// 等待调度器完全停止,最多等待 10 秒
60+
select {
61+
case <-done:
62+
logger.Info("已停止")
63+
case <-time.After(10 * time.Second):
64+
logger.Warn("停止超时,强制退出")
65+
}
66+
5367
return nil
5468
},
5569
}

internal/client/ws_connection.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ func (c *WSClient) StartWSNotify() {
154154
busyOps := c.busyOperations.Load()
155155
if busyOps > 0 {
156156
logger.Warn("WebSocket连接意外断开(有业务正在执行)", "error", err, "busyOps", busyOps)
157+
} else {
158+
logger.Info("WebSocket连接断开", "error", err)
157159
}
158160

159161
isConnected.Store(false)

internal/client/ws_sender.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func (c *WSClient) sendNotifyRequest(req *deployPB.NotifyRequest) error {
2929
}
3030

3131
// 发送 JSON 消息(WebSocket Text 消息)
32-
ctx, cancel := context.WithTimeout(c.ctx, 10*time.Second)
32+
// 使用较长的超时时间,避免网络慢时误判为失败
33+
ctx, cancel := context.WithTimeout(c.ctx, 30*time.Second)
3334
defer cancel()
3435

3536
return conn.Write(ctx, websocket.MessageText, data)
@@ -85,8 +86,8 @@ func (c *WSClient) sendHeartbeat(ctx context.Context) {
8586
// 获取系统信息用于心跳
8687
systemInfo, err := c.getSystemInfo()
8788
if err != nil {
88-
logger.Warn("获取系统信息失败", "error", err)
89-
return
89+
logger.Warn("获取系统信息失败,跳过本次心跳", "error", err)
90+
continue // 跳过本次心跳,不要退出整个心跳循环
9091
}
9192

9293
// 发送心跳消息(使用 RegisterResponse 格式)

internal/scheduler/scheduler.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,32 @@ func Start(ctx context.Context) {
7777

7878
// stop 停止调度器
7979
func (s *Scheduler) stop() {
80+
logger.Info("正在停止调度器...")
81+
8082
if s.ticker != nil {
8183
s.ticker.Stop()
8284
}
8385

86+
// 关闭 WebSocket 客户端连接
87+
if s.client != nil {
88+
if err := s.client.Close(); err != nil {
89+
logger.Error("关闭 WebSocket 客户端失败", "error", err)
90+
} else {
91+
logger.Info("WebSocket 客户端已关闭")
92+
}
93+
}
94+
8495
// 停止 HTTP 服务器
8596
if s.httpServer != nil {
8697
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
8798
defer cancel()
8899

89100
if err := s.httpServer.Stop(ctx); err != nil {
90101
logger.Error("停止 HTTP-01 验证服务失败", "error", err)
102+
} else {
103+
logger.Info("HTTP-01 验证服务已停止")
91104
}
92105
}
106+
107+
logger.Info("调度器已停止")
93108
}

0 commit comments

Comments
 (0)