plugin - adds tower state to TowerInfo

The state flags whether the tower is reachable or not so the user can be aware of it
This commit is contained in:
Sergi Delgado Segura
2020-04-23 16:30:05 +02:00
parent 7b3bc6e6a8
commit bc5f09000a
2 changed files with 18 additions and 9 deletions

View File

@@ -1,7 +1,8 @@
class TowerInfo: class TowerInfo:
def __init__(self, netaddr, available_slots, appointments=None): def __init__(self, netaddr, available_slots, status="active", appointments=None):
self.netaddr = netaddr self.netaddr = netaddr
self.available_slots = available_slots self.available_slots = available_slots
self.status = status
if not appointments: if not appointments:
self.appointments = {} self.appointments = {}
@@ -12,15 +13,16 @@ class TowerInfo:
def from_dict(cls, tower_data): def from_dict(cls, tower_data):
netaddr = tower_data.get("netaddr") netaddr = tower_data.get("netaddr")
available_slots = tower_data.get("available_slots") available_slots = tower_data.get("available_slots")
status = tower_data.get("status")
appointments = tower_data.get("appointments") 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") 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): def to_dict(self):
return self.__dict__ return self.__dict__
def get_summary(self): 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}

View File

@@ -184,6 +184,7 @@ def add_appointment(plugin, **kwargs):
# Send appointment to the server. # Send appointment to the server.
# FIXME: sending the appointment to all registered towers atm. Some management would be nice. # FIXME: sending the appointment to all registered towers atm. Some management would be nice.
for tower_id, tower in plugin.wt_client.towers.items(): 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: try:
plugin.log("Sending appointment to the Eye of Satoshi at {}".format(tower.get("netaddr"))) plugin.log("Sending appointment to the Eye of Satoshi at {}".format(tower.get("netaddr")))
add_appointment_endpoint = "{}/add_appointment".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. # 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. # 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.appointments[appointment.locator] = signature
tower_info.available_slots = response.get("available_slots") 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 # Update memory and TowersDB
plugin.wt_client.towers[tower_id]["available_slots"] = response.get("available_slots") 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: except TowerConnectionError as e:
# TODO: Implement retry logic # TODO: Implement retry logic
plugin.log(str(e)) 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: except TowerResponseError as e:
plugin.log(str(e)) plugin.log(str(e))