-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Prismadic/6-featperf-add-nats-functionality
🧲 feat(release): Basic NATS functionality w/ persistent streams
- Loading branch information
Showing
9 changed files
with
144 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
from .ic import ic | ||
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import nats | ||
from magnet.utils import _f | ||
|
||
class Charge: | ||
def __init__(self, server): | ||
self.server = server | ||
|
||
async def on(self, frequency: str = 'default', stream: str = 'default'): | ||
self.frequency = frequency | ||
self.stream = stream | ||
try: | ||
nc = await nats.connect(f'nats://{self.server}:4222') | ||
self.nc = nc | ||
self.js = self.nc.jetstream() | ||
await self.js.add_stream(name=self.stream, subjects=[self.frequency]) | ||
self.sub = await self.js.pull_subscribe(self.frequency, 'magnet-charge') | ||
_f("success", f'connected to {self.server}') | ||
except TimeoutError: | ||
_f('fatal', f'could not connect to {self.server}') | ||
async def off(self): | ||
await self.js.unsubscribe() | ||
_f('warn', f'unsubscribed from {self.stream}') | ||
await self.nc.drain() | ||
_f('warn', f'disconnected from {self.server}') | ||
async def pulse(self, packet): | ||
try: | ||
await self.js.publish(self.frequency, packet) | ||
except Exception as e: | ||
print(e) | ||
_f('fatal', f'could not send data to {self.server}') | ||
async def emp(self): | ||
await self.js.delete_stream(name=self.stream) | ||
_f('success', f'{self.frequency} stream deleted') | ||
|
||
class Resonator: | ||
|
||
def __init__(self, server): | ||
self.server = server | ||
async def on(self, frequency: str = 'default', stream: str = 'default', cb=print): | ||
self.frequency = frequency | ||
self.stream = stream | ||
self.nc = await nats.connect(f'nats://{self.server}:4222') | ||
self.js = self.nc.jetstream() | ||
self.sub = await self.js.pull_subscribe(self.frequency, stream=self.stream) | ||
while True: | ||
msgs = await self.sub.fetch(batch=10, timeout=60) | ||
for msg in msgs: | ||
cb(msg) | ||
async def off(self): | ||
await self.js.unsubscribe() | ||
_f('warn', f'unsubscribed from {self.stream}') | ||
await self.nc.drain() | ||
_f('warn', f'disconnected from {self.server}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
from .ize import ize | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.