@@ -663,34 +663,84 @@ func (s *Service) ControlTunnel(req TunnelActionRequest) error {
663663 return err
664664 }
665665
666- // 目标状态映射
667- var targetStatus TunnelStatus
668- switch req .Action {
669- case "start" , "restart" :
670- targetStatus = StatusRunning
671- case "stop" :
672- targetStatus = StatusStopped
673- default :
674- targetStatus = "" // 不会发生,已验证
675- }
666+ // 重启操作需要特殊处理:先监听stopped,再监听running
667+ if req .Action == "restart" {
668+ log .Infof ("[API] 重启隧道 %s: 开始监听状态变化" , req .InstanceID )
669+
670+ // 第一阶段:等待状态变为 stopped(最多5秒)
671+ log .Infof ("[API] 重启隧道 %s: 等待停止状态" , req .InstanceID )
672+ stoppedDeadline := time .Now ().Add (5 * time .Second )
673+ stoppedDetected := false
674+
675+ for time .Now ().Before (stoppedDeadline ) {
676+ var curStatus string
677+ if err := s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& curStatus ); err == nil {
678+ if TunnelStatus (curStatus ) == StatusStopped {
679+ log .Infof ("[API] 重启隧道 %s: 检测到停止状态" , req .InstanceID )
680+ stoppedDetected = true
681+ break
682+ }
683+ }
684+ time .Sleep (200 * time .Millisecond )
685+ }
676686
677- // 轮询数据库等待状态变更 (最多8秒)
678- deadline := time .Now ().Add (3 * time .Second )
679- for time .Now ().Before (deadline ) {
680- var curStatus string
681- if err := s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& curStatus ); err == nil {
682- if TunnelStatus (curStatus ) == targetStatus {
683- break // 成功
687+ if ! stoppedDetected {
688+ log .Warnf ("[API] 重启隧道 %s: 未检测到停止状态,继续等待启动" , req .InstanceID )
689+ }
690+
691+ // 第二阶段:等待状态变为 running(最多5秒)
692+ log .Infof ("[API] 重启隧道 %s: 等待运行状态" , req .InstanceID )
693+ runningDeadline := time .Now ().Add (5 * time .Second )
694+ runningDetected := false
695+
696+ for time .Now ().Before (runningDeadline ) {
697+ var curStatus string
698+ if err := s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& curStatus ); err == nil {
699+ if TunnelStatus (curStatus ) == StatusRunning {
700+ log .Infof ("[API] 重启隧道 %s: 检测到运行状态,重启完成" , req .InstanceID )
701+ runningDetected = true
702+ break
703+ }
684704 }
705+ time .Sleep (200 * time .Millisecond )
685706 }
686- time .Sleep (200 * time .Millisecond )
687- }
688707
689- // 再次检查,若仍未到目标状态则手动更新
690- var finalStatus string
691- _ = s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& finalStatus )
692- if TunnelStatus (finalStatus ) != targetStatus {
693- _ = s .UpdateTunnelStatus (req .InstanceID , targetStatus )
708+ // 如果未检测到运行状态,手动更新
709+ if ! runningDetected {
710+ log .Warnf ("[API] 重启隧道 %s: 未检测到运行状态,手动更新状态" , req .InstanceID )
711+ _ = s .UpdateTunnelStatus (req .InstanceID , StatusRunning )
712+ }
713+
714+ } else {
715+ // start 和 stop 操作使用原有的简单轮询逻辑
716+ var targetStatus TunnelStatus
717+ switch req .Action {
718+ case "start" :
719+ targetStatus = StatusRunning
720+ case "stop" :
721+ targetStatus = StatusStopped
722+ default :
723+ targetStatus = "" // 不会发生,已验证
724+ }
725+
726+ // 轮询数据库等待状态变更 (最多3秒)
727+ deadline := time .Now ().Add (3 * time .Second )
728+ for time .Now ().Before (deadline ) {
729+ var curStatus string
730+ if err := s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& curStatus ); err == nil {
731+ if TunnelStatus (curStatus ) == targetStatus {
732+ break // 成功
733+ }
734+ }
735+ time .Sleep (200 * time .Millisecond )
736+ }
737+
738+ // 再次检查,若仍未到目标状态则手动更新
739+ var finalStatus string
740+ _ = s .db .QueryRow (`SELECT status FROM "Tunnel" WHERE instanceId = ?` , req .InstanceID ).Scan (& finalStatus )
741+ if TunnelStatus (finalStatus ) != targetStatus {
742+ _ = s .UpdateTunnelStatus (req .InstanceID , targetStatus )
743+ }
694744 }
695745
696746 // 记录操作日志
0 commit comments