Skip to content

Commit

Permalink
优化健康检查日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
anjia0532 committed Jul 15, 2024
1 parent dc4447b commit 83577d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gateway等网关插件的高扩展性
### 通过docker运行

```bash
docker run anjia0532/discovery-syncer-python:v2.6.2
docker run anjia0532/discovery-syncer-python:v2.6.3
```

特别的,`-c ` 支持配置远端http[s]的地址,比如读取静态资源的,比如读取nacos的
Expand Down
11 changes: 11 additions & 0 deletions app/model/syncer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def health_check(self, healthcheck: dict, sqla_helper: SqlaReflectHelper):
if self.timeouts + params['timeouts'] >= healthcheck.get("unhealthy", {}).get("timeouts", 1):
params['status'] = "unhealthy"
self.set_counts(params, sqla_helper)
if not success:
instances = self.get_target_service_all_instance(0, sqla_helper)
logger.warning(f"健康检查 {self.target_id} {self.service} {schema}{self.instance}{healthcheck.get('uri')} , 实例状态: {json.dumps([d.to_dict_item() for d in instances])}")
if params['status'] != self.status and healthcheck.get("alert", {}).get("url"):
instances = self.get_target_service_all_instance(0, sqla_helper)
keyfunc = lambda item: item['status']
Expand Down Expand Up @@ -154,13 +157,20 @@ def save_or_update(self, discovery_instances: ['Instance'], sqla_helper: SqlaRef
instances = ss.query(DiscoveryInstance).filter(and_(DiscoveryInstance.target_id == self.target_id,
DiscoveryInstance.service == self.service)).all()
if len(discovery_instances) == 0:
if len(instances) > 0:
delete_instances = [d.instance for d in instances]
logger.info(f"删除无效的实例: {json.dumps(delete_instances)}")
sql = delete(DiscoveryInstance).where(DiscoveryInstance.instance.in_(delete_instances))
ss.execute(sql)
ss.commit()
return instances
tmps = [f"{d.ip}:{d.port}" for d in discovery_instances if d.enabled]
ins = [d.instance for d in instances]

# 删除无效的
delete_instances = [d for d in ins if d not in tmps]
if delete_instances:
logger.info(f"删除无效的实例: {json.dumps(delete_instances)}")
sql = delete(DiscoveryInstance).where(DiscoveryInstance.instance.in_(delete_instances))
ss.execute(sql)
ss.commit()
Expand All @@ -169,6 +179,7 @@ def save_or_update(self, discovery_instances: ['Instance'], sqla_helper: SqlaRef
{"id": self.id or str(uuid.uuid4()), "target_id": self.target_id, "service": self.service,
"instance": d, "create_time": datetime.now()}) for d in tmps if d not in ins]
if save_instances:
logger.info(f"新增的实例: {json.dumps([d.to_dict_item() for d in save_instances])}")
ss.add_all(save_instances)
ss.commit()
return instances
Expand Down
1 change: 1 addition & 0 deletions app/tasks/task_syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def health_check(target: dict):
t.join()
logger.info(f"健康检查完成, target: {target}")


@boost(boost_params=FunboostCommonConfig(queue_name='queue_syncer_job', qps=50, ))
def syncer(target: dict):
"""
Expand Down

0 comments on commit 83577d7

Please sign in to comment.