refinements and fixes

This commit is contained in:
holger krekel 2024-04-03 18:50:20 +02:00
parent cc4920ddc7
commit 9b76d46558
2 changed files with 7 additions and 2 deletions

View File

@ -50,6 +50,7 @@ class PersistentQueueItem:
@classmethod @classmethod
def create(cls, queue_dir, addr, start_ts, token): def create(cls, queue_dir, addr, start_ts, token):
queue_id = uuid4().hex queue_id = uuid4().hex
start_ts = int(start_ts)
path = queue_dir.joinpath(queue_id) path = queue_dir.joinpath(queue_id)
tmp_path = path.with_name(path.name + ".tmp") tmp_path = path.with_name(path.name + ".tmp")
tmp_path.write_text(f"{addr}\n{start_ts}\n{token}") tmp_path.write_text(f"{addr}\n{start_ts}\n{token}")
@ -61,6 +62,9 @@ class PersistentQueueItem:
addr, start_ts, token = path.read_text().split("\n", maxsplit=2) addr, start_ts, token = path.read_text().split("\n", maxsplit=2)
return cls(path, addr, int(start_ts), token) return cls(path, addr, int(start_ts), token)
def __lt__(self, other):
return self.start_ts < other.start_ts
class Notifier: class Notifier:
URL = "https://notifications.delta.chat/notify" URL = "https://notifications.delta.chat/notify"
@ -95,7 +99,7 @@ class Notifier:
def queue_for_retry(self, queue_item, retry_num=0): def queue_for_retry(self, queue_item, retry_num=0):
delay = self.compute_delay(retry_num) delay = self.compute_delay(retry_num)
when = time.time() + delay when = int(time.time()) + delay
deadline = queue_item.start_ts + self.DROP_DEADLINE deadline = queue_item.start_ts + self.DROP_DEADLINE
if retry_num >= len(self.retry_queues) or when > deadline: if retry_num >= len(self.retry_queues) or when > deadline:
queue_item.delete() queue_item.delete()

View File

@ -238,7 +238,7 @@ def test_requeue_removes_tmp_files(notifier, metadata, testaddr, caplog):
assert not p.exists() assert not p.exists()
assert notifier2.retry_queues[0].qsize() == 1 assert notifier2.retry_queues[0].qsize() == 1
when, queue_item = notifier2.retry_queues[0].get() when, queue_item = notifier2.retry_queues[0].get()
assert when >= int(time.time()) assert when <= int(time.time())
assert queue_item.addr == testaddr assert queue_item.addr == testaddr
@ -295,3 +295,4 @@ def test_persistent_queue_items(tmp_path, testaddr, token):
assert item2 == queue_item assert item2 == queue_item
item2.delete() item2.delete()
assert not item2.path.exists() assert not item2.path.exists()
assert not queue_item < item2 and not item2 < queue_item