Skip to content

Commit 751bef7

Browse files
committed
doc: add getting started section
1 parent e537857 commit 751bef7

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

doc/getting-started.rst

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,84 @@ Or using **Docker Compose** to start Postgres with ``PGMQ`` extension:
4242
4343
For more information, see `PGMQ GitHub <https://github.com/tembo-io/pgmq>`_.
4444

45-
PGMQ SQLAlchemy
46-
---------------
45+
pgmq-sqlalchemy Setup
46+
---------------------
4747

48+
.. tip::
49+
50+
See `API Reference <api-reference>`_ for **more examples and detailed usage**.
51+
52+
For ``dispatcher.py``:
53+
54+
.. code-block:: python
55+
56+
from typing import List
57+
from pgmq_sqlalchemy import PGMQueue
58+
59+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
60+
61+
pgmq = PGMQueue(dsn=postgres_dsn)
62+
pgmq.create_queue('my_queue')
63+
64+
msg = {'key': 'value', 'key2': 'value2'}
65+
msg_id:int = pgmq.send('my_queue', msg)
66+
67+
# could also send a list of messages
68+
msg_ids:List[int] = pgmq.send_batch('my_queue', [msg, msg])
69+
70+
.. seealso::
71+
72+
.. _init_method: ref:`pgmq_sqlalchemy.PGMQueue.__init__`
73+
.. |init_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.__init__`
74+
75+
.. _send_method: ref:`pgmq_sqlalchemy.PGMQueue.send`
76+
.. |send_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.send`
77+
78+
See |init_method|_ for more options on how to initialize the ``PGMQueue`` object, and advance usage with |send_method|_ on `API Reference <api-reference>`_.
79+
80+
81+
For ``consumer.py``:
82+
83+
.. code-block:: python
84+
85+
from pgmq_sqlalchemy import PGMQueue
86+
from pgmq_sqlalchemy.schema import Message
87+
88+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
89+
90+
pgmq = PGMQueue(dsn=postgres_dsn)
91+
92+
# read a single message
93+
msg:Message = pgmq.read('my_queue')
94+
95+
# read a batch of messages
96+
msgs:List[Message] = pgmq.read_batch('my_queue', 10)
97+
98+
.. seealso::
99+
100+
.. _read_with_poll_method: ref:`pgmq_sqlalchemy.PGMQueue.read_with_poll`
101+
.. |read_with_poll_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.read_with_poll`
102+
103+
.. _read_method: ref:`pgmq_sqlalchemy.PGMQueue.read`
104+
.. |read_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.read`
105+
106+
See |read_with_poll_method|_ for reading messages with long-polling, and advance usage with |read_method|_ for **consumer retries mechanism** and more control over message consumption on `API Reference <api-reference>`_.
107+
108+
For ``monitor.py``:
109+
110+
.. code-block:: python
111+
112+
from pgmq_sqlalchemy import PGMQueue
113+
from pgmq_sqlalchemy.schema import QueueMetrics
114+
115+
postgres_dsn = 'postgresql://postgres:postgres@localhost:5432/postgres'
116+
117+
pgmq = PGMQueue(dsn=postgres_dsn)
118+
119+
# get queue metrics
120+
metrics:QueueMetrics = pgmq.metrics('my_queue')
121+
print(metrics.queue_length)
122+
print(metrics.total_messages)
123+
124+
125+

pgmq_sqlalchemy/queue.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ def read(self, queue_name: str, vt: Optional[int] = None) -> Optional[Message]:
522522
.. _for_update_skip_locked: https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE
523523
.. |for_update_skip_locked| replace:: **FOR UPDATE SKIP LOCKED**
524524
525+
.. _read_method: ref:`pgmq_sqlalchemy.PGMQueue.read`
526+
.. |read_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.read`
527+
525528
Read a message from the queue.
526529
527530
Returns:
@@ -749,6 +752,11 @@ def read_with_poll(
749752
poll_interval_ms: int = 100,
750753
) -> Optional[List[Message]]:
751754
"""
755+
756+
.. _read_with_poll_method: ref:`pgmq_sqlalchemy.PGMQueue.read_with_poll`
757+
.. |read_with_poll_method| replace:: :py:meth:`~pgmq_sqlalchemy.PGMQueue.read_with_poll`
758+
759+
752760
| Read messages from a queue with long-polling.
753761
|
754762
| When the queue is empty, the function block at most ``max_poll_seconds`` seconds.

0 commit comments

Comments
 (0)