feat: add reconnect to nostrclient

This commit is contained in:
Vlad Stan 2023-04-11 19:03:41 +03:00
parent 929d45bed8
commit e05b54468a
5 changed files with 52 additions and 3 deletions

View file

@ -43,6 +43,7 @@ def nostrmarket_start():
# wait for 'nostrclient' extension to initialize # wait for 'nostrclient' extension to initialize
await asyncio.sleep(10) await asyncio.sleep(10)
await nostr_client.run_forever() await nostr_client.run_forever()
raise ValueError("Must reconnect to websocket")
async def _wait_for_nostr_events(): async def _wait_for_nostr_events():
# wait for this extension to initialize # wait for this extension to initialize

View file

@ -18,6 +18,11 @@ class NostrClient:
self.send_req_queue: Queue = Queue() self.send_req_queue: Queue = Queue()
self.ws: WebSocketApp = None 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( async def connect_to_nostrclient_ws(
self, on_open: Callable, on_message: Callable self, on_open: Callable, on_message: Callable
) -> WebSocketApp: ) -> WebSocketApp:
@ -39,7 +44,10 @@ class NostrClient:
return ws return ws
async def get_event(self): 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): async def run_forever(self):
def on_open(_): def on_open(_):
@ -48,7 +56,9 @@ class NostrClient:
def on_message(_, message): def on_message(_, message):
self.recieve_event_queue.put_nowait(message) self.recieve_event_queue.put_nowait(message)
while True: running = True
while running:
try: try:
req = None req = None
if not self.ws: if not self.ws:
@ -56,7 +66,11 @@ class NostrClient:
# be sure the connection is open # be sure the connection is open
await asyncio.sleep(3) await asyncio.sleep(3)
req = await self.send_req_queue.get() 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: except Exception as ex:
logger.warning(ex) logger.warning(ex)
if req: if req:

View file

@ -136,6 +136,17 @@ const merchant = async () => {
caption: `${error}` 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 () { created: async function () {

View file

@ -133,6 +133,21 @@
</div> </div>
<div class="col-12 col-md-5 q-gutter-y-md"> <div class="col-12 col-md-5 q-gutter-y-md">
<div v-if="g.user.admin" class="col-12 q-mb-lg">
<q-card>
<q-card-section class="q-pa-md">
<q-btn
label="Restart Nostr Connection"
color="pink"
@click="restartNostrConnection"
>
<q-tooltip>
Restart the connection to the nostrclient extension
</q-tooltip>
</q-btn>
</q-card-section>
</q-card>
</div>
<div class="col-12"> <div class="col-12">
<q-card> <q-card>
<q-card-section> <q-card-section>

View file

@ -826,6 +826,14 @@ async def api_list_currencies_available():
return list(currencies.keys()) 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) @nostrmarket_ext.delete("/api/v1", status_code=HTTPStatus.OK)
async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)): async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)):
for t in scheduled_tasks: for t in scheduled_tasks: