From bc5f09000a3d807af8966ea39b94cb594532caf5 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 23 Apr 2020 16:30:05 +0200 Subject: [PATCH] plugin - adds tower state to TowerInfo The state flags whether the tower is reachable or not so the user can be aware of it --- watchtower-plugin/tower_info.py | 10 ++++++---- watchtower-plugin/watchtower.py | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/watchtower-plugin/tower_info.py b/watchtower-plugin/tower_info.py index 4ae5587..9eab281 100644 --- a/watchtower-plugin/tower_info.py +++ b/watchtower-plugin/tower_info.py @@ -1,7 +1,8 @@ class TowerInfo: - def __init__(self, netaddr, available_slots, appointments=None): + def __init__(self, netaddr, available_slots, status="active", appointments=None): self.netaddr = netaddr self.available_slots = available_slots + self.status = status if not appointments: self.appointments = {} @@ -12,15 +13,16 @@ class TowerInfo: def from_dict(cls, tower_data): netaddr = tower_data.get("netaddr") available_slots = tower_data.get("available_slots") + status = tower_data.get("status") appointments = tower_data.get("appointments") - if any(v is None for v in [netaddr, available_slots, appointments]): + if any(v is None for v in [netaddr, available_slots, status, appointments]): raise ValueError("Wrong appointment data, some fields are missing") - return cls(netaddr, available_slots, appointments) + return cls(netaddr, available_slots, status, appointments) def to_dict(self): return self.__dict__ def get_summary(self): - return {"netaddr": self.netaddr, "available_slots": self.available_slots} + return {"netaddr": self.netaddr, "status": self.status, "available_slots": self.available_slots} diff --git a/watchtower-plugin/watchtower.py b/watchtower-plugin/watchtower.py index 75b9bf9..c24af5e 100755 --- a/watchtower-plugin/watchtower.py +++ b/watchtower-plugin/watchtower.py @@ -184,6 +184,7 @@ def add_appointment(plugin, **kwargs): # Send appointment to the server. # FIXME: sending the appointment to all registered towers atm. Some management would be nice. for tower_id, tower in plugin.wt_client.towers.items(): + tower_info = TowerInfo.from_dict(plugin.wt_client.db_manager.load_tower_record(tower_id)) try: plugin.log("Sending appointment to the Eye of Satoshi at {}".format(tower.get("netaddr"))) add_appointment_endpoint = "{}/add_appointment".format(tower.get("netaddr")) @@ -204,18 +205,24 @@ def add_appointment(plugin, **kwargs): # TODO: Not storing the whole appointments for now. The node can recreate all the data if needed. # DISCUSS: It may be worth checking that the available slots match instead of blindly trusting. - # Update TowersDB - tower_info = TowerInfo.from_dict(plugin.wt_client.db_manager.load_tower_record(tower_id)) tower_info.appointments[appointment.locator] = signature tower_info.available_slots = response.get("available_slots") - plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info) + tower_info.status = "reachable" - # Update memory - plugin.wt_client.towers[tower_id]["available_slots"] = response.get("available_slots") + # Update memory and TowersDB + plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info) + plugin.wt_client.towers[tower_id] = tower_info.get_summary() except TowerConnectionError as e: # TODO: Implement retry logic plugin.log(str(e)) + if e.kwargs.get("transitory"): + tower_info.status = "temporarily unreachable" + else: + tower_info.status = "unreachable" + + plugin.wt_client.towers[tower_id] = tower_info.get_summary() + plugin.wt_client.db_manager.store_tower_record(tower_id, tower_info) except TowerResponseError as e: plugin.log(str(e))