Moves appointment deletion + db update to the cleaner

This commit is contained in:
Sergi Delgado Segura
2019-11-07 16:36:55 +00:00
parent 82f9de1717
commit 8b2f0efa2f
2 changed files with 24 additions and 17 deletions

View File

@@ -25,6 +25,24 @@ class Cleaner:
# Delete appointment from the db # Delete appointment from the db
db_manager.delete_watcher_appointment(uuid) 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 @staticmethod
def delete_completed_jobs(jobs, tx_job_map, completed_jobs, height, db_manager): def delete_completed_jobs(jobs, tx_job_map, completed_jobs, height, db_manager):
for uuid, confirmations in completed_jobs: for uuid, confirmations in completed_jobs:

View File

@@ -18,7 +18,7 @@ logger = Logger("Watcher")
class 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.appointments = dict()
self.locator_uuid_map = dict() self.locator_uuid_map = dict()
self.asleep = True self.asleep = True
@@ -30,7 +30,7 @@ class Watcher:
if not isinstance(responder, Responder): if not isinstance(responder, Responder):
self.responder = Responder(db_manager) 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") raise ValueError("No signing key provided. Please fix your pisa.conf")
else: else:
with open(PISA_SECRET_KEY, "r") as key_file: with open(PISA_SECRET_KEY, "r") as key_file:
@@ -137,21 +137,10 @@ class Watcher:
block_hash, block_hash,
) )
# Delete the appointment # Delete the appointment and update db
appointment = self.appointments.pop(uuid) Cleaner.delete_complete_appointment(
self.appointments, self.locator_uuid_map, locator, uuid, self.db_manager
# 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())
# Register the last processed block for the watcher # Register the last processed block for the watcher
self.db_manager.store_last_block_hash_watcher(block_hash) self.db_manager.store_last_block_hash_watcher(block_hash)