add function to send WireGuard stats and update sync interval handling

This commit is contained in:
Eduardo Silva
2026-01-08 13:52:10 -03:00
parent b40e9afcb6
commit 61b35601f3

View File

@@ -244,6 +244,28 @@ class ClusterWorker:
self.config_version = new_config_version self.config_version = new_config_version
logger.info(f"Configuration updated to version {self.config_version}") logger.info(f"Configuration updated to version {self.config_version}")
def send_stats(self):
try:
stats = self.func_process_wireguard_status()
params = {
'token': TOKEN,
'worker_config_version': self.config_version,
'worker_dns_version': self.dns_version,
'worker_version': WORKER_VERSION
}
logger.info("Sending WireGuard stats to Master...")
response = self.session.post(f"{self.base_url}/worker/submit_wireguard_stats/", json=stats, params=params, timeout=REQUEST_TIMEOUT)
if response.status_code == 200:
logger.info("Stats sent successfully.")
return True
else:
logger.error(f"Failed to send stats. Status: {response.status_code}")
return False
except Exception as e:
logger.error(f"Error sending stats: {e}")
return False
def run(self): def run(self):
if not self.should_run: if not self.should_run:
return return
@@ -253,9 +275,18 @@ class ClusterWorker:
# Initial cleanup # Initial cleanup
self.cleanup_wireguard() self.cleanup_wireguard()
last_config_check = 0
last_stats_send = 0
stats_sync_interval = 60 # Default initial value
while True: while True:
current_time = time.time()
# Check Config (Fixed 60s interval)
if current_time - last_config_check >= 60:
try: try:
response = self.get_status() response = self.get_status()
last_config_check = time.time()
if response is not None: if response is not None:
if response.status_code == 403: if response.status_code == 403:
@@ -272,7 +303,12 @@ class ClusterWorker:
if response.status_code == 200: if response.status_code == 200:
data = response.json() data = response.json()
remote_config_version = data.get('cluster_settings', {}).get('config_version', 0)
# Update stats interval from master settings
cluster_settings = data.get('cluster_settings', {})
stats_sync_interval = cluster_settings.get('stats_sync_interval', 60)
remote_config_version = cluster_settings.get('config_version', 0)
# Check WireGuard Config # Check WireGuard Config
updated = False updated = False
@@ -287,7 +323,7 @@ class ClusterWorker:
logger.error("Failed to download config files.") logger.error("Failed to download config files.")
# Check DNS Config # Check DNS Config
remote_dns_version = int(data.get('cluster_settings', {}).get('dns_version', 0)) remote_dns_version = int(cluster_settings.get('dns_version', 0))
if not updated and remote_dns_version != self.dns_version: if not updated and remote_dns_version != self.dns_version:
logger.info(f"DNS version mismatch (Local: {self.dns_version}, Remote: {remote_dns_version}). Updating...") logger.info(f"DNS version mismatch (Local: {self.dns_version}, Remote: {remote_dns_version}). Updating...")
if self.download_dns_config(): if self.download_dns_config():
@@ -298,11 +334,15 @@ class ClusterWorker:
logger.info(f"No changes detected. Configuration is up to date (WG: {self.config_version}, DNS: {self.dns_version}).") logger.info(f"No changes detected. Configuration is up to date (WG: {self.config_version}, DNS: {self.dns_version}).")
except Exception as e: except Exception as e:
logger.error(f"Unexpected error in main loop: {e}") logger.error(f"Unexpected error in config check loop: {e}")
interval = 60 # Check Stats Sync
logger.info(f"Waiting {interval} seconds for next check...") if current_time - last_stats_send >= stats_sync_interval:
time.sleep(interval) self.send_stats()
last_stats_send = time.time()
# Sleep briefly to be responsive but not busy loop
time.sleep(1)
# Final loop state if 403 was received # Final loop state if 403 was received
while not self.should_run: while not self.should_run: