minor fixes, rework consumer request, update requirements.txt

This commit is contained in:
Timothy Pogue
2022-08-30 11:04:16 -06:00
parent 47f7c384fb
commit 418bd26a80
11 changed files with 53 additions and 38 deletions

View File

@@ -2,8 +2,7 @@ import logging
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
from freqtrade.enums import RPCMessageType
from freqtrade.rpc.api_server.deps import get_channel_manager
from freqtrade.rpc.api_server.deps import get_channel_manager, get_rpc_optional
from freqtrade.rpc.api_server.ws.utils import is_websocket_alive
@@ -16,7 +15,8 @@ router = APIRouter()
@router.websocket("/message/ws")
async def message_endpoint(
ws: WebSocket,
channel_manager=Depends(get_channel_manager)
channel_manager=Depends(get_channel_manager),
rpc=Depends(get_rpc_optional)
):
try:
if is_websocket_alive(ws):
@@ -31,19 +31,10 @@ async def message_endpoint(
while not channel.is_closed():
request = await channel.recv()
# This is where we'd parse the request. For now this should only
# be a list of topics to subscribe too. List[str]
# Maybe allow the consumer to update the topics subscribed
# during runtime?
# If the request isn't a list then skip it
if not isinstance(request, list):
continue
# Check if all topics listed are an RPCMessageType
if all([any(x.value == topic for x in RPCMessageType) for topic in request]):
logger.debug(f"{ws.client} subscribed to topics: {request}")
channel.set_subscriptions(request)
# Process the request here. Should this be a method of RPC?
if rpc:
logger.info(f"Request: {request}")
rpc._process_consumer_request(request, channel)
except WebSocketDisconnect:
# Handle client disconnects

View File

@@ -63,11 +63,7 @@ class ApiServer(RPCHandler):
ApiServer.__initialized = False
return ApiServer.__instance
def __init__(
self,
config: Dict[str, Any],
standalone: bool = False,
) -> None:
def __init__(self, config: Dict[str, Any], standalone: bool = False) -> None:
ApiServer._config = config
if self.__initialized and (standalone or self._standalone):
return

View File

@@ -19,8 +19,8 @@ from freqtrade.configuration.timerange import TimeRange
from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT
from freqtrade.data.history import load_data
from freqtrade.data.metrics import calculate_max_drawdown
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirection, State,
TradingMode)
from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, RPCMessageType, RPCRequestType,
SignalDirection, State, TradingMode)
from freqtrade.exceptions import ExchangeError, PricingError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_msecs
from freqtrade.loggers import bufferHandler
@@ -1089,6 +1089,16 @@ class RPC:
'last_process_loc': last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT),
'last_process_ts': int(last_p.timestamp()),
}
# We are passed a Channel object, we can only do sync functions on that channel object
def _process_consumer_request(self, request, channel):
# Should we ensure that request is Dict[str, Any]?
type, data = request.get('type'), request.get('data')
if type == RPCRequestType.SUBSCRIBE:
if all([any(x.value == topic for x in RPCMessageType) for topic in data]):
logger.debug(f"{channel} subscribed to topics: {data}")
channel.set_subscriptions(data)
#
# # ------------------------------ EXTERNAL SIGNALS -----------------------
#

View File

@@ -64,7 +64,7 @@ class RPCManager:
""" Stops all enabled rpc modules """
logger.info('Cleaning up rpc modules ...')
while self.registered_modules:
mod = self.registered_modules.pop()
mod = self.registered_modules.pop() # popleft to cleanup API server last?
logger.info('Cleaning up rpc.%s ...', mod.name)
mod.cleanup()
del mod

View File

@@ -436,9 +436,13 @@ class Telegram(RPCHandler):
# Notification disabled
return
message = self.compose_message(msg, msg_type)
self._send_msg(message, disable_notification=(noti == 'silent'))
# Would this be better than adding un-needed if statements to compose_message?
try:
message = self.compose_message(msg, msg_type)
self._send_msg(message, disable_notification=(noti == 'silent'))
except NotImplementedError:
# just skip it
return
def _get_sell_emoji(self, msg):
"""