Implement durable image processing pipeline with outbox and recovery
This commit is contained in:
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
DC = str(ROOT / "scripts/dc")
|
||||
|
||||
STORES = ["postgres", "redis", "s3", "broker"]
|
||||
PROCESSES = ["api", "lb", "publisher", "worker", "cleanup"]
|
||||
SERVICES = STORES + PROCESSES
|
||||
|
||||
|
||||
def compose(*args, timeout=60):
|
||||
result = subprocess.run(
|
||||
[DC, *args],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout,
|
||||
)
|
||||
if result.returncode:
|
||||
raise RuntimeError("Compose failed: " + " ".join(args))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def snapshot():
|
||||
ids = compose("ps", "-a", "-q", *SERVICES).split()
|
||||
if not ids:
|
||||
raise RuntimeError("No lab5 containers found")
|
||||
|
||||
result = subprocess.run(
|
||||
["docker", "inspect", *ids],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=15,
|
||||
)
|
||||
|
||||
records = []
|
||||
for item in json.loads(result.stdout):
|
||||
records.append({
|
||||
"service": item["Config"]["Labels"]["com.docker.compose.service"],
|
||||
"container_id": item["Id"],
|
||||
"started_at": item["State"]["StartedAt"],
|
||||
"status": item["State"]["Status"],
|
||||
"volumes": sorted(
|
||||
mount["Name"]
|
||||
for mount in item["Mounts"]
|
||||
if mount["Type"] == "volume"
|
||||
),
|
||||
})
|
||||
return sorted(records, key=lambda item: (item["service"], item["container_id"]))
|
||||
|
||||
|
||||
def main():
|
||||
report = {"before": snapshot(), "result": "INCOMPLETE"}
|
||||
try:
|
||||
print("Stopping API, LB and background processes...", flush=True)
|
||||
compose("stop", "--timeout", "5", *PROCESSES)
|
||||
|
||||
print("Restarting PostgreSQL, Redis, S3 and RabbitMQ...", flush=True)
|
||||
compose("restart", "--no-deps", "--timeout", "10", *STORES)
|
||||
|
||||
deadline = time.monotonic() + 20
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
compose(
|
||||
"exec", "-T", "postgres",
|
||||
"pg_isready", "-U", "messenger", "-d", "messenger",
|
||||
timeout=4,
|
||||
)
|
||||
break
|
||||
except (RuntimeError, subprocess.TimeoutExpired):
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise RuntimeError("PostgreSQL readiness timeout")
|
||||
|
||||
print("Starting API, LB and background processes...", flush=True)
|
||||
compose("up", "-d", "--no-deps", "--no-build", *PROCESSES)
|
||||
|
||||
after = snapshot()
|
||||
report["after"] = after
|
||||
|
||||
for service in SERVICES:
|
||||
old = [item for item in report["before"] if item["service"] == service]
|
||||
new = [item for item in after if item["service"] == service]
|
||||
|
||||
if not old or len(old) != len(new):
|
||||
raise RuntimeError(f"Unexpected container count: {service}")
|
||||
|
||||
old_starts = {item["started_at"] for item in old}
|
||||
if any(item["started_at"] in old_starts for item in new):
|
||||
raise RuntimeError(f"Restart not observed: {service}")
|
||||
|
||||
for service in STORES:
|
||||
old_volumes = {
|
||||
volume
|
||||
for item in report["before"] if item["service"] == service
|
||||
for volume in item["volumes"]
|
||||
}
|
||||
new_volumes = {
|
||||
volume
|
||||
for item in after if item["service"] == service
|
||||
for volume in item["volumes"]
|
||||
}
|
||||
if not old_volumes or old_volumes != new_volumes:
|
||||
raise RuntimeError(f"Persistent volumes changed: {service}")
|
||||
|
||||
report["result"] = "HOOK_COMPLETED"
|
||||
print("Restart hook completed; persistent volume names preserved.", flush=True)
|
||||
|
||||
finally:
|
||||
path = ROOT / "evidence" / "restart_containers.json"
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
path.write_text(json.dumps(report, indent=2) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user