From 8b2f0efa2fa17e91ecdf619dd0571645acd697b6 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 7 Nov 2019 16:36:55 +0000 Subject: [PATCH] Moves appointment deletion + db update to the cleaner --- pisa/cleaner.py | 18 ++++++++++++++++++ pisa/watcher.py | 23 ++++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/pisa/cleaner.py b/pisa/cleaner.py index 5ad772c..33af54a 100644 --- a/pisa/cleaner.py +++ b/pisa/cleaner.py @@ -25,6 +25,24 @@ class Cleaner: # Delete appointment from the db db_manager.delete_watcher_appointment(uuid) + @staticmethod + def delete_complete_appointment(appointments, locator_uuid_map, locator, uuid, db_manager): + # Delete the appointment + appointment = appointments.pop(uuid) + + # If there was only one appointment that matches the locator we can delete the whole list + if len(locator_uuid_map[locator]) == 1: + locator_uuid_map.pop(locator) + else: + # Otherwise we just delete the appointment that matches locator:appointment_pos + locator_uuid_map[locator].remove(uuid) + + # DISCUSS: instead of deleting the appointment, we will mark it as triggered and delete it from both + # the watcher's and responder's db after fulfilled + # Update appointment in the db + appointment.triggered = True + db_manager.store_watcher_appointment(uuid, appointment.to_json()) + @staticmethod def delete_completed_jobs(jobs, tx_job_map, completed_jobs, height, db_manager): for uuid, confirmations in completed_jobs: diff --git a/pisa/watcher.py b/pisa/watcher.py index 3db1ce7..03da907 100644 --- a/pisa/watcher.py +++ b/pisa/watcher.py @@ -18,7 +18,7 @@ logger = Logger("Watcher") class Watcher: - def __init__(self, db_manager, responder=None, max_appointments=MAX_APPOINTMENTS): + def __init__(self, db_manager, pisa_sk_file=PISA_SECRET_KEY, responder=None, max_appointments=MAX_APPOINTMENTS): self.appointments = dict() self.locator_uuid_map = dict() self.asleep = True @@ -30,7 +30,7 @@ class Watcher: if not isinstance(responder, Responder): self.responder = Responder(db_manager) - if PISA_SECRET_KEY is None: + if pisa_sk_file is None: raise ValueError("No signing key provided. Please fix your pisa.conf") else: with open(PISA_SECRET_KEY, "r") as key_file: @@ -137,21 +137,10 @@ class Watcher: block_hash, ) - # Delete the appointment - appointment = self.appointments.pop(uuid) - - # If there was only one appointment that matches the locator we can delete the whole list - if len(self.locator_uuid_map[locator]) == 1: - self.locator_uuid_map.pop(locator) - else: - # Otherwise we just delete the appointment that matches locator:appointment_pos - self.locator_uuid_map[locator].remove(uuid) - - # DISCUSS: instead of deleting the appointment, we will mark it as triggered and delete it from both - # the watcher's and responder's db after fulfilled - # Update appointment in the db - appointment.triggered = True - self.db_manager.store_watcher_appointment(uuid, appointment.to_json()) + # Delete the appointment and update db + Cleaner.delete_complete_appointment( + self.appointments, self.locator_uuid_map, locator, uuid, self.db_manager + ) # Register the last processed block for the watcher self.db_manager.store_last_block_hash_watcher(block_hash)