Readding websocket reconnection functions

This commit is contained in:
Vivek Teega 2020-09-08 03:25:31 +05:30
parent 4cb07c2647
commit c344777f22

View File

@ -2213,6 +2213,7 @@ def scanBlockchain():
# At this point the script has updated to the latest block # At this point the script has updated to the latest block
# Now we connect to flosight's websocket API to get information about the latest blocks # Now we connect to flosight's websocket API to get information about the latest blocks
# Configuration of required variables # Configuration of required variables
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
@ -2220,8 +2221,7 @@ logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s') formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
file_handler = logging.FileHandler('tracking.log') file_handler = logging.FileHandler('tracking.log')
file_handler.setLevel(logging.INFO) file_handler.setLevel(logging.INFO)file_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler() stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter) stream_handler.setFormatter(formatter)
@ -2302,22 +2302,60 @@ if args.reset == 1:
# MAIN LOGIC # MAIN LOGIC
# scan from the latest block saved locally to latest network block # scan from the latest block saved locally to latest network block
scanBlockchain() scanBlockchain()
scanBlockchain()
def switchNeturl(neturl):
testserverlist = ['http://0.0.0.0:9000/', 'https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
mainserverlist = ['http://0.0.0.0:9001/', 'https://flosight.duckdns.org/', 'https://explorer.mediciland.com/', 'https://livenet.flocha.in/']
if config['DEFAULT']['NET'] == 'mainnet':
neturlindex = mainserverlist.index(neturl)
if neturlindex+1 >= len(mainserverlist):
return mainserverlist[neturlindex+1 - len(mainserverlist)]
else:
return mainserverlist[neturlindex+1]
elif config['DEFAULT']['NET'] == 'testnet':
neturlindex = testserverlist.index(neturl)
if neturlindex+1 >= len(testserverlist):
return testserverlist[neturlindex+1 - len(testserverlist)]
else:
return testserverlist[neturlindex+1]
def reconnectSSE(neturl):
# Connect to Flosight websocket to get data on new incoming blocks # Connect to Flosight websocket to get data on new incoming blocks
sio = socketio.Client(reconnection=True) sio = socketio.Client(reconnection=False)
try:
sio.connect( neturl + "socket.io/socket.io.js") sio.connect( neturl + "socket.io/socket.io.js")
except:
logger.debug(f"Could not connect to the websocket endpoint {neturl}. Switching & retrying to next")
neturl = switchNeturl(neturl)
reconnectSSE(neturl)
@sio.on('connect') @sio.on('connect')
def on_connect(): def on_connect():
logger.debug('Token Tracker has connected to websocket') logger.debug(f"Token Tracker has connected to websocket endpoint {neturl}")
sio.emit('subscribe', 'inv') sio.emit('subscribe', 'inv')
@sio.on('disconnect') @sio.on('disconnect')
def disconnect(): def disconnect():
logger.debug('Token Tracker disconnected from websocket') logger.debug(f"Token Tracker disconnected from websocket endpoint {neturl}")
logger.debug('The script will rescan from the latest block in local db to latest server on FLO network') logger.debug('The script will rescan from the latest block in local db to latest server on FLO network')
scanBlockchain() scanBlockchain()
logger.debug("Rescan completed")
logger.debug('Attempting reconnect to websocket ...') logger.debug('Attempting reconnect to websocket ...')
reconnectSSE(neturl)
@sio.on('connect_error')
def connect_error():
logger.debug(f"CONNECTION_ERROR to the websocket endpoint {neturl}. Switching & retrying to next")
neturl = switchNeturl(neturl)
reconnectSSE(neturl)
@sio.on('block')
def on_block(data):
logger.debug('New block received')
logger.debug(str(data))
processApiBlock(data)
# At this point the script has updated to the latest block # At this point the script has updated to the latest block
# Now we connect to flosight's websocket API to get information about the latest blocks # Now we connect to flosight's websocket API to get information about the latest blocks