Complete Lab 5 grade 4 async attachment pipeline

This commit is contained in:
2026-11-16 16:46:44 +03:00
parent 276dbdad8a
commit f9246b8844
52 changed files with 4023 additions and 50 deletions
+7 -2
View File
@@ -40,8 +40,13 @@ def main():
assert 'X-Instance-Id' in response['headers']
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
ast.parse(file.read_text(),filename=str(file))
python_files = []
for directory in ('src', 'tests', 'scripts', 'student-tests'):
folder = ROOT / directory
if folder.exists():
python_files.extend(folder.rglob('*.py'))
for file in python_files:
ast.parse(file.read_text(encoding='utf-8'),filename=str(file))
for file in ROOT.rglob('*.json'):
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
continue
+61
View File
@@ -0,0 +1,61 @@
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$secretDir = Join-Path $repo 'secrets'
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
function New-Secret {
$bytes = New-Object byte[] 32
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
}
function New-AccessKey {
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
$bytes = New-Object byte[] 20
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
$value = -join ($bytes | ForEach-Object { $alphabet[($_ % $alphabet.Length)] })
return $value
}
function Read-Secret([string]$name) {
$path = Join-Path $secretDir $name
if (-not (Test-Path -LiteralPath $path)) { return $null }
return [System.IO.File]::ReadAllText($path).Trim()
}
function Write-Secret([string]$name, [string]$value) {
$path = Join-Path $secretDir $name
[System.IO.File]::WriteAllText($path, $value, [System.Text.UTF8Encoding]::new($false))
}
function Ensure-Pair([string]$first, [string]$second, [scriptblock]$newFirst, [scriptblock]$newSecond) {
$left = Read-Secret $first
$right = Read-Secret $second
if (($null -eq $left) -ne ($null -eq $right)) {
throw "Only one file of credential pair '$first'/'$second' exists. Resolve it manually; credentials are never rotated automatically."
}
if ($null -eq $left) {
$left = & $newFirst
$right = & $newSecond $left
Write-Secret $first $left
Write-Secret $second $right
}
return @($left, $right)
}
$pg = Ensure-Pair 'postgres_password' 'database_url' { New-Secret } {
param($password)
"postgresql+psycopg://messenger:$password@db:5432/messenger"
}
$redis = Ensure-Pair 'redis_password' 'cache_url' { New-Secret } {
param($password)
"redis://:$password@redis:6379/0"
}
$broker = Ensure-Pair 'broker_password' 'broker_url' { New-Secret } {
param($password)
"redis://:$password@broker:6379/0"
}
$s3root = Ensure-Pair 's3_root_access_key_id' 's3_root_secret_access_key' { New-AccessKey } { New-Secret }
$s3api = Ensure-Pair 's3_access_key_id' 's3_secret_access_key' { New-AccessKey } { New-Secret }
Write-Output 'Local Postgres, Redis sessions, Redis Streams broker, RustFS root and limited API credentials are ready under secrets/. Values were not displayed.'
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
docker compose --env-file .env up -d dispatcher worker >/dev/null
docker compose --env-file .env up -d --scale api=2 api lb >/dev/null
deadline=$((SECONDS + 60))
while (( SECONDS < deadline )); do
healthy=0
for container_id in $(docker compose --env-file .env ps -q api); do
state=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)
[[ "$state" == healthy ]] && healthy=$((healthy + 1))
done
if (( healthy >= 2 )) && docker compose --env-file .env exec -T lb wget -q -O /dev/null http://127.0.0.1:8080/health/ready; then
printf 'PASS: two API replicas are healthy through the load balancer.\n'
exit 0
fi
sleep 1
done
printf 'API replicas did not recover within 60 seconds.\n' >&2
docker compose --env-file .env ps
exit 1
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env sh
set -eu
cd "$(dirname "$0")/.."
# Stop entry points and background consumers before the state stores; named volumes remain.
docker compose stop lb api dispatcher worker broker redis db s3
docker compose start db redis broker s3
attempt=0
until docker compose exec -T db pg_isready -U messenger -d messenger >/dev/null 2>&1 \
&& docker compose exec -T redis sh -ec 'REDISCLI_AUTH="$(cat /run/secrets/redis_password)" redis-cli ping | grep -q PONG' >/dev/null 2>&1 \
&& docker compose exec -T broker sh -ec 'REDISCLI_AUTH="$(cat /run/secrets/broker_password)" redis-cli ping | grep -q PONG' >/dev/null 2>&1 \
&& docker compose exec -T s3 curl -fsS http://127.0.0.1:9000/health/ready >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge 60 ]; then
echo 'Database, cache, broker, or object store did not become ready within 60 seconds.' >&2
exit 1
fi
sleep 1
done
docker compose --env-file .env up -d migrate s3-bootstrap
docker compose --env-file .env up -d dispatcher worker
docker compose --env-file .env up -d --scale api=2 api lb
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
for container_id in $(docker compose ps -q api); do
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
if [[ "$hostname" == "$TARGET_INSTANCE_ID" ]]; then
mkdir -p evidence
printf '%s %s\n' "$(date -u +%FT%TZ)" "$TARGET_INSTANCE_ID" >> evidence/stopped-replicas.txt
docker stop --time 6 "$container_id" >/dev/null
exit 0
fi
done
printf 'No running API container matches TARGET_INSTANCE_ID.\n' >&2
exit 1