askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
cluster_heartbeat_daemon.py · 5.2 KB · 181 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Cluster Heartbeat Daemon |
| 4 | |
| 5 | Runs on worker stations to send periodic heartbeats to master. |
| 6 | |
| 7 | Usage: |
| 8 | python cluster_heartbeat_daemon.py <master_host> |
| 9 | |
| 10 | Example: |
| 11 | python cluster_heartbeat_daemon.py localhost |
| 12 | python cluster_heartbeat_daemon.py 192.0.2.1 |
| 13 | """ |
| 14 | import os |
| 15 | import sys |
| 16 | import time |
| 17 | import socket |
| 18 | import requests |
| 19 | from pathlib import Path |
| 20 | |
| 21 | try: |
| 22 | import psutil |
| 23 | except ImportError: |
| 24 | psutil = None |
| 25 | |
| 26 | |
| 27 | def get_local_ip(): |
| 28 | """Get local IP address""" |
| 29 | try: |
| 30 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 31 | s.connect(('8.8.8.8', 80)) |
| 32 | local_ip = s.getsockname()[0] |
| 33 | s.close() |
| 34 | return local_ip |
| 35 | except: |
| 36 | return '127.0.0.1' |
| 37 | |
| 38 | |
| 39 | def get_load_metrics(): |
| 40 | """ |
| 41 | Collect current load metrics |
| 42 | |
| 43 | Returns: |
| 44 | { |
| 45 | 'cpu_percent': float, # CPU usage (0-100) |
| 46 | 'memory_percent': float, # Memory usage (0-100) |
| 47 | 'memory_used_mb': float, # Memory used in MB |
| 48 | 'memory_total_mb': float, # Total memory in MB |
| 49 | 'object_count': int # Number of objects available |
| 50 | } |
| 51 | """ |
| 52 | metrics = {} |
| 53 | |
| 54 | if psutil: |
| 55 | # CPU usage (averaged over 1 second) |
| 56 | metrics['cpu_percent'] = psutil.cpu_percent(interval=0.1) |
| 57 | |
| 58 | # Memory usage |
| 59 | mem = psutil.virtual_memory() |
| 60 | metrics['memory_percent'] = mem.percent |
| 61 | metrics['memory_used_mb'] = round(mem.used / (1024 * 1024), 1) |
| 62 | metrics['memory_total_mb'] = round(mem.total / (1024 * 1024), 1) |
| 63 | else: |
| 64 | # Fallback if psutil not available |
| 65 | metrics['cpu_percent'] = 0 |
| 66 | metrics['memory_percent'] = 0 |
| 67 | metrics['memory_used_mb'] = 0 |
| 68 | metrics['memory_total_mb'] = 0 |
| 69 | |
| 70 | # Count objects (count .py files in examples/) |
| 71 | try: |
| 72 | examples_dir = Path('examples') |
| 73 | if examples_dir.exists(): |
| 74 | object_count = len([ |
| 75 | f for f in examples_dir.rglob('*.py') |
| 76 | if f.name != '__init__.py' and '__pycache__' not in str(f) |
| 77 | ]) |
| 78 | metrics['object_count'] = object_count |
| 79 | else: |
| 80 | metrics['object_count'] = 0 |
| 81 | except: |
| 82 | metrics['object_count'] = 0 |
| 83 | |
| 84 | return metrics |
| 85 | |
| 86 | |
| 87 | def get_version(): |
| 88 | """Read version from VERSION file""" |
| 89 | try: |
| 90 | version_file = Path('VERSION') |
| 91 | if version_file.exists(): |
| 92 | return version_file.read_text().strip() |
| 93 | except: |
| 94 | pass |
| 95 | return 'unknown' |
| 96 | |
| 97 | |
| 98 | def send_heartbeat(master_host, master_port, station_id, local_ip, station_port): |
| 99 | """Send heartbeat to master with load metrics and version""" |
| 100 | try: |
| 101 | # Collect load metrics |
| 102 | metrics = get_load_metrics() |
| 103 | |
| 104 | # Get version |
| 105 | version = get_version() |
| 106 | |
| 107 | url = f'http://{master_host}:{master_port}/cluster/heartbeat' |
| 108 | data = { |
| 109 | 'station_id': station_id, |
| 110 | 'host': local_ip, |
| 111 | 'port': station_port, |
| 112 | 'metrics': metrics, |
| 113 | 'version': version |
| 114 | } |
| 115 | response = requests.post(url, json=data, timeout=5) |
| 116 | |
| 117 | if response.status_code == 200: |
| 118 | result = response.json() |
| 119 | print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Heartbeat sent: {result.get('message')}") |
| 120 | return True |
| 121 | else: |
| 122 | print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Heartbeat failed: {response.status_code}") |
| 123 | return False |
| 124 | except Exception as e: |
| 125 | print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Heartbeat error: {e}") |
| 126 | return False |
| 127 | |
| 128 | |
| 129 | def main(): |
| 130 | import argparse |
| 131 | |
| 132 | parser = argparse.ArgumentParser(description="Cluster Heartbeat Daemon") |
| 133 | parser.add_argument("master_host", help="Master station hostname or IP") |
| 134 | parser.add_argument("--master-port", type=int, default=8001, help="Master station port (default: 8001)") |
| 135 | parser.add_argument("--station-port", type=int, default=8001, help="This station's port (default: 8001)") |
| 136 | parser.add_argument("--station-id", help="Station ID (overrides STATION_ID env var)") |
| 137 | args = parser.parse_args() |
| 138 | |
| 139 | master_host = args.master_host |
| 140 | master_port = args.master_port |
| 141 | station_port = args.station_port |
| 142 | station_id = args.station_id or os.environ.get('STATION_ID') |
| 143 | |
| 144 | if not station_id: |
| 145 | print("Error: STATION_ID not provided via --station-id or STATION_ID environment variable") |
| 146 | sys.exit(1) |
| 147 | |
| 148 | if station_id == 'station1': |
| 149 | print("This is the master station - no need to run heartbeat daemon") |
| 150 | sys.exit(0) |
| 151 | |
| 152 | local_ip = get_local_ip() |
| 153 | |
| 154 | print("=" * 60) |
| 155 | print("Cluster Heartbeat Daemon") |
| 156 | print("=" * 60) |
| 157 | print(f"Station ID: {station_id}") |
| 158 | print(f"Local IP: {local_ip}") |
| 159 | print(f"Station Port: {station_port}") |
| 160 | print(f"Master: {master_host}:{master_port}") |
| 161 | print(f"Heartbeat interval: 10 seconds") |
| 162 | print() |
| 163 | print("Press Ctrl+C to stop") |
| 164 | print("=" * 60) |
| 165 | print() |
| 166 | |
| 167 | # Initial registration |
| 168 | print(f"Registering with master...") |
| 169 | send_heartbeat(master_host, master_port, station_id, local_ip, station_port) |
| 170 | |
| 171 | # Send heartbeats every 10 seconds |
| 172 | try: |
| 173 | while True: |
| 174 | time.sleep(10) |
| 175 | send_heartbeat(master_host, master_port, station_id, local_ip, station_port) |
| 176 | except KeyboardInterrupt: |
| 177 | print("\nHeartbeat daemon stopped") |
| 178 | |
| 179 | |
| 180 | if __name__ == "__main__": |
| 181 | main() |