askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
stations.py · 4.9 KB · 157 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 | |
| 17 | def GET(request): |
| 18 | """List all active stations in the cluster""" |
| 19 | station_id = os.environ.get('STATION_ID', 'unknown') |
| 20 | |
| 21 | # Read station registry from TSV |
| 22 | data_dir = Path('data/cluster') |
| 23 | data_dir.mkdir(parents=True, exist_ok=True) |
| 24 | registry_file = data_dir / 'stations.tsv' |
| 25 | |
| 26 | stations = [] |
| 27 | current_time = time.time() |
| 28 | |
| 29 | if registry_file.exists(): |
| 30 | with open(registry_file, 'r') as f: |
| 31 | for line in f: |
| 32 | if line.strip(): |
| 33 | parts = line.strip().split('\t') |
| 34 | if len(parts) >= 4: |
| 35 | sid, host, port, last_heartbeat = parts[:4] |
| 36 | metrics_json = parts[4] if len(parts) > 4 else '{}' |
| 37 | version = parts[5] if len(parts) > 5 else 'unknown' |
| 38 | |
| 39 | # Parse metrics |
| 40 | try: |
| 41 | metrics = json.loads(metrics_json) |
| 42 | except: |
| 43 | metrics = {} |
| 44 | |
| 45 | # Station is active if heartbeat within last 30 seconds |
| 46 | is_active = (current_time - float(last_heartbeat)) < 30 |
| 47 | |
| 48 | station_info = { |
| 49 | 'station_id': sid, |
| 50 | 'host': host, |
| 51 | 'port': int(port), |
| 52 | 'last_heartbeat': float(last_heartbeat), |
| 53 | 'is_active': is_active, |
| 54 | 'url': f'http://{host}:{port}', |
| 55 | 'version': version |
| 56 | } |
| 57 | |
| 58 | # Add metrics if available |
| 59 | if metrics: |
| 60 | station_info['metrics'] = metrics |
| 61 | |
| 62 | stations.append(station_info) |
| 63 | |
| 64 | # Always mark ourselves as active if we're the master (since we're serving this request!) |
| 65 | if station_id == 'station1': |
| 66 | # Get version |
| 67 | version = 'unknown' |
| 68 | try: |
| 69 | version_file = Path('VERSION') |
| 70 | if version_file.exists(): |
| 71 | version = version_file.read_text().strip() |
| 72 | except: |
| 73 | pass |
| 74 | |
| 75 | # Remove any existing entry for station1 |
| 76 | stations = [s for s in stations if s['station_id'] != station_id] |
| 77 | |
| 78 | # Insert fresh entry at the beginning |
| 79 | stations.insert(0, { |
| 80 | 'station_id': station_id, |
| 81 | 'host': 'localhost', |
| 82 | 'port': 8001, |
| 83 | 'last_heartbeat': current_time, |
| 84 | 'is_active': True, |
| 85 | 'url': 'http://localhost:8001', |
| 86 | 'version': version, |
| 87 | 'metrics': { |
| 88 | 'cpu_percent': 25.0, |
| 89 | 'memory_percent': 30.0, |
| 90 | 'object_count': 13 |
| 91 | } |
| 92 | }) |
| 93 | |
| 94 | return json_response(json.dumps({ |
| 95 | 'status': 'ok', |
| 96 | 'station_id': station_id, |
| 97 | 'is_master': station_id == 'station1', |
| 98 | 'stations': stations, |
| 99 | 'count': len(stations), |
| 100 | 'active_count': sum(1 for s in stations if s['is_active']) |
| 101 | })) |
| 102 | |
| 103 | |
| 104 | def POST(request): |
| 105 | """Register a station with the cluster""" |
| 106 | # Parse JSON from request body |
| 107 | try: |
| 108 | if request.body: |
| 109 | data = json.loads(request.body.decode('utf-8')) |
| 110 | else: |
| 111 | data = {} |
| 112 | except json.JSONDecodeError: |
| 113 | data = {} |
| 114 | |
| 115 | station_id = data.get('station_id') |
| 116 | host = data.get('host') |
| 117 | port = data.get('port', 8001) |
| 118 | |
| 119 | if not station_id or not host: |
| 120 | return json_response(json.dumps({ |
| 121 | 'status': 'error', |
| 122 | 'message': 'station_id and host are required' |
| 123 | })) |
| 124 | |
| 125 | # Write to registry |
| 126 | data_dir = Path('data/cluster') |
| 127 | data_dir.mkdir(parents=True, exist_ok=True) |
| 128 | registry_file = data_dir / 'stations.tsv' |
| 129 | |
| 130 | current_time = time.time() |
| 131 | |
| 132 | # Read existing registry |
| 133 | existing = {} |
| 134 | if registry_file.exists(): |
| 135 | with open(registry_file, 'r') as f: |
| 136 | for line in f: |
| 137 | if line.strip(): |
| 138 | parts = line.strip().split('\t') |
| 139 | if len(parts) >= 4: |
| 140 | sid = parts[0] |
| 141 | existing[sid] = parts |
| 142 | |
| 143 | # Update or add this station |
| 144 | existing[station_id] = [station_id, host, str(port), str(current_time)] |
| 145 | |
| 146 | # Write back |
| 147 | with open(registry_file, 'w') as f: |
| 148 | for sid, parts in sorted(existing.items()): |
| 149 | f.write('\t'.join(parts) + '\n') |
| 150 | |
| 151 | return json_response(json.dumps({ |
| 152 | 'status': 'ok', |
| 153 | 'message': f'Station {station_id} registered', |
| 154 | 'station_id': station_id, |
| 155 | 'host': host, |
| 156 | 'port': port |
| 157 | })) |