askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
replicate_file.py · 2.7 KB · 98 lines
| 1 | """ |
| 2 | Cluster File Replication |
| 3 | |
| 4 | POST /cluster/replicate_file - Receive file replication from another station |
| 5 | |
| 6 | Used for high availability: when an object stores a file on station1, |
| 7 | it automatically replicates to station2 and station3. If station1 fails, |
| 8 | files can be accessed from replicas. |
| 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, json_error |
| 15 | |
| 16 | |
| 17 | def POST(request): |
| 18 | """ |
| 19 | Receive file replication from another station |
| 20 | |
| 21 | Request (multipart/form-data): |
| 22 | file: uploaded file data |
| 23 | object_id: str (form field) |
| 24 | filename: str (form field) |
| 25 | source_station: str (form field) |
| 26 | |
| 27 | Response: |
| 28 | { |
| 29 | "status": "ok", |
| 30 | "message": "File replicated", |
| 31 | "object_id": str, |
| 32 | "filename": str, |
| 33 | "size": int |
| 34 | } |
| 35 | """ |
| 36 | # Get form data |
| 37 | try: |
| 38 | object_id = request.forms.get('object_id') |
| 39 | filename = request.forms.get('filename') |
| 40 | source_station = request.forms.get('source_station') |
| 41 | except Exception as e: |
| 42 | return json_error(f'Failed to parse form data: {e}', status=400) |
| 43 | |
| 44 | if not all([object_id, filename, source_station]): |
| 45 | return json_error( |
| 46 | 'Missing required fields: object_id, filename, source_station', |
| 47 | status=400 |
| 48 | ) |
| 49 | |
| 50 | # Get uploaded file |
| 51 | if not request.files or 'file' not in request.files: |
| 52 | return json_error('No file uploaded', status=400) |
| 53 | |
| 54 | upload = request.files.get('file') |
| 55 | file_content = upload.file.read() |
| 56 | |
| 57 | # Write to local file storage (replica) |
| 58 | files_dir = Path(f'data/files/{object_id}') |
| 59 | files_dir.mkdir(parents=True, exist_ok=True) |
| 60 | file_path = files_dir / filename |
| 61 | |
| 62 | # Write file (last-write-wins, no timestamp checking needed for files) |
| 63 | try: |
| 64 | with open(file_path, 'wb') as f: |
| 65 | f.write(file_content) |
| 66 | except Exception as e: |
| 67 | return json_error(f'Failed to write file: {e}', status=500) |
| 68 | |
| 69 | return json_response(json.dumps({ |
| 70 | 'status': 'ok', |
| 71 | 'message': 'File replicated', |
| 72 | 'object_id': object_id, |
| 73 | 'filename': filename, |
| 74 | 'size': len(file_content), |
| 75 | 'source_station': source_station, |
| 76 | 'timestamp': time.time() |
| 77 | })) |
| 78 | |
| 79 | |
| 80 | def GET(request): |
| 81 | """ |
| 82 | Get file replication status for monitoring |
| 83 | |
| 84 | Response: |
| 85 | { |
| 86 | "status": "ok", |
| 87 | "station_id": str, |
| 88 | "message": "File replication endpoint active" |
| 89 | } |
| 90 | """ |
| 91 | station_id = os.environ.get('STATION_ID', 'unknown') |
| 92 | |
| 93 | return json_response(json.dumps({ |
| 94 | 'status': 'ok', |
| 95 | 'station_id': station_id, |
| 96 | 'message': 'File replication endpoint active', |
| 97 | 'timestamp': time.time() |
| 98 | })) |