From e05b54468a0a8db01703a4163b9e3fb4a4def6b2 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Tue, 11 Apr 2023 19:03:41 +0300 Subject: [PATCH] feat: add reconnect to `nostrclient` --- __init__.py | 1 + nostr/nostr_client.py | 20 +++++++++++++++++--- static/js/index.js | 11 +++++++++++ templates/nostrmarket/index.html | 15 +++++++++++++++ views_api.py | 8 ++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/__init__.py b/__init__.py index bac1155..9d9c204 100644 --- a/__init__.py +++ b/__init__.py @@ -43,6 +43,7 @@ def nostrmarket_start(): # wait for 'nostrclient' extension to initialize await asyncio.sleep(10) await nostr_client.run_forever() + raise ValueError("Must reconnect to websocket") async def _wait_for_nostr_events(): # wait for this extension to initialize diff --git a/nostr/nostr_client.py b/nostr/nostr_client.py index b788072..dfacf7c 100644 --- a/nostr/nostr_client.py +++ b/nostr/nostr_client.py @@ -18,6 +18,11 @@ class NostrClient: self.send_req_queue: Queue = Queue() self.ws: WebSocketApp = None + async def restart(self): + await self.send_req_queue.put(ValueError("Restarting NostrClient...")) + await self.recieve_event_queue.put(ValueError("Restarting NostrClient...")) + self.ws.close() + async def connect_to_nostrclient_ws( self, on_open: Callable, on_message: Callable ) -> WebSocketApp: @@ -39,7 +44,10 @@ class NostrClient: return ws async def get_event(self): - return await self.recieve_event_queue.get() + value = await self.recieve_event_queue.get() + if isinstance(value, ValueError): + raise value + return value async def run_forever(self): def on_open(_): @@ -48,7 +56,9 @@ class NostrClient: def on_message(_, message): self.recieve_event_queue.put_nowait(message) - while True: + running = True + + while running: try: req = None if not self.ws: @@ -56,7 +66,11 @@ class NostrClient: # be sure the connection is open await asyncio.sleep(3) req = await self.send_req_queue.get() - self.ws.send(json.dumps(req)) + if isinstance(req, ValueError): + running = False + logger.warning("Nostr Client stopping") + else: + self.ws.send(json.dumps(req)) except Exception as ex: logger.warning(ex) if req: diff --git a/static/js/index.js b/static/js/index.js index 6593430..96d1d0b 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -136,6 +136,17 @@ const merchant = async () => { caption: `${error}` }) } + }, + restartNostrConnection: async function () { + try { + await LNbits.api.request( + 'PUT', + '/nostrmarket/api/v1/restart', + this.g.user.wallets[0].adminkey + ) + } catch (error) { + LNbits.utils.notifyApiError(error) + } } }, created: async function () { diff --git a/templates/nostrmarket/index.html b/templates/nostrmarket/index.html index 9064c0c..aef450b 100644 --- a/templates/nostrmarket/index.html +++ b/templates/nostrmarket/index.html @@ -133,6 +133,21 @@
+
+ + + + + Restart the connection to the nostrclient extension + + + + +
diff --git a/views_api.py b/views_api.py index 25a5022..0fa8317 100644 --- a/views_api.py +++ b/views_api.py @@ -826,6 +826,14 @@ async def api_list_currencies_available(): return list(currencies.keys()) +@nostrmarket_ext.put("/api/v1/restart") +async def restart_nostr_client(wallet: WalletTypeInfo = Depends(require_admin_key)): + try: + await nostr_client.restart() + except Exception as ex: + logger.warning(ex) + + @nostrmarket_ext.delete("/api/v1", status_code=HTTPStatus.OK) async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)): for t in scheduled_tasks: