|
| 1 | +"""WARNING: This module is a work in progress. |
| 2 | +
|
| 3 | +The PsycopgJournal implementation has not been extensively reviewed. It passes |
| 4 | +the base test suite but may contain bugs or design flaws. You shouldn't use this yet. |
| 5 | +""" |
| 6 | + |
| 7 | +from collections.abc import Iterator |
| 8 | +from contextlib import suppress |
| 9 | +from threading import Lock |
| 10 | + |
| 11 | +import psycopg |
| 12 | + |
| 13 | +from queueio.journal import Journal |
| 14 | + |
| 15 | + |
| 16 | +class PsycopgJournal(Journal): |
| 17 | + @classmethod |
| 18 | + def from_uri(cls, uri: str, /): |
| 19 | + return cls(uri) |
| 20 | + |
| 21 | + def __init__(self, uri: str): |
| 22 | + self.__publish_conn = psycopg.connect(uri, autocommit=True) |
| 23 | + self.__subscribe_conn = psycopg.connect(uri, autocommit=True) |
| 24 | + self.__publish_lock = Lock() |
| 25 | + self.__shutdown_lock = Lock() |
| 26 | + self.__shutdown = False |
| 27 | + |
| 28 | + # Ensure schema exists |
| 29 | + self.__publish_conn.execute(""" |
| 30 | + CREATE TABLE IF NOT EXISTS queueio_journal ( |
| 31 | + id BIGSERIAL PRIMARY KEY, |
| 32 | + body BYTEA NOT NULL, |
| 33 | + created_at TIMESTAMPTZ NOT NULL DEFAULT now() |
| 34 | + ) |
| 35 | + """) |
| 36 | + |
| 37 | + # LISTEN before SELECT to avoid race condition: |
| 38 | + # any NOTIFY after LISTEN will be buffered, and the subsequent |
| 39 | + # SELECT establishes our starting position. |
| 40 | + self.__subscribe_conn.execute("LISTEN queueio_journal") |
| 41 | + row = self.__subscribe_conn.execute( |
| 42 | + "SELECT COALESCE(MAX(id), 0) FROM queueio_journal" |
| 43 | + ).fetchone() |
| 44 | + assert row is not None |
| 45 | + self.__last_id: int = row[0] |
| 46 | + |
| 47 | + def publish(self, message: bytes): |
| 48 | + with self.__publish_lock, self.__publish_conn.transaction(): |
| 49 | + self.__publish_conn.execute( |
| 50 | + t"INSERT INTO queueio_journal (body) VALUES ({message})" |
| 51 | + ) |
| 52 | + self.__publish_conn.execute("NOTIFY queueio_journal") |
| 53 | + |
| 54 | + def subscribe(self) -> Iterator[bytes]: |
| 55 | + try: |
| 56 | + while not self.__shutdown: |
| 57 | + rows = self.__subscribe_conn.execute( |
| 58 | + t""" |
| 59 | + SELECT id, body FROM queueio_journal |
| 60 | + WHERE id > {self.__last_id} |
| 61 | + ORDER BY id |
| 62 | + """ |
| 63 | + ).fetchall() |
| 64 | + |
| 65 | + for row in rows: |
| 66 | + self.__last_id = row[0] |
| 67 | + yield row[1] |
| 68 | + |
| 69 | + if not rows and not self.__shutdown: |
| 70 | + # Wait for a notification to wake us up |
| 71 | + for _ in self.__subscribe_conn.notifies(timeout=1.0): |
| 72 | + break # Got one, go back to query loop |
| 73 | + finally: |
| 74 | + with suppress(Exception): |
| 75 | + self.__subscribe_conn.execute("UNLISTEN queueio_journal") |
| 76 | + with suppress(Exception): |
| 77 | + self.__subscribe_conn.close() |
| 78 | + |
| 79 | + def shutdown(self): |
| 80 | + with self.__shutdown_lock: |
| 81 | + if self.__shutdown: |
| 82 | + return |
| 83 | + self.__shutdown = True |
| 84 | + |
| 85 | + # Wake up the subscriber by sending a notification |
| 86 | + with suppress(Exception): |
| 87 | + self.__publish_conn.execute("NOTIFY queueio_journal") |
| 88 | + |
| 89 | + with suppress(Exception): |
| 90 | + self.__publish_conn.close() |
0 commit comments