saving
This commit is contained in:
@@ -2,19 +2,20 @@ import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from typing import AsyncContextManager
|
||||
|
||||
import valkey
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
# from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
'''
|
||||
TO DO:
|
||||
- Insert config changes into database for analysis later / general tracking
|
||||
'''
|
||||
|
||||
### Database ###
|
||||
EXTEND_CLIENT = None
|
||||
CON: AsyncContextManager | None = None
|
||||
VAL_KEY = None
|
||||
VK_IN = 'fr_orchestrator_input'
|
||||
@@ -24,12 +25,12 @@ VK_OUT = 'fr_orchestrator_output'
|
||||
load_dotenv()
|
||||
LOG_FILEPATH: str = os.getenv("LOGS_PATH") + '/Fund_Rate_Algo_Orchestrator.log'
|
||||
|
||||
### ALGO GLOBALS ###
|
||||
ASTER_ALLOW_ORDERING: bool = True
|
||||
EXTEND_ALLOW_ORDERING: bool = True
|
||||
LOOP_SLEEP_SEC = 1
|
||||
ALGO_CONFIG: None | dict
|
||||
# ALGO_CONFIG: None | Algo_Config = None
|
||||
|
||||
async def orchestrator() -> None:
|
||||
global ALGO_CONFIG
|
||||
|
||||
try:
|
||||
VK_PUBSUB = VAL_KEY.pubsub()
|
||||
VK_PUBSUB.subscribe(VK_IN)
|
||||
@@ -37,11 +38,20 @@ async def orchestrator() -> None:
|
||||
print(f"Subscribed to '{VK_IN}'. Waiting for messages...")
|
||||
|
||||
for message in VK_PUBSUB.listen():
|
||||
# Valkey sends a 'subscribe' message first; we usually want to skip it
|
||||
if message['type'] == 'message':
|
||||
data = message['data']
|
||||
channel = message['channel']
|
||||
print(f"[{channel}] Received: {data}")
|
||||
timestamp = round(datetime.now().timestamp()*1000)
|
||||
data = json.loads(message['data'])
|
||||
# channel = message['channel']
|
||||
|
||||
for k, v in data.items():
|
||||
if ALGO_CONFIG.get(k, None) is not None:
|
||||
ALGO_CONFIG[k] = v
|
||||
|
||||
ALGO_CONFIG['Config_Updated_Timestamp'] = timestamp
|
||||
VAL_KEY.set(VK_OUT, json.dumps(ALGO_CONFIG))
|
||||
with open('algo_config.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(ALGO_CONFIG, f, indent=4)
|
||||
print(f"Algo Config Updated @ {timestamp}; {data}")
|
||||
|
||||
except valkey.exceptions.ConnectionError as e:
|
||||
print(f"Could not connect to Valkey. Please check the publish server is up; {e}")
|
||||
@@ -53,15 +63,20 @@ async def orchestrator() -> None:
|
||||
|
||||
### MAIN STARTUP ###
|
||||
async def main() -> None:
|
||||
global EXTEND_CLIENT
|
||||
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')
|
||||
# engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
|
||||
|
||||
async with engine.connect() as CON:
|
||||
await orchestrator()
|
||||
with open('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['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)
|
||||
|
||||
Reference in New Issue
Block a user