Merge pull request #42 from lnbits/force_reconnect_to_nostrclient
Force reconnect to nostrclient
This commit is contained in:
commit
fe78cffce1
5 changed files with 59 additions and 3 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,12 @@ 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()
|
||||||
|
self.ws = None
|
||||||
|
|
||||||
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 +45,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 +57,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 +67,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(str(req))
|
||||||
|
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:
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,23 @@ const merchant = async () => {
|
||||||
caption: `${error}`
|
caption: `${error}`
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
restartNostrConnection: async function () {
|
||||||
|
LNbits.utils
|
||||||
|
.confirmDialog(
|
||||||
|
'Are you sure you want to reconnect to the nostrcient extension?'
|
||||||
|
)
|
||||||
|
.onOk(async () => {
|
||||||
|
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 () {
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue