Skip to content

Commit

Permalink
feat: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
keivanipchihagh committed Mar 19, 2024
1 parent 87a9659 commit 764a9b6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
File renamed without changes.
14 changes: 12 additions & 2 deletions src/rpc/athena/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import grpc

# Third-party imports
from base_clienty import BaseClient
from src.rpc.base_client import BaseClient
from protos.athena import athena_pb2
from protos.athena import athena_pb2_grpc
from google.protobuf.json_format import MessageToDict


class AthenaClient(BaseClient):
Expand All @@ -29,6 +31,14 @@ def __init__(
- port (int): Server port number. Defaults to `50051`.
"""
super().__init__(host, port)

self.channel = grpc.insecure_channel(self.target)
self.stub = athena_pb2_grpc.AthenaStub(self.channel)

self.is_server_ready() # Check server rediness


def get_backtest(self) -> dict:
request = athena_pb2.GetBacktestRequest() # Create request
response = self.stub.GetBacktest(request) # Get response
return MessageToDict(response) # Return as dict
16 changes: 8 additions & 8 deletions src/rpc/athena/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
from protos.athena import athena_pb2_grpc


class BacktestService(athena_pb2_grpc.AthenaServicer):
class GetBacktestService(athena_pb2_grpc.AthenaServicer):

def Backtest(
def GetBacktest(
self,
request: athena_pb2.RequestBacktest,
request: athena_pb2.GetBacktestRequest,
context: grpc.ServicerContext,
) -> athena_pb2.ResponseBacktest:
) -> athena_pb2.GetBacktestResponse:
"""
Executes a backtest based on the provided request.
Parameters:
- request (athena_pb2.RequestBacktest): The backtest request.
- request (athena_pb2.GetBacktestRequest): The backtest request.
- context (grpc.ServicerContext): The gRPC context.
Returns:
- (athena_pb2.ResponseBacktest): The backtest response.
- (athena_pb2.GetBacktestResponse): The backtest response.
"""
response = athena_pb2.ResponseBacktest()
response = athena_pb2.GetBacktestResponse()
return response


Expand All @@ -55,4 +55,4 @@ def __init__(
super().__init__(host, port, n_workers)

# Register servicers
athena_pb2_grpc.add_AthenaServicer_to_server(BacktestService(), self.server)
athena_pb2_grpc.add_AthenaServicer_to_server(GetBacktestService(), self.server)
4 changes: 4 additions & 0 deletions src/rpc/base_clienty.py → src/rpc/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import grpc

# Third-party imports
from src.utils import logger


class BaseClient(object):

Expand Down Expand Up @@ -45,6 +48,7 @@ def is_server_ready(self, timeout: int = 1) -> bool:
grpc.channel_ready_future(self.channel).result(timeout)
return True
except grpc.FutureTimeoutError:
logger.warning(f"Server not ready on {self.target}!")
return False
except Exception as ex:
raise ex
2 changes: 1 addition & 1 deletion src/rpc/base_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def start(self) -> None:
"""
Starts the server in blocking mode.
"""
logger.debug(f"Starting server on {self.target}")
logger.debug(f"Listening on '{self.target}'")
self.server.start()
self.server.wait_for_termination()

0 comments on commit 764a9b6

Please sign in to comment.