Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgmq py ci #250

Merged
merged 11 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/workflows/pgmq-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: coredb-pgmq-python CI workflow

defaults:
run:
shell: bash
working-directory: ./extensions/pgmq/coredb-pgmq-python

on:
pull_request:
branches:
- main
paths:
- '.github/workflows/pgmq-python.yml'
- 'extensions/pgmq/coredb-pgmq-python/**'
push:
branches:
- main
paths:
- '.github/workflows/pgmq-python.yml'
- 'extensions/pgmq/coredb-pgmq-python/**'

jobs:
lints:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.11.0
uses: actions/setup-python@v2
with:
python-version: 3.11.0
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.2 python3 -
poetry install
- name: Lints / Type Checking
run: make lints

tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.11.0
uses: actions/setup-python@v2
with:
python-version: 3.11.0
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.2 python3 -
poetry install
- name: Unit and Integration Tests
run: make test

publish:
runs-on: ubuntu-latest
# only publish off main branch
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.11.0
uses: actions/setup-python@v2
with:
python-version: 3.11.0
- name: Install dependencies
run: |
curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.2 python3 -
poetry install
- name: Publish
env:
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
run: |
poetry publish --build --skip-existing
3 changes: 2 additions & 1 deletion extensions/pgmq/coredb-pgmq-python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
poetry.lock
**/*.pyc
.vscode
.vscode
dist
7 changes: 5 additions & 2 deletions extensions/pgmq/coredb-pgmq-python/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SCOPE=src/
SCOPE=coredb_pgmq_python/

format:
poetry run black ${SCOPE}
Expand All @@ -8,4 +8,7 @@ lints:
poetry run black --check ${SCOPE}
poetry run isort --check-only ${SCOPE}
poetry run flake8 ${SCOPE}
poetry run mypy ${SCOPE}
poetry run mypy ${SCOPE}

test:
poetry run pytest
68 changes: 68 additions & 0 deletions extensions/pgmq/coredb-pgmq-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,71 @@

## Installation

Install with `pip` from pypi.org

```bash
pip install coredb-pgmq-python
```

Dependencies

Postgres running the [CoreDB PGMQ extension](https://github.com/CoreDB-io/coredb/tree/main/extensions/pgmq).

## Usage

## Start a Postgres Instance with the CoreDB extension installed

```bash
docker run -d -p 5432:5432 quay.io/coredb/coredb-pg:latest
```

Initialize a connection to Postgres

```python

from coredb_pgmq_python import PGMQueue, Message

queue = PGMQueue(host="0.0.0.0")
```

Create a queue (or a partitioned queue)

```python
queue.create_queue("my_queue")
# queue.create_partitioned_queue("my_partitioned_queue", partition_size=10000)
```


Send a message

```python
msg_id: int = queue.send("my_queue", {"hello": "world"})
```

Read a message, set it invisible for 30 seconds.

```python
read_message: Message = queue.read("my_queue", vt=10)
print(read_message)
```

Archive the message after we're done with it. Archived messages are moved to an archive table.

```python
archived: bool = queue.archive("my_queue", read_message.msg_id)
```

Delete a message completely.

```python
msg_id: int = queue.send("my_queue", {"hello": "world"})
read_message: Message = queue.read("my_queue")
deleted: bool = queue.delete("my_queue", read_message.msg_id)
```

Pop a message, deleting it and reading it in one transaction.

```python
popped_message: Message = queue.pop("my_queue")
print(popped_message)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from coredb_pgmq_python.queue import Message, PGMQueue # type: ignore

__all__ = ["Message", "PGMQueue"]
4 changes: 2 additions & 2 deletions extensions/pgmq/coredb-pgmq-python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[tool.poetry]
name = "coredb-pgmq-python"
version = "0.0.1"
version = "0.1.0"
description = "Python client for the PGMQ Postgres extension."
authors = ["Adam Hendel <adam@coredb.io>"]
license = "Apache 2.0"
readme = "README.md"
packages = [{include = "src/coredb_pgmq_python"}]
packages = [{include = "coredb_pgmq_python"}]

[tool.poetry.dependencies]
python = "^3.9"
Expand Down

This file was deleted.

Empty file.
14 changes: 14 additions & 0 deletions extensions/pgmq/coredb-pgmq-python/tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from datetime import datetime

from coredb_pgmq_python import Message


def test_message() -> None:
message = Message(
msg_id=123,
read_ct=1,
enqueued_at=datetime.utcnow(),
vt=datetime.utcnow(),
message={"hello": "world"},
)
assert isinstance(message, Message)