Skip to content

Commit

Permalink
add JormConfig class, cleanup a little, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Feb 8, 2023
1 parent 50c6dbe commit afd14cf
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions services/voting-node/voting_node/app.py
Expand Up @@ -13,49 +13,69 @@ def heartbeat():
"""Returns 200 if the service is running."""
return None

class JormConfig(object):
def __init__(self, jormungandr_path: str, jcli_path: str):
self.jormungandr_path = jormungandr_path
self.jcli_path = jcli_path

class VotingServer(Server):
def __init__(self, config: Config):
def __init__(self, config: Config, jorm_config: JormConfig):
Server.__init__(self, config)
self.logger = applogs.getLogger(config.log_level)
self.retry_jorm = True
self.jorm_config = jorm_config

def jormungandr_exec(self) -> str:
return self.jorm_config.jormungandr_path

def run(self, jormungandr_path, jcli_path, sockets=None):
asyncio.run(self.start_service(jormungandr_path, jcli_path, sockets=sockets))
def jcli_exec(self) -> str:
return self.jorm_config.jcli_path

async def start_service(self, jormungandr_path, jcli_path, sockets=None):
api_task = asyncio.create_task(self.start_api_server(sockets=sockets))
def run(self, sockets=None):
asyncio.run(self.start_service(sockets=sockets))

def keeps_trying(self) -> bool:
return self.retry_jorm

def stop_trying(self):
self.retry_jorm = False

jorm_task = asyncio.create_task(self.start_jormungandr(jormungandr_path=jormungandr_path, jcli_path=jcli_path))
# Starts Voting Node Service, including this fastAPI server as well as the
# jormungandr node's REST and GRPC servers.
async def start_service(self, sockets=None):
"""Starts Voting Node Service."""
api_task = asyncio.create_task(self.start_api_server(sockets=sockets))

while self.retry_jorm:
# keeps on launching jormungandr until `stop_trying()` is called
while self.keeps_trying():
jorm_task = asyncio.create_task(self.start_jormungandr())
try:
self.logger.debug("jorm task starting")
await jorm_task
self.logger.debug("jorm task is finished")
break
except Exception as e:
self.logger.debug(f"jorm failed to start: {e}")
await asyncio.sleep(1)

await asyncio.sleep(1)
print(await api_task)

async def start_api_server(self, sockets=None):
"""Starts API server for the Voting Node."""
print('starting api')
await self.serve(sockets=sockets)
self.retry_jorm = False
# stops trying to launch jormungandr after API service is finished
self.stop_trying()

async def start_jormungandr(self, jormungandr_path, jcli_path):
async def start_jormungandr(self):
try:
await self.try_to_start_jormungandr(jormungandr_path, jcli_path)
await self.jormungandr_subprocess_exec()
except Exception as e:
f"jorm error: {e}"
raise e


async def try_to_start_jormungandr(self, jormungandr_path, jcli_path):
async def jormungandr_subprocess_exec(self):
try:
proc = await asyncio.create_subprocess_exec(f"{jormungandr_path}", #"--help",
proc = await asyncio.create_subprocess_exec(f"{self.jormungandr_exec()}", #"--help",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
Expand All @@ -72,8 +92,10 @@ async def try_to_start_jormungandr(self, jormungandr_path, jcli_path):
self.logger.warning(f"jorm node error: {e}")
raise e

# Use this to run your service
def run(jormungandr_path, jcli_path, host="127.0.0.1", port=8000, log_level="info"):
"""Entrypoint to running the service."""
config = Config(app=app, host=host, port=port, log_level=log_level)
server = VotingServer(config=config)
server.run(jormungandr_path=jormungandr_path, jcli_path=jcli_path)
"""Main entrypoint to running the service."""
api_config = Config(app=app, host=host, port=port, log_level=log_level)
jorm_config = JormConfig(jormungandr_path=jormungandr_path, jcli_path=jcli_path)
server = VotingServer(config=api_config, jorm_config=jorm_config)
server.run()

0 comments on commit afd14cf

Please sign in to comment.