diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index 21f1054..f5e93e2 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -75,11 +75,14 @@ class Notifier: when += pow(self.NOTIFICATION_RETRY_DELAY, numtries) self.retry_queues[numtries].put((when, token)) - def start_notification_threads(self): + def requeue_persistent_pending_tokens(self): for token_path in self.notification_dir.iterdir(): self.add_token_for_retry(token_path.name) - # we start a thread for each retry-queue bucket + def start_notification_threads(self): + self.requeue_persistent_pending_tokens() + + # start a thread for each retry-queue bucket for numtries in range(len(self.retry_queues)): t = Thread(target=self.thread_retry_loop, args=(numtries,)) t.setDaemon(True) @@ -99,6 +102,7 @@ class Notifier: self.notify_one(requests_session, token, numtries) def notify_one(self, requests_session, token, numtries=0): + token_path = self.notification_dir.joinpath(token) try: response = requests_session.post( "https://notifications.delta.chat/notify", @@ -109,15 +113,12 @@ class Notifier: response = e else: if response.status_code in (200, 410): - token_path = self.notification_dir.joinpath(token) if response.status_code == 410: # 410 Gone: means the token is no longer valid. try: addr = token_path.read_text() except FileNotFoundError: - logging.warning( - "could not determine address for token %r:", token - ) + logging.warning("no address for token %r:", token) return self.remove_token(addr, token) token_path.unlink(missing_ok=True) @@ -128,6 +129,7 @@ class Notifier: if numtries < self.MAX_NUMBER_OF_TRIES: self.add_token_for_retry(token, numtries=numtries) else: + token_path.unlink(missing_ok=True) logging.warning( "giving up on token after %d tries: %r", numtries - 1, token ) diff --git a/chatmaild/src/chatmaild/tests/test_metadata.py b/chatmaild/src/chatmaild/tests/test_metadata.py index 3f7a55f..ebbefaa 100644 --- a/chatmaild/src/chatmaild/tests/test_metadata.py +++ b/chatmaild/src/chatmaild/tests/test_metadata.py @@ -177,6 +177,8 @@ def test_notifier_thread_firstrun(notifier, testaddr): url, data, timeout = reqmock.requests[0] assert data == "01234" assert notifier.get_tokens(testaddr) == ["01234"] + notifier.requeue_persistent_pending_tokens() + assert notifier.retry_queues[0].qsize() == 0 def test_notifier_thread_run(notifier, testaddr): @@ -187,6 +189,8 @@ def test_notifier_thread_run(notifier, testaddr): url, data, timeout = reqmock.requests[0] assert data == "01234" assert notifier.get_tokens(testaddr) == ["01234"] + notifier.requeue_persistent_pending_tokens() + assert notifier.retry_queues[0].qsize() == 0 @pytest.mark.parametrize("status", [requests.exceptions.RequestException(), 404, 500]) @@ -210,6 +214,8 @@ def test_notifier_thread_connection_failures(notifier, testaddr, status, caplog) else: assert len(caplog.records) == 2 assert "giving up" in caplog.records[1].msg + notifier.requeue_persistent_pending_tokens() + assert notifier.retry_queues[0].qsize() == 0 def test_multi_device_notifier(notifier, testaddr):