Karan Adhikari commited on
Enhance client management with DC tracking
Browse filesAdded client_dc_map to track data center IDs for clients and updated logging for client connections.
- Backend/pyrofork/clients.py +23 -5
Backend/pyrofork/clients.py
CHANGED
|
@@ -2,7 +2,7 @@ from asyncio import gather, create_task
|
|
| 2 |
from pyrogram import Client
|
| 3 |
from Backend.logger import LOGGER
|
| 4 |
from Backend.config import Telegram
|
| 5 |
-
from Backend.pyrofork.bot import multi_clients, work_loads, StreamBot
|
| 6 |
from os import environ
|
| 7 |
|
| 8 |
class TokenParser:
|
|
@@ -12,7 +12,7 @@ class TokenParser:
|
|
| 12 |
c + 1: t
|
| 13 |
for c, (_, t) in enumerate(
|
| 14 |
filter(
|
| 15 |
-
lambda n: n[0].startswith("MULTI_TOKEN"),
|
| 16 |
sorted(environ.items())
|
| 17 |
)
|
| 18 |
)
|
|
@@ -31,6 +31,15 @@ async def start_client(client_id, token):
|
|
| 31 |
no_updates=True,
|
| 32 |
in_memory=True
|
| 33 |
).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
work_loads[client_id] = 0
|
| 35 |
return client_id, client
|
| 36 |
except Exception as e:
|
|
@@ -39,6 +48,15 @@ async def start_client(client_id, token):
|
|
| 39 |
|
| 40 |
async def initialize_clients():
|
| 41 |
multi_clients[0], work_loads[0] = StreamBot, 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
all_tokens = TokenParser.parse_from_env()
|
| 43 |
if not all_tokens:
|
| 44 |
LOGGER.info("No additional Bot Clients found, Using default client")
|
|
@@ -46,11 +64,11 @@ async def initialize_clients():
|
|
| 46 |
|
| 47 |
tasks = [create_task(start_client(i, token)) for i, token in all_tokens.items()]
|
| 48 |
clients = await gather(*tasks)
|
| 49 |
-
clients = {client_id: client for client_id, client in clients if client}
|
| 50 |
multi_clients.update(clients)
|
| 51 |
-
|
| 52 |
if len(multi_clients) != 1:
|
| 53 |
LOGGER.info(f"Multi-Client Mode Enabled with {len(multi_clients)} clients")
|
|
|
|
| 54 |
else:
|
| 55 |
LOGGER.info("No additional clients were initialized, using default client")
|
| 56 |
-
|
|
|
|
| 2 |
from pyrogram import Client
|
| 3 |
from Backend.logger import LOGGER
|
| 4 |
from Backend.config import Telegram
|
| 5 |
+
from Backend.pyrofork.bot import multi_clients, work_loads, StreamBot, client_dc_map
|
| 6 |
from os import environ
|
| 7 |
|
| 8 |
class TokenParser:
|
|
|
|
| 12 |
c + 1: t
|
| 13 |
for c, (_, t) in enumerate(
|
| 14 |
filter(
|
| 15 |
+
lambda n: n[0].startswith("MULTI_TOKEN"),
|
| 16 |
sorted(environ.items())
|
| 17 |
)
|
| 18 |
)
|
|
|
|
| 31 |
no_updates=True,
|
| 32 |
in_memory=True
|
| 33 |
).start()
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
client_dc = await client.storage.dc_id()
|
| 37 |
+
client_dc_map[client_id] = client_dc
|
| 38 |
+
LOGGER.info(f"Client {client_id} connected to DC {client_dc}")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
LOGGER.warning(f"Could not get DC for Client {client_id}: {e}")
|
| 41 |
+
client_dc_map[client_id] = None
|
| 42 |
+
|
| 43 |
work_loads[client_id] = 0
|
| 44 |
return client_id, client
|
| 45 |
except Exception as e:
|
|
|
|
| 48 |
|
| 49 |
async def initialize_clients():
|
| 50 |
multi_clients[0], work_loads[0] = StreamBot, 0
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
main_dc = await StreamBot.storage.dc_id()
|
| 54 |
+
client_dc_map[0] = main_dc
|
| 55 |
+
LOGGER.info(f"Main StreamBot connected to DC {main_dc}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
LOGGER.warning(f"Could not get DC for StreamBot: {e}")
|
| 58 |
+
client_dc_map[0] = None
|
| 59 |
+
|
| 60 |
all_tokens = TokenParser.parse_from_env()
|
| 61 |
if not all_tokens:
|
| 62 |
LOGGER.info("No additional Bot Clients found, Using default client")
|
|
|
|
| 64 |
|
| 65 |
tasks = [create_task(start_client(i, token)) for i, token in all_tokens.items()]
|
| 66 |
clients = await gather(*tasks)
|
| 67 |
+
clients = {client_id: client for client_id, client in clients if client}
|
| 68 |
multi_clients.update(clients)
|
| 69 |
+
|
| 70 |
if len(multi_clients) != 1:
|
| 71 |
LOGGER.info(f"Multi-Client Mode Enabled with {len(multi_clients)} clients")
|
| 72 |
+
LOGGER.info(f"DC Distribution: {client_dc_map}")
|
| 73 |
else:
|
| 74 |
LOGGER.info("No additional clients were initialized, using default client")
|
|
|