@@ -569,102 +569,95 @@ func (m *Master) Shutdown(ctx context.Context) error {
569569
570570// startPeriodicTasks 启动所有定期任务
571571func (m * Master ) startPeriodicTasks () {
572- go m .startPeriodicBackup ()
573- go m .startPeriodicCleanup ()
574- go m .startPeriodicRestart ()
575- }
572+ ticker := time .NewTicker (ReloadInterval )
573+ defer ticker .Stop ()
576574
577- // startPeriodicBackup 启动定期备份
578- func (m * Master ) startPeriodicBackup () {
579575 for {
580576 select {
581- case <- time .After (ReloadInterval ):
582- // 固定备份文件名
583- backupPath := fmt .Sprintf ("%s.backup" , m .statePath )
584-
585- if err := m .saveStateToPath (backupPath ); err != nil {
586- m .logger .Error ("startPeriodicBackup: backup state failed: %v" , err )
587- } else {
588- m .logger .Info ("State backup saved: %v" , backupPath )
589- }
577+ case <- ticker .C :
578+ // 执行定期备份
579+ m .performPeriodicBackup ()
580+ // 执行定期清理
581+ m .performPeriodicCleanup ()
582+ // 执行定期重启
583+ m .performPeriodicRestart ()
590584 case <- m .periodicDone :
585+ ticker .Stop ()
591586 return
592587 }
593588 }
594589}
595590
596- // startPeriodicCleanup 启动定期清理重复ID的实例
597- func (m * Master ) startPeriodicCleanup () {
598- for {
599- select {
600- case <- time .After (ReloadInterval ):
601- // 收集实例并按ID分组
602- idInstances := make (map [string ][]* Instance )
603- m .instances .Range (func (key , value any ) bool {
604- if id := key .(string ); id != apiKeyID {
605- idInstances [id ] = append (idInstances [id ], value .(* Instance ))
606- }
607- return true
608- })
591+ // performPeriodicBackup 定期备份任务
592+ func (m * Master ) performPeriodicBackup () {
593+ // 固定备份文件名
594+ backupPath := fmt .Sprintf ("%s.backup" , m .statePath )
609595
610- // 清理重复实例
611- for _ , instances := range idInstances {
612- if len (instances ) <= 1 {
613- continue
614- }
596+ if err := m .saveStateToPath (backupPath ); err != nil {
597+ m .logger .Error ("performPeriodicBackup: backup state failed: %v" , err )
598+ } else {
599+ m .logger .Info ("State backup saved: %v" , backupPath )
600+ }
601+ }
615602
616- // 选择保留实例
617- keepIdx := 0
618- for i , inst := range instances {
619- if inst .Status == "running" && instances [keepIdx ].Status != "running" {
620- keepIdx = i
621- }
622- }
603+ // performPeriodicCleanup 定期清理重复ID的实例
604+ func (m * Master ) performPeriodicCleanup () {
605+ // 收集实例并按ID分组
606+ idInstances := make (map [string ][]* Instance )
607+ m .instances .Range (func (key , value any ) bool {
608+ if id := key .(string ); id != apiKeyID {
609+ idInstances [id ] = append (idInstances [id ], value .(* Instance ))
610+ }
611+ return true
612+ })
623613
624- // 清理多余实例
625- for i , inst := range instances {
626- if i == keepIdx {
627- continue
628- }
629- inst . deleted = true
630- if inst . Status != "stopped" {
631- m . stopInstance ( inst )
632- }
633- m . instances . Delete ( inst . ID )
634- }
614+ // 清理重复实例
615+ for _ , instances := range idInstances {
616+ if len ( instances ) <= 1 {
617+ continue
618+ }
619+
620+ // 选择保留实例
621+ keepIdx := 0
622+ for i , inst := range instances {
623+ if inst . Status == "running" && instances [ keepIdx ]. Status != "running" {
624+ keepIdx = i
635625 }
636- case <- m .periodicDone :
637- return
626+ }
627+
628+ // 清理多余实例
629+ for i , inst := range instances {
630+ if i == keepIdx {
631+ continue
632+ }
633+ inst .deleted = true
634+ if inst .Status != "stopped" {
635+ m .stopInstance (inst )
636+ }
637+ m .instances .Delete (inst .ID )
638638 }
639639 }
640640}
641641
642- // startPeriodicRestart 启动定期错误实例重启
643- func (m * Master ) startPeriodicRestart () {
644- for {
645- select {
646- case <- time .After (ReloadInterval ):
647- // 收集所有error状态的实例
648- var errorInstances []* Instance
649- m .instances .Range (func (key , value any ) bool {
650- if id := key .(string ); id != apiKeyID {
651- instance := value .(* Instance )
652- if instance .Status == "error" && ! instance .deleted {
653- errorInstances = append (errorInstances , instance )
654- }
655- }
656- return true
657- })
658-
659- // 重启所有error状态的实例
660- for _ , instance := range errorInstances {
661- m .stopInstance (instance )
662- time .Sleep (baseDuration )
663- m .startInstance (instance )
642+ // performPeriodicRestart 定期错误实例重启
643+ func (m * Master ) performPeriodicRestart () {
644+ // 收集所有error状态的实例
645+ var errorInstances []* Instance
646+ m .instances .Range (func (key , value any ) bool {
647+ if id := key .(string ); id != apiKeyID {
648+ instance := value .(* Instance )
649+ if instance .Status == "error" && ! instance .deleted {
650+ errorInstances = append (errorInstances , instance )
664651 }
665- case <- m .periodicDone :
666- return
667652 }
653+ return true
654+ })
655+
656+ // 重启所有error状态的实例
657+ for _ , instance := range errorInstances {
658+ m .stopInstance (instance )
659+ time .Sleep (baseDuration )
660+ m .startInstance (instance )
668661 }
669662}
670663
0 commit comments