Factor out `_start_bg_tasks` from timelord.

This commit is contained in:
Richard Kiss 2020-05-20 11:13:21 -07:00 committed by wjblanke
parent 5b840ff44d
commit 1b1ee7bc4b
3 changed files with 29 additions and 32 deletions

View File

@ -13,12 +13,36 @@ except ImportError:
from src.server.outbound_message import NodeType
from src.server.server import ChiaServer
from src.timelord import Timelord
from src.types.peer_info import PeerInfo
from src.util.config import load_config_cli, load_config
from src.util.default_root import DEFAULT_ROOT_PATH
from src.util.logging import initialize_logging
from src.util.setproctitle import setproctitle
def start_timelord_bg_task(server, peer_info, log):
"""
Start a background task that checks connection and reconnects periodically to the full_node.
"""
async def connection_check():
while True:
if server is not None:
full_node_retry = True
for connection in server.global_connections.get_connections():
if connection.get_peer_info() == peer_info:
full_node_retry = False
if full_node_retry:
log.info(f"Reconnecting to full_node {peer_info}")
if not await server.start_client(peer_info, None, auth=False):
await asyncio.sleep(1)
await asyncio.sleep(30)
return asyncio.create_task(connection_check())
async def async_main():
root_path = DEFAULT_ROOT_PATH
net_config = load_config(root_path, "config.yaml")
@ -64,8 +88,10 @@ async def async_main():
await asyncio.sleep(10) # Allows full node to startup
timelord.set_server(server)
timelord._start_bg_tasks()
peer_info = PeerInfo(
config["full_node_peer"]["host"], config["full_node_peer"]["port"]
)
bg_task = start_timelord_bg_task(server, peer_info, log)
vdf_server = asyncio.ensure_future(coro)
@ -79,6 +105,7 @@ async def async_main():
await server.await_closed()
vdf_server.cancel()
bg_task.cancel()
log.info("Timelord fully closed.")

View File

@ -10,7 +10,6 @@ from chiavdf import create_discriminant
from src.protocols import timelord_protocol
from src.server.outbound_message import Delivery, Message, NodeType, OutboundMessage
from src.types.classgroup import ClassgroupElement
from src.types.peer_info import PeerInfo
from src.types.proof_of_time import ProofOfTime
from src.types.sized_bytes import bytes32
from src.util.api_decorators import api_request
@ -53,34 +52,6 @@ class Timelord:
def set_server(self, server):
self.server = server
def _start_bg_tasks(self):
"""
Start a background task that checks connection and reconnects periodically to the full_node.
"""
full_node_peer = PeerInfo(
self.config["full_node_peer"]["host"], self.config["full_node_peer"]["port"]
)
async def connection_check():
while not self._is_shutdown:
if self.server is not None:
full_node_retry = True
for connection in self.server.global_connections.get_connections():
if connection.get_peer_info() == full_node_peer:
full_node_retry = False
if full_node_retry:
log.info(f"Reconnecting to full_node {full_node_retry}")
if not await self.server.start_client(
full_node_peer, None, auth=False
):
await asyncio.sleep(1)
await asyncio.sleep(30)
self.reconnect_task = asyncio.create_task(connection_check())
async def _handle_client(self, reader: StreamReader, writer: StreamWriter):
async with self.lock:
client_ip = writer.get_extra_info("peername")[0]

View File

@ -345,7 +345,6 @@ async def setup_timelord(port, dic={}):
vdf_server = asyncio.ensure_future(coro)
timelord.set_server(server)
timelord._start_bg_tasks()
async def run_timelord():
async for msg in timelord._manage_discriminant_queue():