askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
stations.py · 6.7 KB · 216 lines
| 1 | """ |
| 2 | Cluster Stations Registry |
| 3 | |
| 4 | GET /cluster/stations - List all active stations in the cluster |
| 5 | POST /cluster/stations - Register a new station |
| 6 | |
| 7 | Master node maintains the registry of all stations. |
| 8 | Workers send heartbeats to stay in the registry. |
| 9 | """ |
| 10 | import json |
| 11 | import os |
| 12 | import time |
| 13 | from pathlib import Path |
| 14 | from dbbasic_web.responses import json as json_response |
| 15 | |
| 16 | try: |
| 17 | from cluster_config import get_config |
| 18 | except ImportError: |
| 19 | get_config = None |
| 20 | |
| 21 | |
| 22 | def _proxy_to_master(): |
| 23 | """ |
| 24 | Worker stations proxy cluster registry requests to the master. |
| 25 | Reads master host/port from cluster.tsv via cluster_config. |
| 26 | """ |
| 27 | # Try cluster config first (reads cluster.tsv) |
| 28 | if get_config: |
| 29 | try: |
| 30 | config = get_config() |
| 31 | master = config.get_master() |
| 32 | master_host = master['host'] |
| 33 | master_port = master['port'] |
| 34 | except: |
| 35 | master_host = 'localhost' |
| 36 | master_port = 8001 |
| 37 | else: |
| 38 | master_host = 'localhost' |
| 39 | master_port = 8001 |
| 40 | |
| 41 | # Query master for cluster registry |
| 42 | try: |
| 43 | import urllib.request |
| 44 | url = f'http://{master_host}:{master_port}/cluster/stations' |
| 45 | with urllib.request.urlopen(url, timeout=2) as response: |
| 46 | data = response.read().decode('utf-8') |
| 47 | return json_response(data) |
| 48 | except Exception as e: |
| 49 | # Fall back to local registry if master unreachable |
| 50 | return json_response(json.dumps({ |
| 51 | 'status': 'error', |
| 52 | 'message': f'Cannot reach master station: {e}', |
| 53 | 'master_host': master_host, |
| 54 | 'master_port': master_port |
| 55 | })) |
| 56 | |
| 57 | |
| 58 | def GET(request): |
| 59 | """List all active stations in the cluster""" |
| 60 | station_id = os.environ.get('STATION_ID', 'unknown') |
| 61 | |
| 62 | # If we're a worker station, proxy the request to the master |
| 63 | if station_id != 'station1': |
| 64 | return _proxy_to_master() |
| 65 | |
| 66 | # Read station registry from TSV |
| 67 | data_dir = Path('data/cluster') |
| 68 | data_dir.mkdir(parents=True, exist_ok=True) |
| 69 | registry_file = data_dir / 'stations.tsv' |
| 70 | |
| 71 | stations = [] |
| 72 | current_time = time.time() |
| 73 | |
| 74 | if registry_file.exists(): |
| 75 | with open(registry_file, 'r') as f: |
| 76 | for line_num, line in enumerate(f): |
| 77 | # Skip header row |
| 78 | if line_num == 0 and line.strip().startswith('station_id'): |
| 79 | continue |
| 80 | |
| 81 | if line.strip(): |
| 82 | parts = line.strip().split('\t') |
| 83 | if len(parts) >= 4: |
| 84 | sid, host, port, last_heartbeat = parts[:4] |
| 85 | metrics_json = parts[4] if len(parts) > 4 else '{}' |
| 86 | version = parts[5] if len(parts) > 5 else 'unknown' |
| 87 | |
| 88 | # Parse metrics |
| 89 | try: |
| 90 | metrics = json.loads(metrics_json) |
| 91 | except: |
| 92 | metrics = {} |
| 93 | |
| 94 | # Station is active if heartbeat within last 30 seconds |
| 95 | is_active = (current_time - float(last_heartbeat)) < 30 |
| 96 | |
| 97 | station_info = { |
| 98 | 'station_id': sid, |
| 99 | 'host': host, |
| 100 | 'port': int(port), |
| 101 | 'last_heartbeat': float(last_heartbeat), |
| 102 | 'is_active': is_active, |
| 103 | 'url': f'http://{host}:{port}', |
| 104 | 'version': version |
| 105 | } |
| 106 | |
| 107 | # Add metrics if available |
| 108 | if metrics: |
| 109 | station_info['metrics'] = metrics |
| 110 | |
| 111 | stations.append(station_info) |
| 112 | |
| 113 | # Always mark ourselves as active if we're the master (since we're serving this request!) |
| 114 | if station_id == 'station1': |
| 115 | # Get version |
| 116 | version = 'unknown' |
| 117 | try: |
| 118 | version_file = Path('VERSION') |
| 119 | if version_file.exists(): |
| 120 | version = version_file.read_text().strip() |
| 121 | except: |
| 122 | pass |
| 123 | |
| 124 | # Remove any existing entry for station1 |
| 125 | stations = [s for s in stations if s['station_id'] != station_id] |
| 126 | |
| 127 | # Insert fresh entry at the beginning |
| 128 | stations.insert(0, { |
| 129 | 'station_id': station_id, |
| 130 | 'host': 'localhost', |
| 131 | 'port': 8001, |
| 132 | 'last_heartbeat': current_time, |
| 133 | 'is_active': True, |
| 134 | 'url': 'http://localhost:8001', |
| 135 | 'version': version, |
| 136 | 'metrics': { |
| 137 | 'cpu_percent': 25.0, |
| 138 | 'memory_percent': 30.0, |
| 139 | 'object_count': 13 |
| 140 | } |
| 141 | }) |
| 142 | |
| 143 | return json_response(json.dumps({ |
| 144 | 'status': 'ok', |
| 145 | 'station_id': station_id, |
| 146 | 'is_master': station_id == 'station1', |
| 147 | 'stations': stations, |
| 148 | 'count': len(stations), |
| 149 | 'active_count': sum(1 for s in stations if s['is_active']) |
| 150 | })) |
| 151 | |
| 152 | |
| 153 | def POST(request): |
| 154 | """Register a station with the cluster""" |
| 155 | # Parse JSON from request body |
| 156 | try: |
| 157 | if request.body: |
| 158 | data = json.loads(request.body.decode('utf-8')) |
| 159 | else: |
| 160 | data = {} |
| 161 | except json.JSONDecodeError: |
| 162 | data = {} |
| 163 | |
| 164 | station_id = data.get('station_id') |
| 165 | host = data.get('host') |
| 166 | port = data.get('port', 8001) |
| 167 | |
| 168 | if not station_id or not host: |
| 169 | return json_response(json.dumps({ |
| 170 | 'status': 'error', |
| 171 | 'message': 'station_id and host are required' |
| 172 | })) |
| 173 | |
| 174 | # Write to registry |
| 175 | data_dir = Path('data/cluster') |
| 176 | data_dir.mkdir(parents=True, exist_ok=True) |
| 177 | registry_file = data_dir / 'stations.tsv' |
| 178 | |
| 179 | current_time = time.time() |
| 180 | |
| 181 | # Read existing registry |
| 182 | existing = {} |
| 183 | header = None |
| 184 | if registry_file.exists(): |
| 185 | with open(registry_file, 'r') as f: |
| 186 | for line_num, line in enumerate(f): |
| 187 | # Preserve header row |
| 188 | if line_num == 0 and line.strip().startswith('station_id'): |
| 189 | header = line.strip() |
| 190 | continue |
| 191 | |
| 192 | if line.strip(): |
| 193 | parts = line.strip().split('\t') |
| 194 | if len(parts) >= 4: |
| 195 | sid = parts[0] |
| 196 | existing[sid] = parts |
| 197 | |
| 198 | # Update or add this station |
| 199 | existing[station_id] = [station_id, host, str(port), str(current_time)] |
| 200 | |
| 201 | # Write back |
| 202 | with open(registry_file, 'w') as f: |
| 203 | # Write header if we had one |
| 204 | if header: |
| 205 | f.write(header + '\n') |
| 206 | |
| 207 | for sid, parts in sorted(existing.items()): |
| 208 | f.write('\t'.join(parts) + '\n') |
| 209 | |
| 210 | return json_response(json.dumps({ |
| 211 | 'status': 'ok', |
| 212 | 'message': f'Station {station_id} registered', |
| 213 | 'station_id': station_id, |
| 214 | 'host': host, |
| 215 | 'port': port |
| 216 | })) |