@@ -36,6 +36,8 @@ func (self *client) Close() {
3636 if self .dockerClient != nil {
3737 self .dockerClient .Close ()
3838 }
39+ // 因为需要重复的使用监控 client,关掉旧的后,还需要再新建一个新的上下文
40+ self .ctx , self .ctxCancel = context .WithCancel (context .Background ())
3941}
4042
4143type monitor struct {
@@ -90,20 +92,25 @@ func (self *monitor) listen(c *client) {
9092 var initErr error
9193
9294 for {
93- time .Sleep (10 * time .Second )
94- if _ , ok := self .clients .Load (c .dockerEnv .Name ); ! ok {
95- c .Close ()
96- return
97- }
98- c .dockerClient , initErr = docker .NewClientWithDockerEnv (c .dockerEnv )
9995 if initErr != nil {
100- if os .Getenv ("APP_ENV" ) == "debug" {
101- slog .Debug ("Monitor start" , "name" , c .dockerEnv .Name , "error" , initErr )
102- }
10396 facade .GetEvent ().Publish (event .DockerStopEvent , event.DockerPayload {
10497 DockerEnv : c .dockerEnv ,
10598 Error : initErr ,
10699 })
100+ }
101+ time .Sleep (10 * time .Second )
102+ if _ , ok := self .clients .Load (c .dockerEnv .Name ); ! ok {
103+ slog .Debug ("Monitor client not found" , "name" , c .dockerEnv .Name , "error" , initErr )
104+ c .Close ()
105+ return
106+ }
107+
108+ if os .Getenv ("APP_ENV" ) == "debug" {
109+ slog .Debug ("Monitor start" , "name" , c .dockerEnv .Name , "error" , initErr )
110+ }
111+
112+ if c .dockerClient , initErr = docker .NewClientWithDockerEnv (c .dockerEnv ); initErr != nil {
113+ c .Close ()
107114 continue
108115 }
109116
@@ -124,7 +131,7 @@ func (self *monitor) listen(c *client) {
124131 case <- c .ctx .Done ():
125132 slog .Debug ("Monitor closed by monitor" , "name" , c .dockerEnv .Name )
126133 c .Close ()
127- return
134+ break eventLoop
128135 case <- self .ctx .Done ():
129136 slog .Debug ("Monitor closed" )
130137 self .Close ()
0 commit comments