Skip to content

Commit 6c01871

Browse files
committed
doc: add return type link reference
1 parent 4192ff0 commit 6c01871

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

pgmq_sqlalchemy/queue.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(
7777
7878
.. note::
7979
| ``PGMQueue`` will **auto create** the ``pgmq`` extension ( and ``pg_partman`` extension if the method is related with **partitioned_queue** ) if it does not exist in the Postgres.
80-
| But you must make sure that the ``pgmq`` extension ( or ``pg_partman`` extension )already **installed** in the Postgres.
80+
| But you must make sure that the ``pgmq`` extension ( or ``pg_partman`` extension ) already **installed** in the Postgres.
8181
"""
8282
if not dsn and not engine and not session_maker:
8383
raise ValueError("Must provide either dsn, engine, or session_maker")
@@ -334,7 +334,8 @@ async def _drop_queue_async(self, queue: str, partitioned: bool = False) -> bool
334334
def drop_queue(self, queue: str, partitioned: bool = False) -> bool:
335335
"""Drop a queue.
336336
337-
.. _drop_queue_anchor:
337+
.. _drop_queue_method: ref:`pgmq_sqlalchemy.PGMQueue.drop_queue`
338+
.. |drop_queue_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.drop_queue`
338339
339340
.. code-block:: python
340341
@@ -345,6 +346,8 @@ def drop_queue(self, queue: str, partitioned: bool = False) -> bool:
345346
.. warning::
346347
| All messages and queue itself will be deleted. (``pgmq.q_<queue_name>`` table)
347348
| **Archived tables** (``pgmq.a_<queue_name>`` table **will be dropped as well. )**
349+
|
350+
| See |archive_method|_ for more details.
348351
"""
349352
# check if the pg_partman extension exists before dropping a partitioned queue at runtime
350353
if partitioned:
@@ -521,11 +524,14 @@ def read(self, queue_name: str, vt: Optional[int] = None) -> Optional[Message]:
521524
522525
Read a message from the queue.
523526
527+
Returns:
528+
|schema_message_class|_ or ``None`` if the queue is empty.
529+
524530
.. note::
525531
| ``PGMQ`` use |for_update_skip_locked|_ lock to make sure **a message is only read by one consumer**.
526532
| See the `pgmq.read <https://github.com/tembo-io/pgmq/blob/main/pgmq-extension/sql/pgmq.sql?plain=1#L44-L75>`_ function for more details.
527533
|
528-
| For **consumer retries mechanism** (e.g. mark a message as failed after a certain number of retries) can be implemented by using the ``read_ct`` field in the ``Message`` object.
534+
| For **consumer retries mechanism** (e.g. mark a message as failed after a certain number of retries) can be implemented by using the ``read_ct`` field in the |schema_message_class|_ object.
529535
530536
531537
.. important::
@@ -642,6 +648,9 @@ def read_batch(
642648
| Read a batch of messages from the queue.
643649
| Usage:
644650
651+
Returns:
652+
List of |schema_message_class|_ or ``None`` if the queue is empty.
653+
645654
.. code-block:: python
646655
647656
from pgmq_sqlalchemy.schema import Message
@@ -752,6 +761,9 @@ def read_with_poll(
752761
max_poll_seconds (int): The maximum number of seconds to poll.
753762
poll_interval_ms (int): The interval in milliseconds to poll.
754763
764+
Returns:
765+
List of |schema_message_class|_ or ``None`` if the queue is empty.
766+
755767
Usage:
756768
757769
.. code-block:: python
@@ -865,6 +877,9 @@ def delete(self, queue_name: str, msg_id: int) -> bool:
865877
"""
866878
Delete a message from the queue.
867879
880+
.. _delete_method: ref:`pgmq_sqlalchemy.PGMQueue.delete`
881+
.. |delete_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.delete`
882+
868883
* Raises an error if the ``queue_name`` does not exist.
869884
* Returns ``True`` if the message is deleted successfully.
870885
* If the message does not exist, returns ``False``.
@@ -912,9 +927,12 @@ def delete_batch(self, queue_name: str, msg_ids: List[int]) -> List[int]:
912927
"""
913928
Delete a batch of messages from the queue.
914929
930+
.. _delete_batch_method: ref:`pgmq_sqlalchemy.PGMQueue.delete_batch`
931+
.. |delete_batch_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.delete_batch`
932+
915933
.. note::
916-
| Instead of return `bool` like ``delete``,
917-
| ``delete_batch`` will return a list of ``msg_ids`` that are successfully deleted.
934+
| Instead of return `bool` like |delete_method|_,
935+
| |delete_batch_method|_ will return a list of ``msg_id`` that are successfully deleted.
918936
919937
.. code-block:: python
920938
@@ -952,6 +970,9 @@ def archive(self, queue_name: str, msg_id: int) -> bool:
952970
"""
953971
Archive a message from a queue.
954972
973+
.. _archive_method: ref:`pgmq_sqlalchemy.PGMQueue.archive`
974+
.. |archive_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.archive`
975+
955976
956977
* Message will be deleted from the queue and moved to the archive table.
957978
* Will be deleted from ``pgmq.q_<queue_name>`` and be inserted into the ``pgmq.a_<queue_name>`` table.
@@ -997,7 +1018,7 @@ def archive_batch(self, queue_name: str, msg_ids: List[int]) -> List[int]:
9971018
Archive multiple messages from a queue.
9981019
9991020
* Messages will be deleted from the queue and moved to the archive table.
1000-
* Returns a list of ``msg_ids`` that are successfully archived.
1021+
* Returns a list of ``msg_id`` that are successfully archived.
10011022
10021023
.. code-block:: python
10031024
@@ -1091,11 +1112,8 @@ def metrics(self, queue_name: str) -> Optional[QueueMetrics]:
10911112
"""
10921113
Get metrics for a queue.
10931114
1094-
.. _schema_message_class: `schema.Message`_
1095-
.. |schema_message_class| replace:: :py:class:`.~pgmq_sqlalchemy.schema.QueueMetrics`
1096-
10971115
Returns:
1098-
|schema_message_class|_
1116+
|schema_queue_metrics_class|_ or ``None`` if the queue does not exist.
10991117
11001118
Usage:
11011119
@@ -1155,15 +1173,13 @@ def metrics_all(self) -> Optional[List[QueueMetrics]]:
11551173
.. _read_committed_isolation_level: https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED
11561174
.. |read_committed_isolation_level| replace:: **READ COMMITTED**
11571175
1158-
.. _drop_queue_method: ref:`pgmq_sqlalchemy.PGMQueue.drop_queue`
1159-
.. |drop_queue_method| replace:: :py:class:`~pgmq_sqlalchemy.PGMQueue.drop_queue`
1176+
.. _metrics_all_method: ref:`pgmq_sqlalchemy.PGMQueue.metrics_all`
1177+
.. |metrics_all_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.metrics_all`
11601178
11611179
Get metrics for all queues.
11621180
11631181
Returns:
1164-
List of |schema_message_class|_ objects.
1165-
1166-
:py:class:`~pgmq_sqlalchemy.schema.QueueMetrics`
1182+
List of |schema_queue_metrics_class|_ or ``None`` if there are no queues.
11671183
11681184
Usage:
11691185
@@ -1178,9 +1194,10 @@ def metrics_all(self) -> Optional[List[QueueMetrics]]:
11781194
print(m.queue_length)
11791195
11801196
.. warning::
1181-
| You should use a **distributed lock** to avoid **race conditions** when calling :py:meth:`~pgmq_sqlalchemy.metrics_call` in **concurrent** |drop_queue_method|_ **scenarios**.
1197+
| You should use a **distributed lock** to avoid **race conditions** when calling |metrics_all_method|_ in **concurrent** |drop_queue_method|_ **scenarios**.
11821198
|
11831199
| Since the default PostgreSQL isolation level is |read_committed_isolation_level|_, the queue metrics to be fetched **may not exist** if there are **concurrent** |drop_queue_method|_ **operations**.
1200+
| Check the `pgmq.metrics_all <https://github.com/tembo-io/pgmq/blob/main/pgmq-extension/sql/pgmq.sql?plain=1#L334-L346>`_ function for more details.
11841201
11851202
11861203
"""

pgmq_sqlalchemy/schema.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from dataclasses import dataclass
22
from datetime import datetime
3+
from typing import Optional
34

45

56
@dataclass
67
class Message:
8+
"""
9+
.. _schema_message_class: `schema.Message`_
10+
.. |schema_message_class| replace:: :py:class:`.~pgmq_sqlalchemy.schema.Message`
11+
"""
12+
713
msg_id: int
814
read_ct: int
915
enqueued_at: datetime
@@ -13,8 +19,13 @@ class Message:
1319

1420
@dataclass
1521
class QueueMetrics:
22+
"""
23+
.. _schema_queue_metrics_class: `schema.QueueMetrics`_
24+
.. |schema_queue_metrics_class| replace:: :py:class:`.~pgmq_sqlalchemy.schema.QueueMetrics`
25+
"""
26+
1627
queue_name: str
1728
queue_length: int
18-
newest_msg_age_sec: int
19-
oldest_msg_age_sec: int
29+
newest_msg_age_sec: Optional[int]
30+
oldest_msg_age_sec: Optional[int]
2031
total_messages: int

0 commit comments

Comments
 (0)