romeways-nats-queue 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- romeways_nats_queue/__init__.py +2 -0
- romeways_nats_queue/config/__init__.py +2 -0
- romeways_nats_queue/config/connector/__init__.py +1 -0
- romeways_nats_queue/config/connector/model.py +15 -0
- romeways_nats_queue/config/queue/__init__.py +1 -0
- romeways_nats_queue/config/queue/model.py +18 -0
- romeways_nats_queue/infrastructure/__init__.py +1 -0
- romeways_nats_queue/infrastructure/connector/__init__.py +1 -0
- romeways_nats_queue/infrastructure/connector/infrastructure.py +58 -0
- romeways_nats_queue-0.1.0.dist-info/METADATA +122 -0
- romeways_nats_queue-0.1.0.dist-info/RECORD +12 -0
- romeways_nats_queue-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .model import NatsConnectorConfig
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from romeways import GenericConnectorConfig
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(slots=True, frozen=True)
|
|
7
|
+
class NatsConnectorConfig(GenericConnectorConfig):
|
|
8
|
+
"""
|
|
9
|
+
servers: str | list[str] NATS server URL(s), e.g. "nats://localhost:4222".
|
|
10
|
+
See nats.connect doc -> https://nats-io.github.io/nats.py/
|
|
11
|
+
name: str | None Optional client name reported to the server.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
servers: str | list[str] = "nats://localhost:4222"
|
|
15
|
+
name: str | None = field(default=None)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .model import NatsQueueConfig
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from romeways import GenericQueueConfig
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(slots=True, frozen=True)
|
|
7
|
+
class NatsQueueConfig(GenericQueueConfig):
|
|
8
|
+
"""
|
|
9
|
+
subject: str JetStream subject to consume from / publish to.
|
|
10
|
+
stream: str JetStream stream that captures ``subject`` (created if missing).
|
|
11
|
+
durable: str Durable pull-consumer name (so redelivery survives reconnects).
|
|
12
|
+
fetch_timeout: float Seconds to wait for a batch before returning empty.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
subject: str
|
|
16
|
+
stream: str
|
|
17
|
+
durable: str
|
|
18
|
+
fetch_timeout: float = 1.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .connector import NatsQueueConnector
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .infrastructure import NatsQueueConnector
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import nats
|
|
4
|
+
from nats.errors import TimeoutError as NatsTimeoutError
|
|
5
|
+
from nats.js.errors import NotFoundError
|
|
6
|
+
|
|
7
|
+
from romeways import AQueueConnector
|
|
8
|
+
|
|
9
|
+
from romeways_nats_queue.config import NatsConnectorConfig, NatsQueueConfig
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger("romeways")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class NatsQueueConnector(AQueueConnector):
|
|
15
|
+
"""romeways connector backed by NATS JetStream (durable, at-least-once).
|
|
16
|
+
|
|
17
|
+
Uses a durable pull consumer so messages are redelivered across restarts.
|
|
18
|
+
Each fetched message is acked after it is handed to romeways; romeways' own
|
|
19
|
+
resend-on-error (``ResendException``) republishes to the same subject.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
_connector_config: NatsConnectorConfig
|
|
23
|
+
_config: NatsQueueConfig
|
|
24
|
+
|
|
25
|
+
async def on_start(self):
|
|
26
|
+
self._nc = await nats.connect(
|
|
27
|
+
servers=self._connector_config.servers,
|
|
28
|
+
name=self._connector_config.name,
|
|
29
|
+
)
|
|
30
|
+
self._js = self._nc.jetstream()
|
|
31
|
+
# Ensure the stream exists for the subject (idempotent).
|
|
32
|
+
try:
|
|
33
|
+
await self._js.stream_info(self._config.stream)
|
|
34
|
+
except NotFoundError:
|
|
35
|
+
await self._js.add_stream(
|
|
36
|
+
name=self._config.stream, subjects=[self._config.subject]
|
|
37
|
+
)
|
|
38
|
+
self._subscription = await self._js.pull_subscribe(
|
|
39
|
+
subject=self._config.subject,
|
|
40
|
+
durable=self._config.durable,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
async def get_messages(self, max_chunk_size: int) -> list[bytes]:
|
|
44
|
+
try:
|
|
45
|
+
messages = await self._subscription.fetch(
|
|
46
|
+
batch=abs(max_chunk_size),
|
|
47
|
+
timeout=self._config.fetch_timeout,
|
|
48
|
+
)
|
|
49
|
+
except NatsTimeoutError:
|
|
50
|
+
return []
|
|
51
|
+
buffer = []
|
|
52
|
+
for message in messages:
|
|
53
|
+
await message.ack()
|
|
54
|
+
buffer.append(message.data)
|
|
55
|
+
return buffer
|
|
56
|
+
|
|
57
|
+
async def send_messages(self, message: bytes):
|
|
58
|
+
await self._js.publish(self._config.subject, message)
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: romeways-nats-queue
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: NATS JetStream queue connector for romeways
|
|
5
|
+
Project-URL: homepage, https://github.com/CenturyBoys/romeways
|
|
6
|
+
Project-URL: repository, https://github.com/CenturyBoys/romeways
|
|
7
|
+
Author-email: Marco Sievers de Almeida Ximit Gaia <im.ximit@gmail.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Python: <4,>=3.11
|
|
15
|
+
Requires-Dist: nats-py<3,>=2.6
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# Romeways NATS (JetStream)
|
|
19
|
+
|
|
20
|
+
This is an extra package for romeways — for more details access the
|
|
21
|
+
[romeways](https://github.com/CenturyBoys/romeways) Github page.
|
|
22
|
+
|
|
23
|
+
This package uses [nats-py](https://pypi.org/project/nats-py/) and **NATS JetStream** to provide
|
|
24
|
+
a durable, at-least-once queue consumer. It requires a NATS server with JetStream enabled
|
|
25
|
+
(`nats-server -js`).
|
|
26
|
+
|
|
27
|
+
## Configs
|
|
28
|
+
|
|
29
|
+
### Queue
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from dataclasses import dataclass
|
|
33
|
+
|
|
34
|
+
from romeways import GenericQueueConfig
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(slots=True, frozen=True)
|
|
38
|
+
class NatsQueueConfig(GenericQueueConfig):
|
|
39
|
+
"""
|
|
40
|
+
subject: str JetStream subject to consume from / publish to.
|
|
41
|
+
stream: str JetStream stream that captures `subject` (created if missing).
|
|
42
|
+
durable: str Durable pull-consumer name (redelivery survives reconnects).
|
|
43
|
+
fetch_timeout: float Seconds to wait for a batch before returning empty.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
subject: str
|
|
47
|
+
stream: str
|
|
48
|
+
durable: str
|
|
49
|
+
fetch_timeout: float = 1.0
|
|
50
|
+
connector_name: str
|
|
51
|
+
frequency: float
|
|
52
|
+
max_chunk_size: int
|
|
53
|
+
sequential: bool
|
|
54
|
+
retries: int
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Connector
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from dataclasses import dataclass
|
|
61
|
+
|
|
62
|
+
from romeways import GenericConnectorConfig
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(slots=True, frozen=True)
|
|
66
|
+
class NatsConnectorConfig(GenericConnectorConfig):
|
|
67
|
+
"""
|
|
68
|
+
servers: str | list[str] NATS server URL(s), e.g. "nats://localhost:4222".
|
|
69
|
+
name: str | None Optional client name reported to the server.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
servers: str | list[str] = "nats://localhost:4222"
|
|
73
|
+
name: str | None = None
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Use case
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import asyncio
|
|
80
|
+
|
|
81
|
+
import romeways
|
|
82
|
+
|
|
83
|
+
# Create a queue config
|
|
84
|
+
config_q = romeways.NatsQueueConfig(
|
|
85
|
+
subject="queue.payment.done",
|
|
86
|
+
stream="payments",
|
|
87
|
+
durable="payment-consumer",
|
|
88
|
+
connector_name="nats-dev1",
|
|
89
|
+
frequency=1,
|
|
90
|
+
max_chunk_size=10,
|
|
91
|
+
sequential=False,
|
|
92
|
+
retries=3,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# Register a controller/consumer for the queue name
|
|
97
|
+
@romeways.queue_consumer(queue_name="queue.payment.done", config=config_q)
|
|
98
|
+
async def controller(message: romeways.Message):
|
|
99
|
+
print(message)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
config_p = romeways.NatsConnectorConfig(
|
|
103
|
+
connector_name="nats-dev1",
|
|
104
|
+
servers="nats://localhost:4222",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# Register a connector
|
|
108
|
+
romeways.connector_register(
|
|
109
|
+
connector=romeways.NatsQueueConnector, config=config_p, spawn_process=False
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
asyncio.run(romeways.start())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Semantics
|
|
116
|
+
|
|
117
|
+
- Messages are consumed through a **durable pull consumer**, so unacked messages are
|
|
118
|
+
redelivered after a restart.
|
|
119
|
+
- Each fetched message is acked once handed to romeways; romeways' own resend-on-error
|
|
120
|
+
(`ResendException`) republishes to the same subject, honouring the queue's `retries`.
|
|
121
|
+
- Use `spawn_process=False`: the NATS connection is created in `on_start` on the running
|
|
122
|
+
event loop.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
romeways_nats_queue/__init__.py,sha256=5ajrC2fYUlse5ZsDcYQQG1xZHWqMTfA7knf1Iu54MnA,104
|
|
2
|
+
romeways_nats_queue/config/__init__.py,sha256=VyAqDxW6SUJ-RAgTGNZAgk1GKvvQwqU_SV6sMYntBQg,78
|
|
3
|
+
romeways_nats_queue/config/connector/__init__.py,sha256=nPQednW0DFI27Es5C7aGPQ6uzRmoyLt-y3MBbXFtyDA,39
|
|
4
|
+
romeways_nats_queue/config/connector/model.py,sha256=DquVheunYKwpi1UO5dtqlmnXF1HVY-nIKUScBJ7GIkA,499
|
|
5
|
+
romeways_nats_queue/config/queue/__init__.py,sha256=OB27VNi2WeOPUsAj38tyTt9FkmlKLZVuSOS3Z5Bia10,35
|
|
6
|
+
romeways_nats_queue/config/queue/model.py,sha256=lucjJp_-3xGVYvLMYEft0A7aDWUrV-NPw9FX0RFgrD0,558
|
|
7
|
+
romeways_nats_queue/infrastructure/__init__.py,sha256=GlN7_ypD41nurrzQBMSOqPjmZViSHy2pIDnQVLJ00UY,42
|
|
8
|
+
romeways_nats_queue/infrastructure/connector/__init__.py,sha256=EFFtCXnpbbfXMXbfMbBipWtCPkGHWQKvbmgifLQ3RjM,47
|
|
9
|
+
romeways_nats_queue/infrastructure/connector/infrastructure.py,sha256=1c_wunnXhDtqEW3Jx81K8ocJ--uWmgHqcS-WngrOIRo,1949
|
|
10
|
+
romeways_nats_queue-0.1.0.dist-info/METADATA,sha256=sdKH9Hpp1yP1RJAWh1MtFHl38x8QzKLqD9vmzDdrhm8,3419
|
|
11
|
+
romeways_nats_queue-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
12
|
+
romeways_nats_queue-0.1.0.dist-info/RECORD,,
|