|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import typing as tp |
| 4 | +from collections.abc import AsyncGenerator |
| 5 | +from dataclasses import dataclass |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +import psqlpy |
| 9 | +from psqlpy.exceptions import ConnectionExecuteError |
| 10 | +from psqlpy.extra_types import JSONB |
| 11 | +from taskiq import AckableMessage, AsyncBroker, AsyncResultBackend, BrokerMessage |
| 12 | + |
| 13 | +from taskiq_psqlpy.queries import ( |
| 14 | + CLAIM_MESSAGE_QUERY, |
| 15 | + CREATE_MESSAGE_TABLE_QUERY, |
| 16 | + DELETE_MESSAGE_QUERY, |
| 17 | + INSERT_MESSAGE_QUERY, |
| 18 | +) |
| 19 | + |
| 20 | +logger = logging.getLogger("taskiq.psqlpy_broker") |
| 21 | +_T = tp.TypeVar("_T") |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class MessageRow: |
| 26 | + """Message in db table.""" |
| 27 | + |
| 28 | + id: int |
| 29 | + task_id: str |
| 30 | + task_name: str |
| 31 | + message: str |
| 32 | + labels: JSONB |
| 33 | + status: str |
| 34 | + created_at: datetime |
| 35 | + |
| 36 | + |
| 37 | +class PSQLPyBroker(AsyncBroker): |
| 38 | + """Broker that uses PostgreSQL and PSQLPy with LISTEN/NOTIFY.""" |
| 39 | + |
| 40 | + _read_conn: psqlpy.Connection |
| 41 | + _write_pool: psqlpy.ConnectionPool |
| 42 | + _listener: psqlpy.Listener |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + dsn: ( |
| 47 | + str | tp.Callable[[], str] |
| 48 | + ) = "postgresql://taskiq_psqlpy:look_in_vault@localhost:5432/taskiq_psqlpy", |
| 49 | + result_backend: AsyncResultBackend[_T] | None = None, |
| 50 | + task_id_generator: tp.Callable[[], str] | None = None, |
| 51 | + channel_name: str = "taskiq", |
| 52 | + table_name: str = "taskiq_messages", |
| 53 | + max_retry_attempts: int = 5, |
| 54 | + read_kwargs: dict[str, tp.Any] | None = None, |
| 55 | + write_kwargs: dict[str, tp.Any] | None = None, |
| 56 | + ) -> None: |
| 57 | + """ |
| 58 | + Construct a new broker. |
| 59 | +
|
| 60 | + Args: |
| 61 | + dsn: connection string to PostgreSQL, or callable returning one. |
| 62 | + result_backend: Custom result backend. |
| 63 | + task_id_generator: Custom task_id generator. |
| 64 | + channel_name: Name of the channel to listen on. |
| 65 | + table_name: Name of the table to store messages. |
| 66 | + max_retry_attempts: Maximum number of message processing attempts. |
| 67 | + read_kwargs: Additional arguments for read connection creation. |
| 68 | + write_kwargs: Additional arguments for write pool creation. |
| 69 | +
|
| 70 | + """ |
| 71 | + super().__init__( |
| 72 | + result_backend=result_backend, |
| 73 | + task_id_generator=task_id_generator, |
| 74 | + ) |
| 75 | + self._dsn: str | tp.Callable[[], str] = dsn |
| 76 | + self.channel_name: str = channel_name |
| 77 | + self.table_name: str = table_name |
| 78 | + self.read_kwargs: dict[str, tp.Any] = read_kwargs or {} |
| 79 | + self.write_kwargs: dict[str, tp.Any] = write_kwargs or {} |
| 80 | + self.max_retry_attempts: int = max_retry_attempts |
| 81 | + self._queue: asyncio.Queue[str] | None = None |
| 82 | + |
| 83 | + @property |
| 84 | + def dsn(self) -> str: |
| 85 | + """ |
| 86 | + Get the DSN string. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + A string with dsn or None if dsn isn't set yet. |
| 90 | +
|
| 91 | + """ |
| 92 | + if callable(self._dsn): |
| 93 | + return self._dsn() |
| 94 | + return self._dsn |
| 95 | + |
| 96 | + async def startup(self) -> None: |
| 97 | + """Initialize the broker.""" |
| 98 | + await super().startup() |
| 99 | + self._read_conn = await psqlpy.connect( |
| 100 | + dsn=self.dsn, |
| 101 | + **self.read_kwargs, |
| 102 | + ) |
| 103 | + self._write_pool = psqlpy.ConnectionPool( |
| 104 | + dsn=self.dsn, |
| 105 | + **self.write_kwargs, |
| 106 | + ) |
| 107 | + |
| 108 | + # create messages table if it doesn't exist |
| 109 | + async with self._write_pool.acquire() as conn: |
| 110 | + await conn.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name)) |
| 111 | + |
| 112 | + # listen to notification channel |
| 113 | + self._listener = self._write_pool.listener() |
| 114 | + await self._listener.add_callback(self.channel_name, self._notification_handler) |
| 115 | + await self._listener.startup() |
| 116 | + self._listener.listen() |
| 117 | + |
| 118 | + self._queue = asyncio.Queue() |
| 119 | + |
| 120 | + async def shutdown(self) -> None: |
| 121 | + """Close all connections on shutdown.""" |
| 122 | + await super().shutdown() |
| 123 | + if self._read_conn is not None: |
| 124 | + self._read_conn.close() |
| 125 | + if self._write_pool is not None: |
| 126 | + self._write_pool.close() |
| 127 | + if self._listener is not None: |
| 128 | + self._listener.abort_listen() |
| 129 | + await self._listener.shutdown() |
| 130 | + |
| 131 | + async def _notification_handler( |
| 132 | + self, |
| 133 | + connection: psqlpy.Connection, |
| 134 | + payload: str, |
| 135 | + channel: str, |
| 136 | + process_id: int, |
| 137 | + ) -> None: |
| 138 | + """ |
| 139 | + Handle NOTIFY messages. |
| 140 | +
|
| 141 | + https://psqlpy-python.github.io/components/listener.html#usage |
| 142 | + """ |
| 143 | + logger.debug("Received notification on channel %s: %s", channel, payload) |
| 144 | + if self._queue is not None: |
| 145 | + self._queue.put_nowait(payload) |
| 146 | + |
| 147 | + async def kick(self, message: BrokerMessage) -> None: |
| 148 | + """ |
| 149 | + Send message to the channel. |
| 150 | +
|
| 151 | + Inserts the message into the database and sends a NOTIFY. |
| 152 | +
|
| 153 | + :param message: Message to send. |
| 154 | + """ |
| 155 | + async with self._write_pool.acquire() as conn: |
| 156 | + # insert message into db table |
| 157 | + message_inserted_id = tp.cast( |
| 158 | + "int", |
| 159 | + await conn.fetch_val( |
| 160 | + INSERT_MESSAGE_QUERY.format(self.table_name), |
| 161 | + [ |
| 162 | + message.task_id, |
| 163 | + message.task_name, |
| 164 | + message.message.decode(), |
| 165 | + JSONB(message.labels), |
| 166 | + ], |
| 167 | + ), |
| 168 | + ) |
| 169 | + |
| 170 | + delay_value = tp.cast("str | None", message.labels.get("delay")) |
| 171 | + if delay_value is not None: |
| 172 | + delay_seconds = int(delay_value) |
| 173 | + asyncio.create_task( # noqa: RUF006 |
| 174 | + self._schedule_notification(message_inserted_id, delay_seconds), |
| 175 | + ) |
| 176 | + else: |
| 177 | + # Send NOTIFY with message ID as payload |
| 178 | + _ = await conn.execute( |
| 179 | + f"NOTIFY {self.channel_name}, '{message_inserted_id}'", |
| 180 | + ) |
| 181 | + |
| 182 | + async def _schedule_notification(self, message_id: int, delay_seconds: int) -> None: |
| 183 | + """Schedule a notification to be sent after a delay.""" |
| 184 | + await asyncio.sleep(delay_seconds) |
| 185 | + async with self._write_pool.acquire() as conn: |
| 186 | + # Send NOTIFY with message ID as payload |
| 187 | + _ = await conn.execute(f"NOTIFY {self.channel_name}, '{message_id}'") |
| 188 | + |
| 189 | + async def listen(self) -> AsyncGenerator[AckableMessage, None]: |
| 190 | + """ |
| 191 | + Listen to the channel. |
| 192 | +
|
| 193 | + Yields messages as they are received. |
| 194 | +
|
| 195 | + :yields: AckableMessage instances. |
| 196 | + """ |
| 197 | + while True: |
| 198 | + try: |
| 199 | + payload = await self._queue.get() # type: ignore[union-attr] |
| 200 | + message_id = int(payload) # payload is the message id |
| 201 | + try: |
| 202 | + async with self._write_pool.acquire() as conn: |
| 203 | + claimed_message = await conn.fetch_row( |
| 204 | + CLAIM_MESSAGE_QUERY.format(self.table_name), |
| 205 | + [message_id], |
| 206 | + ) |
| 207 | + except ConnectionExecuteError: # message was claimed by another worker |
| 208 | + continue |
| 209 | + message_row_result = tp.cast( |
| 210 | + "MessageRow", |
| 211 | + tp.cast("object", claimed_message.as_class(MessageRow)), |
| 212 | + ) |
| 213 | + message_data = message_row_result.message.encode() |
| 214 | + |
| 215 | + async def ack(*, _message_id: int = message_id) -> None: |
| 216 | + async with self._write_pool.acquire() as conn: |
| 217 | + _ = await conn.execute( |
| 218 | + DELETE_MESSAGE_QUERY.format(self.table_name), |
| 219 | + [_message_id], |
| 220 | + ) |
| 221 | + |
| 222 | + yield AckableMessage(data=message_data, ack=ack) |
| 223 | + except Exception: |
| 224 | + logger.exception("Error processing message") |
| 225 | + continue |
0 commit comments