Skip to content

Commit af8c201

Browse files
committed
🧲 feat(release): NATS credentials and NGS compat
1 parent 41e6bb5 commit af8c201

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

magnet/ic/field.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __init__(self, server: str):
183183
"""
184184
self.server = server
185185

186-
async def on(self, category: str = 'no_category', stream: str = 'documents', session='magnet', job: bool = None, local: bool = False, domain: str = None, bandwidth: int = 3):
186+
async def on(self, category: str = 'no_category', stream: str = 'documents', session='magnet', job: bool = None, local: bool = False, domain: str = None, bandwidth: int = 3, credentials: str = 'path_to_creds_file', port: int = 4222):
187187
"""
188188
Connects to the NATS server, subscribes to a specific category in a stream, and consumes messages from that category.
189189
@@ -211,19 +211,16 @@ async def on(self, category: str = 'no_category', stream: str = 'documents', ses
211211
)
212212
_f('wait', f'connecting to {self.server}')
213213
try:
214-
self.nc = await nats.connect(f'nats://{self.server}:4222')
214+
self.nc = await nats.connect(f'{"nats://" if not credentials else "tls://"}{self.server}:{port}',user_credentials=credentials)
215215
self.js = self.nc.jetstream(
216216
domain=domain
217217
)
218218
try:
219219
self.sub = await self.js.pull_subscribe(
220-
stream=self.stream, subject=self.category, durable=self.durable, config=self.config
221-
) if job else await self.js.subscribe(
222-
subject=self.category
220+
durable=self.durable
221+
, subject=self.category
223222
, stream=self.stream
224-
, queue=self.session
225223
, config=self.config
226-
, manual_ack=True
227224
)
228225
_f('info',
229226
f'joined worker queue: {self.session} as {self.node}')
@@ -233,9 +230,9 @@ async def on(self, category: str = 'no_category', stream: str = 'documents', ses
233230
except TimeoutError:
234231
_f("fatal", f'could not connect to {self.server}')
235232
except Exception as e:
236-
_f('wait', 'server queuing you...')
233+
_f('wait', f'server queuing you...\n{e}')
237234

238-
async def listen(self, cb=print, job_n: int = None, generic: bool = False):
235+
async def listen(self, cb=print, job_n: int = None, generic: bool = False, verbose=False):
239236
"""
240237
Consume messages from a specific category in a stream and process them.
241238
@@ -275,13 +272,15 @@ async def listen(self, cb=print, job_n: int = None, generic: bool = False):
275272
f'consuming delta from [{self.category}] on\n🛰️ stream: {self.stream}\n🧲 session: "{self.session}"')
276273
while True:
277274
try:
278-
msg = await self.sub.next_msg(timeout=60)
279-
payload = msg.data if generic else Payload(
280-
**json.loads(msg.data))
275+
msgs = await self.sub.fetch(batch=1, timeout=60)
276+
_f('info', f"{msgs}") if verbose else None
277+
payload = msgs[0].data if generic else Payload(
278+
**json.loads(msgs[0].data))
279+
_f('info', f"{payload}") if verbose else None
281280
try:
282-
await cb(payload, msg)
281+
await cb(payload, msgs[0])
283282
except Exception as e:
284-
_f("warn", f'retrying connection to {self.server}')
283+
_f("warn", f'retrying connection to {self.server}\n{e}')
285284
except Exception as e:
286285
_f('fatal', f'invalid JSON\n{e}')
287286
break

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pymilvus
88
boto3
99
nats-py
1010
rich
11-
xxhash
11+
xxhash
12+
nkeys

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
'boto3',
1717
'nats-py',
1818
'rich',
19-
'xxhash'
19+
'xxhash',
20+
'nkeys'
2021
],
2122
url = 'https://github.com/Prismadic/magnet'
2223
)

0 commit comments

Comments
 (0)