added extend symbol change for ws

This commit is contained in:
2026-04-30 04:32:49 +00:00
parent dc3409ac40
commit 1ac0909c21
20 changed files with 28960 additions and 1221 deletions

View File

@@ -4,11 +4,10 @@ import logging
import os
import traceback
from datetime import datetime
from typing import AsyncContextManager
import valkey
from dotenv import load_dotenv
# from sqlalchemy.ext.asyncio import create_async_engine
import modules.utils as utils
'''
TO DO:
@@ -16,75 +15,62 @@ TO DO:
'''
### Database ###
CON: AsyncContextManager | None = None
VAL_KEY = None
VK_IN = 'fr_orchestrator_input'
VK_OUT = 'fr_orchestrator_output'
VK_IN: str = 'fr_orchestrator_input'
VK_OUT: str = 'fr_orchestrator_output'
# CONFIG_FILEPATH: str = '/algo_local_drive/algo_config.json'
CONFIG_FILEPATH: str = 'algo_config.json'
### Logging ###
load_dotenv()
LOG_FILEPATH: str = os.getenv("LOGS_PATH") + '/Fund_Rate_Algo_Orchestrator.log'
LOG_FILEPATH: str = f'{os.getenv("LOGS_PATH")}/Fund_Rate_Algo_Orchestrator.log'
ALGO_CONFIG: None | dict
async def orchestrator() -> None:
global ALGO_CONFIG
async def main() -> None:
VAL_KEY: valkey.Valkey = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)
try:
VK_PUBSUB = VAL_KEY.pubsub()
VK_PUBSUB: valkey.client.PubSub = VAL_KEY.pubsub()
VK_PUBSUB.subscribe(VK_IN)
logging.info(f"Subscribed to '{VK_IN}'. Waiting for messages...")
logging.info(msg=f"Subscribed to '{VK_IN}'. Waiting for messages...")
for message in VK_PUBSUB.listen():
if message['type'] == 'message':
timestamp = round(datetime.now().timestamp()*1000)
data = json.loads(message['data'])
# channel = message['channel']
timestamp: int = round(number=datetime.now().timestamp()*1000)
with open('/algo_local_drive/algo_config.json', 'r', encoding='utf-8') as f:
# ALGO_CONFIG = json.load(f, object_hook=lambda d: Algo_Config(**d))
ALGO_CONFIG = json.load(f)
ALGO_CONFIG['Updated_Timestamp'] = timestamp
for k, v in data.items():
if ALGO_CONFIG.get(k, None) is not None:
ALGO_CONFIG[k] = v
# Receive Update Msg from PubSub
data: dict = json.loads(s=message['data'])
VAL_KEY.set(VK_OUT, json.dumps(ALGO_CONFIG))
with open('/algo_local_drive/algo_config.json', 'w', encoding='utf-8') as f:
json.dump(ALGO_CONFIG, f, indent=4)
logging.info(f"Algo Config Updated @ {timestamp}; {data}")
# Load Config File
with open(file=CONFIG_FILEPATH, mode='r', encoding='utf-8') as f:
Algo_Config: dict = json.load(fp=f)
Algo_Config['Updated_Timestamp'] = timestamp
# Update Config w Update Data
Algo_Config: dict = utils.rec_set_dict(orig_dict=Algo_Config, new_dict=data)
# Set VK KV w Updated Config
VAL_KEY.set(name=VK_OUT, value=json.dumps(obj=Algo_Config))
# Save Updated Config to File
with open(file=CONFIG_FILEPATH, mode='w', encoding='utf-8') as f:
json.dump(obj=Algo_Config, fp=f, indent=4)
logging.info(msg=f"Algo Config Updated @ {timestamp}; {data}")
except valkey.exceptions.ConnectionError as e:
logging.info(f"Could not connect to Valkey. Please check the publish server is up; {e}")
logging.info(msg=f"Could not connect to Valkey. Please check the publish server is up; {e}")
except KeyboardInterrupt:
logging.info('ORCHESTRATOR SHUTTING DOWN...')
logging.info(msg='ORCHESTRATOR SHUTTING DOWN...')
except Exception as e:
logging.error(traceback.format_exc())
logging.critical(f'*** ORCHESTRATOR CRASHED: {e}')
logging.error(msg=traceback.format_exc())
logging.critical(msg=f'*** ORCHESTRATOR CRASHED: {e}')
### MAIN STARTUP ###
async def main() -> None:
global VAL_KEY
global CON
global ALGO_CONFIG
VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)
# engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
with open('/algo_local_drive/algo_config.json', 'r', encoding='utf-8') as f:
# ALGO_CONFIG = json.load(f, object_hook=lambda d: Algo_Config(**d))
ALGO_CONFIG = json.load(f)
ALGO_CONFIG['Updated_Timestamp'] = round(datetime.now().timestamp()*1000)
# async with engine.connect() as CON:
await orchestrator()
if __name__ == '__main__':
START_TIME = round(datetime.now().timestamp()*1000)
START_TIME: int = round(number=datetime.now().timestamp()*1000)
logging.info(f'Log FilePath: {LOG_FILEPATH}')
logging.info(msg=f'Log FilePath: {LOG_FILEPATH}')
logging.basicConfig(
force=True,
@@ -93,6 +79,6 @@ if __name__ == '__main__':
format='%(asctime)s - %(levelname)s - %(message)s',
filemode='w'
)
logging.info(f"STARTED: {START_TIME}")
logging.info(msg=f"STARTED: {START_TIME}")
asyncio.run(main())