actually exit on disconnect

This commit is contained in:
Lex Berezhny 2022-08-29 10:51:28 -04:00
parent 888bd85470
commit 8bd7008fdc

View file

@ -3,6 +3,7 @@ import asyncio
import json
import socket
import random
import sys
from time import perf_counter
from collections import defaultdict
from typing import Dict, Optional, Tuple
@ -197,6 +198,10 @@ class Network:
def jurisdiction(self):
return self.config.get("jurisdiction")
@property
def exit_on_disconnect(self):
return self.config["exit_on_disconnect"]
def disconnect(self):
if self._keepalive_task and not self._keepalive_task.done():
self._keepalive_task.cancel()
@ -373,7 +378,13 @@ class Network:
def rpc(self, list_or_method, args, restricted=True, session: Optional[ClientSession] = None):
if session or self.is_connected:
session = session or self.client
try:
return session.send_request(list_or_method, args)
except asyncio.TimeoutError:
if self.exit_on_disconnect:
log.error("exiting on server disconnect")
sys.exit(1)
raise
else:
self._urgent_need_reconnect.set()
raise ConnectionError("Attempting to send rpc request when connection is not available.")
@ -387,9 +398,16 @@ class Network:
try:
return await function(*args, **kwargs)
except asyncio.TimeoutError:
if self.exit_on_disconnect:
log.error("Wallet server call timed out, exiting on server disconnect.")
sys.exit(1)
else:
log.warning("Wallet server call timed out, retrying.")
except ConnectionError:
log.warning("connection error")
if self.exit_on_disconnect:
log.error("exiting on server disconnect")
sys.exit(1)
raise asyncio.CancelledError() # if we got here, we are shutting down
def _update_remote_height(self, header_args):