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 @@