Complete Lab 5: implementation, fault scenarios, and Track 1 report

This commit is contained in:
2026-11-09 23:39:53 +03:00
parent 9049c404ed
commit fb8a43920e
32 changed files with 1252 additions and 18 deletions
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env python3
"""Create local-only secret files once; do not print their values."""
from pathlib import Path
import secrets
root=Path(__file__).resolve().parents[1]
d=root/'secrets'; d.mkdir(mode=0o700,exist_ok=True); d.chmod(0o700)
def ensure(name,value):
p=d/name
if not p.exists(): p.write_text(value)
p.chmod(0o600)
ensure('db_password',secrets.token_urlsafe(32))
ensure('rustfs_root_user','RUSTFSROOT')
ensure('rustfs_root_password',secrets.token_urlsafe(32))
ensure('s3_access_key_id','messengerapi')
ensure('s3_secret_access_key',secrets.token_urlsafe(40))
p=d/'database_url'; ensure('database_url','postgresql://messenger:'+ (d/'db_password').read_text().strip()+'@postgres:5432/messenger')
ensure('cache_url','redis://redis:6379/0')
ensure('broker_url','redis://broker:6379/0')
env=root/'.env'
if not env.exists(): env.write_text('API_REPLICAS=2\nAPP_PORT=8081\nSESSION_TTL_SECONDS=3600\nWORKER_CONCURRENCY=2\nMAX_QUEUED_JOBS=500\nJOB_MAX_ATTEMPTS=5\nJOB_TIMEOUT_SECONDS=60\n')
env.chmod(0o600)
print('Local secrets created or preserved; values hidden.')
+18
View File
@@ -0,0 +1,18 @@
"""Explicitly replay one terminal failed attachment job, never a ready job."""
import sys, uuid
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from app.migrate import setting
import psycopg
if len(sys.argv)!=2:
raise SystemExit('usage: python scripts/replay_failed.py ATTACHMENT_UUID')
aid=str(uuid.UUID(sys.argv[1]))
with psycopg.connect(setting('DATABASE_URL')) as c:
row=c.execute("SELECT j.job_id,j.state,a.status FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=%s FOR UPDATE OF j,a",(aid,)).fetchone()
if not row or row[1]!='failed' or row[2]!='failed':
raise SystemExit('replay refused: attachment must be terminal failed')
c.execute("UPDATE attachment_jobs SET state='pending',attempt=0,available_at=clock_timestamp(),lease_until=NULL,last_error=NULL WHERE job_id=%s",(row[0],))
c.execute("UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=%s",(aid,))
c.execute("INSERT INTO attachment_job_audit(job_id,event,detail) VALUES (%s,'operator_replay','explicit replay command')",(row[0],))
print('failed job queued for explicit replay')
+1
View File
@@ -0,0 +1 @@
'bash scripts/restart.sh'
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
# Recreate real containers, retain PostgreSQL volume. No migration race.
docker compose down
docker compose up -d --no-build --pull never --wait --wait-timeout 90
+1
View File
@@ -0,0 +1 @@
bash scripts/stop-one.sh
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID from a real API response}"
for id in $(docker compose ps -q api); do
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
case "$TARGET_INSTANCE_ID" in
"$host"-*)
printf 'Stopping API container %s (hostname %s)\n' "$id" "$host"
docker stop --time 5 "$id"
docker inspect -f '{{.Id}} {{.State.Status}}' "$id"
exit 0;;
esac
done
printf 'No running API matches target\n' >&2
exit 1