better preserve notification order, using a queue again
This commit is contained in:
parent
75cc3fdab0
commit
459ffcabd6
@ -1,5 +1,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from threading import Thread, Event
|
from threading import Thread
|
||||||
|
from queue import Queue
|
||||||
from socketserver import (
|
from socketserver import (
|
||||||
UnixStreamServer,
|
UnixStreamServer,
|
||||||
StreamRequestHandler,
|
StreamRequestHandler,
|
||||||
@ -32,7 +33,7 @@ class Notifier:
|
|||||||
self.notification_dir = vmail_dir / "pending_notifications"
|
self.notification_dir = vmail_dir / "pending_notifications"
|
||||||
if not self.notification_dir.exists():
|
if not self.notification_dir.exists():
|
||||||
self.notification_dir.mkdir()
|
self.notification_dir.mkdir()
|
||||||
self.message_arrived_event = Event()
|
self.notification_queue = Queue()
|
||||||
|
|
||||||
def get_metadata_dict(self, addr):
|
def get_metadata_dict(self, addr):
|
||||||
return FileDict(self.vmail_dir / addr / "metadata.json")
|
return FileDict(self.vmail_dir / addr / "metadata.json")
|
||||||
@ -58,20 +59,26 @@ class Notifier:
|
|||||||
|
|
||||||
def new_message_for_addr(self, addr):
|
def new_message_for_addr(self, addr):
|
||||||
self.notification_dir.joinpath(addr).touch()
|
self.notification_dir.joinpath(addr).touch()
|
||||||
self.message_arrived_event.set()
|
self.notification_queue.put(addr)
|
||||||
|
|
||||||
def thread_run_loop(self):
|
def thread_run_loop(self):
|
||||||
requests_session = requests.Session()
|
requests_session = requests.Session()
|
||||||
|
# on startup deliver all persisted notifications from last process run
|
||||||
|
self.notification_queue.put(None)
|
||||||
while 1:
|
while 1:
|
||||||
self.message_arrived_event.wait()
|
|
||||||
self.message_arrived_event.clear()
|
|
||||||
self.thread_run_one(requests_session)
|
self.thread_run_one(requests_session)
|
||||||
|
|
||||||
def thread_run_one(self, requests_session):
|
def thread_run_one(self, requests_session):
|
||||||
|
addr = self.notification_queue.get()
|
||||||
|
if addr is None:
|
||||||
|
# startup, notify any "pending" notifications from last run
|
||||||
for addr_path in self.notification_dir.iterdir():
|
for addr_path in self.notification_dir.iterdir():
|
||||||
addr = addr_path.name
|
if "@" in addr_path.name:
|
||||||
if "@" not in addr:
|
self.notify_tokens_for(requests_session, addr_path.name)
|
||||||
continue
|
else:
|
||||||
|
self.notify_tokens_for(requests_session, addr)
|
||||||
|
|
||||||
|
def notify_tokens_for(self, requests_session, addr):
|
||||||
for token in self.get_tokens(addr):
|
for token in self.get_tokens(addr):
|
||||||
response = requests_session.post(
|
response = requests_session.post(
|
||||||
"https://notifications.delta.chat/notify",
|
"https://notifications.delta.chat/notify",
|
||||||
@ -82,7 +89,7 @@ class Notifier:
|
|||||||
# 410 Gone status code
|
# 410 Gone status code
|
||||||
# means the token is no longer valid.
|
# means the token is no longer valid.
|
||||||
self.remove_token(addr, token)
|
self.remove_token(addr, token)
|
||||||
addr_path.unlink()
|
self.notification_dir.joinpath(addr).unlink(missing_ok=True)
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_protocol(rfile, wfile, notifier):
|
def handle_dovecot_protocol(rfile, wfile, notifier):
|
||||||
|
@ -84,7 +84,7 @@ def test_handle_dovecot_request_happy_path(notifier, testaddr):
|
|||||||
assert handle_dovecot_request(f"B{tx2}\t{testaddr}", transactions, notifier) is None
|
assert handle_dovecot_request(f"B{tx2}\t{testaddr}", transactions, notifier) is None
|
||||||
msg = f"S{tx2}\tpriv/guid00/messagenew"
|
msg = f"S{tx2}\tpriv/guid00/messagenew"
|
||||||
assert handle_dovecot_request(msg, transactions, notifier) is None
|
assert handle_dovecot_request(msg, transactions, notifier) is None
|
||||||
assert notifier.message_arrived_event.is_set()
|
assert notifier.notification_queue.get() == testaddr
|
||||||
assert handle_dovecot_request(f"C{tx2}", transactions, notifier) == "O\n"
|
assert handle_dovecot_request(f"C{tx2}", transactions, notifier) == "O\n"
|
||||||
assert not transactions
|
assert not transactions
|
||||||
assert notifier.notification_dir.joinpath(testaddr).exists()
|
assert notifier.notification_dir.joinpath(testaddr).exists()
|
||||||
@ -159,8 +159,8 @@ def test_handle_dovecot_protocol_messagenew(notifier):
|
|||||||
wfile = io.BytesIO()
|
wfile = io.BytesIO()
|
||||||
handle_dovecot_protocol(rfile, wfile, notifier)
|
handle_dovecot_protocol(rfile, wfile, notifier)
|
||||||
assert wfile.getvalue() == b"O\n"
|
assert wfile.getvalue() == b"O\n"
|
||||||
assert notifier.message_arrived_event.is_set()
|
addr = notifier.notification_queue.get()
|
||||||
assert notifier.notification_dir.joinpath("user@example.org").exists()
|
assert notifier.notification_dir.joinpath(addr).exists()
|
||||||
|
|
||||||
|
|
||||||
def test_notifier_thread_run(notifier, testaddr):
|
def test_notifier_thread_run(notifier, testaddr):
|
||||||
|
Loading…
Reference in New Issue
Block a user