@@ -122,6 +122,9 @@ def health_check(self, healthcheck: dict, sqla_helper: SqlaReflectHelper):
122
122
if self .timeouts + params ['timeouts' ] >= healthcheck .get ("unhealthy" , {}).get ("timeouts" , 1 ):
123
123
params ['status' ] = "unhealthy"
124
124
self .set_counts (params , sqla_helper )
125
+ if not success :
126
+ instances = self .get_target_service_all_instance (0 , sqla_helper )
127
+ logger .warning (f"健康检查 { self .target_id } { self .service } { schema } { self .instance } { healthcheck .get ('uri' )} , 实例状态: { json .dumps ([d .to_dict_item () for d in instances ])} " )
125
128
if params ['status' ] != self .status and healthcheck .get ("alert" , {}).get ("url" ):
126
129
instances = self .get_target_service_all_instance (0 , sqla_helper )
127
130
keyfunc = lambda item : item ['status' ]
@@ -154,13 +157,20 @@ def save_or_update(self, discovery_instances: ['Instance'], sqla_helper: SqlaRef
154
157
instances = ss .query (DiscoveryInstance ).filter (and_ (DiscoveryInstance .target_id == self .target_id ,
155
158
DiscoveryInstance .service == self .service )).all ()
156
159
if len (discovery_instances ) == 0 :
160
+ if len (instances ) > 0 :
161
+ delete_instances = [d .instance for d in instances ]
162
+ logger .info (f"删除无效的实例: { json .dumps (delete_instances )} " )
163
+ sql = delete (DiscoveryInstance ).where (DiscoveryInstance .instance .in_ (delete_instances ))
164
+ ss .execute (sql )
165
+ ss .commit ()
157
166
return instances
158
167
tmps = [f"{ d .ip } :{ d .port } " for d in discovery_instances if d .enabled ]
159
168
ins = [d .instance for d in instances ]
160
169
161
170
# 删除无效的
162
171
delete_instances = [d for d in ins if d not in tmps ]
163
172
if delete_instances :
173
+ logger .info (f"删除无效的实例: { json .dumps (delete_instances )} " )
164
174
sql = delete (DiscoveryInstance ).where (DiscoveryInstance .instance .in_ (delete_instances ))
165
175
ss .execute (sql )
166
176
ss .commit ()
@@ -169,6 +179,7 @@ def save_or_update(self, discovery_instances: ['Instance'], sqla_helper: SqlaRef
169
179
{"id" : self .id or str (uuid .uuid4 ()), "target_id" : self .target_id , "service" : self .service ,
170
180
"instance" : d , "create_time" : datetime .now ()}) for d in tmps if d not in ins ]
171
181
if save_instances :
182
+ logger .info (f"新增的实例: { json .dumps ([d .to_dict_item () for d in save_instances ])} " )
172
183
ss .add_all (save_instances )
173
184
ss .commit ()
174
185
return instances
0 commit comments