(20241231) Too many thing happening...

This commit is contained in:
2024-12-31 07:34:49 +00:00
parent f67263320e
commit a07c762586
13 changed files with 406 additions and 162 deletions
+20 -2
View File
@@ -43,7 +43,8 @@ import os
from utils_v2.string import json
from utils_v2.system import files
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.queue.async_kafka import ConsumerKafka, get_ssl_context
from utils_v2.queue.kafka.controllers.async_kafka import ConsumerKafka, get_ssl_context
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
# To make HTTP calls:
import httpx
@@ -98,6 +99,9 @@ EVENT_DISCONNECT = "disconnect"
EVENT_ECHO = "echo"
EVENT_TICKS = "ticks"
# Redis:
redis_cache = None
# *****************************************************************************************************************
# ***** ****
@@ -137,12 +141,19 @@ async def handle_connect(sid, environ) -> bool:
async with lock:
connected_clients[sid] = {
"user": None,
"redisKey": f"io_{environ.get('HTTP_X_SESSION_TOKEN')}",
"rooms": []
}
# Allow/reject requests:
printer(sid)
print("ENVIRON:", json.to_string(environ, default = str))
while redis_cache is None: await asyncio.sleep(0.5)
result = await redis_cache.set(
key = connected_clients[sid]["redisKey"],
value = {"server": SERVER_HOSTNAME, "socket_id": sid}
)
print("CACHED:", result)
return True
@@ -152,6 +163,8 @@ async def handle_connect(sid, environ) -> bool:
@sio.on(event = EVENT_DISCONNECT, namespace = NAMESPACE_MODULE)
async def handle_disconnect(sid, reason) -> None:
printer(sid, reason)
result = await redis_cache.delete(key = connected_clients[sid]["redisKey"])
print("UN-CACHED:", result)
# ---------------------------------------------------------------------------------------------------------------------
@@ -313,7 +326,7 @@ async def ticks_from_kafka(
# Each message must be treated as an array of tick updates (list of dicts).
# In case the producer is sending each individual tick as a separate message,
# we normalize it to be a list:
tasks = [send_ticks(t["value"] if isinstance(t["value"], list) else [t["value"]]) for t in ticks]
tasks = [send_ticks(t.value if isinstance(t.value, list) else [t.value]) for t in ticks]
results = await asyncio.gather(*tasks)
printer(len(ticks))
@@ -327,6 +340,11 @@ async def init():
if os.environ["DEBUG"].lower() == "true": printer.enable()
printer("Initializing.")
global redis_cache
redis_cache = AsyncRedisCache(
connection_string = r"redis://:dc4da94197c843ab6a730113c2b801d9@192.168.2.251/0"
)
# Start consuming ticks in the background:
cwd = files.get_cwd()
parent_dir = cwd