Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
replicate_file.py · 2.7 KB · 98 lines
1"""
2Cluster File Replication
3
4POST /cluster/replicate_file - Receive file replication from another station
5
6Used for high availability: when an object stores a file on station1,
7it automatically replicates to station2 and station3. If station1 fails,
8files can be accessed from replicas.
9"""
10import json
11import os
12import time
13from pathlib import Path
14from dbbasic_web.responses import json as json_response, json_error
15
16
17def 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
80def 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 }))