From c78fe4f9f1bc7e4b505618ebfbdfad15f9433fd5 Mon Sep 17 00:00:00 2001 From: lalanza808 Date: Mon, 12 Sep 2022 11:04:16 -0700 Subject: [PATCH] Automatically remove dead nodes (#18) * remove nodes if all the health checks are failed * use utc * node should have at least 5 health checks --- xmrnodes/app.py | 7 ++++++- xmrnodes/models.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/xmrnodes/app.py b/xmrnodes/app.py index 70a34ae..3b38539 100644 --- a/xmrnodes/app.py +++ b/xmrnodes/app.py @@ -157,7 +157,7 @@ def add(): return redirect("/") def cleanup_health_checks(): - diff = datetime.now() - timedelta(hours=24) + diff = datetime.utcnow() - timedelta(hours=24) checks = HealthCheck.select().where(HealthCheck.datetime <= diff) for check in checks: print("Deleting check", check.id) @@ -201,6 +201,11 @@ def check(): node.datetime_checked = now node.save() hc.save() + if node.get_failed_checks().count() == node.get_all_checks().count() and node.get_all_checks().count() > 5: + print('this node fails all of its health checks - deleting it!') + for _hc in node.get_all_checks(): + _hc.delete_instance() + node.delete_instance() @app.cli.command("get_peers") def get_peers(): diff --git a/xmrnodes/models.py b/xmrnodes/models.py index 8cb5ca7..32fb6bd 100644 --- a/xmrnodes/models.py +++ b/xmrnodes/models.py @@ -23,6 +23,14 @@ class Node(Model): datetime_failed = DateTimeField(default=None, null=True) fail_reason = CharField(null=True) + def get_failed_checks(self): + hcs = HealthCheck.select().where(HealthCheck.node == self, HealthCheck.health == False) + return hcs + + def get_all_checks(self): + hcs = HealthCheck.select().where(HealthCheck.node == self) + return hcs + class Meta: database = db