Browse Source

retry logic for when host server is offline during startup of host block

main
Michael Pilosov 2 years ago
parent
commit
34a8310f6f
  1. 33
      example_block/eden-server/announce.py

33
example_block/eden-server/announce.py

@ -28,9 +28,20 @@ async def announce_server():
@sio.event @sio.event
async def connect(): async def connect():
await sio.emit('register', {'name': SERVER_NAME, 'ip': SERVER_IP, 'port': SERVER_PORT}) await sio.emit('register', {'name': SERVER_NAME, 'ip': SERVER_IP, 'port': SERVER_PORT})
print("Announced server to host")
async def main(): async def main():
# retry until we connect to the host
while True:
try:
await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}') await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}')
break
except Exception as e:
print(e)
print("Failed to connect to host, retrying in 5 seconds")
await asyncio.sleep(5)
# await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}')
print("Connected to host")
@sio.on("heartbeat") @sio.on("heartbeat")
async def on_heartbeat(): async def on_heartbeat():
@ -42,24 +53,6 @@ async def announce_server():
await main() await main()
# def announce_server_decorator(host_block_function):
# @wraps(host_block_function)
# def wrapper(*args, **kwargs):
# loop = asyncio.get_event_loop()
# # Start the server announcement task
# announce_task = loop.create_task(announce_server())
# # Run the original host_block function
# result = host_block_function(*args, **kwargs)
# # Cancel the announcement task after the host_block function is done
# announce_task.cancel()
# return result
# return wrapper
def announce_server_decorator(host_block_function): def announce_server_decorator(host_block_function):
@wraps(host_block_function) @wraps(host_block_function)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
@ -69,14 +62,10 @@ def announce_server_decorator(host_block_function):
# Announce the server to the host # Announce the server to the host
await announce_server() await announce_server()
# announce_task = loop.create_task(announce_server())
# run announcement task infinitely in background but allow host_block to run
# await asyncio.gather(announce_task, host_block_thread)
# Wait for host_block to finish # Wait for host_block to finish
await host_block_thread await host_block_thread
# announce_task.cancel()
return asyncio.run(main()) return asyncio.run(main())
return wrapper return wrapper

Loading…
Cancel
Save