Fix cluster client uptime and worker display issues

- Fix cluster client uptime calculation to start from 0 by using explicit UTC timestamps
- Fix cluster client workers not showing by populating cluster_master.clients dictionary
- Ensure connected_at uses current UTC time instead of database CURRENT_TIMESTAMP
parent 8845dc86
......@@ -181,6 +181,9 @@ class ClusterMaster:
client_id = self._get_client_by_websocket(websocket)
if client_id:
update_cluster_client_last_seen(client_id)
# Update in-memory last_seen
if client_id in self.clients:
self.clients[client_id]['last_seen'] = time.time()
return {'type': 'heartbeat_ack'}
elif msg_type == 'pong':
......@@ -215,14 +218,28 @@ class ClusterMaster:
except:
ip_address = '127.0.0.1'
# Save client to database
# Save client to database with current connection time
import time
weight = client_info.get('weight', 100)
save_cluster_client(client_id, token, hostname, ip_address, weight, gpu_info, available_backends)
connected_at = time.time() # Current UTC time
save_cluster_client(client_id, token, hostname, ip_address, weight, gpu_info, available_backends, connected_at)
# Store in memory for websocket management
self.client_websockets[client_id] = websocket
self.tokens[token] = client_id
# Store client info in memory
self.clients[client_id] = {
'token': token,
'hostname': hostname,
'ip_address': ip_address,
'weight': weight,
'gpu_info': gpu_info,
'available_backends': available_backends,
'connected': True,
'last_seen': time.time()
}
# If this is the first client and weight wasn't explicitly set, change master weight to 0
from .database import get_connected_cluster_clients
connected_clients = get_connected_cluster_clients()
......
......@@ -1641,20 +1641,30 @@ def get_all_client_driver_preferences(hostname: str, token: str) -> dict:
# Cluster client management functions
def save_cluster_client(client_id: str, token: str, hostname: str, ip_address: str = None,
weight: int = 100, gpu_info: dict = None, available_backends: list = None) -> bool:
weight: int = 100, gpu_info: dict = None, available_backends: list = None,
connected_at: float = None) -> bool:
"""Save or update a cluster client in the database."""
import json
import time
conn = get_db_connection()
cursor = conn.cursor()
gpu_info_json = json.dumps(gpu_info) if gpu_info else None
available_backends_json = json.dumps(available_backends) if available_backends else None
# Use provided connected_at or current time
if connected_at is None:
connected_at = time.time()
# Convert to datetime string for database storage
from datetime import datetime
connected_at_str = datetime.utcfromtimestamp(connected_at).strftime('%Y-%m-%d %H:%M:%S')
config = get_db_config()
if config['type'] == 'mysql':
cursor.execute('''
INSERT INTO cluster_clients (client_id, token, hostname, ip_address, weight, gpu_info, available_backends, connected, connected_at, last_seen)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
hostname = VALUES(hostname),
ip_address = VALUES(ip_address),
......@@ -1662,16 +1672,16 @@ def save_cluster_client(client_id: str, token: str, hostname: str, ip_address: s
gpu_info = VALUES(gpu_info),
available_backends = VALUES(available_backends),
connected = 1,
connected_at = CURRENT_TIMESTAMP,
connected_at = VALUES(connected_at),
last_seen = CURRENT_TIMESTAMP
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json))
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_str))
else:
# For SQLite, update connected_at on each successful connection
# For SQLite
cursor.execute('''
INSERT OR REPLACE INTO cluster_clients
(client_id, token, hostname, ip_address, weight, gpu_info, available_backends, connected, connected_at, last_seen)
VALUES (?, ?, ?, ?, ?, ?, ?, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json))
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, CURRENT_TIMESTAMP)
''', (client_id, token, hostname, ip_address, weight, gpu_info_json, available_backends_json, connected_at_str))
conn.commit()
success = cursor.rowcount > 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment