import json import os from pathlib import Path import subprocess import sys import time sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from client import Client, check def compose(*args): return subprocess.check_output(["docker", "compose", *args], text=True, timeout=90).strip() def recreate(service, setting): env = dict(os.environ) env.update(setting) subprocess.run(["docker", "compose", "up", "-d", "--no-deps", "--force-recreate", service], env=env, check=True, timeout=90, stdout=subprocess.DEVNULL) def job_for(attachment): code = f""" from storage import Store s = Store() try: with s.pool.connection() as db: print(db.execute('SELECT id FROM jobs WHERE attachment_id=%s', ('{attachment["id"]}',)).fetchone()['id']) finally: s.close() """ return compose("exec", "-T", "-w", "/app/src", "api-a", "python", "-c", code) def main(): c = Client() actor, _, _, chat = c.bootstrap() recreate("dispatcher", {"CRASH_BEFORE_PUBLISH": "1"}) attachment, _ = c.attachment(actor, chat) try: time.sleep(1) check(compose("ps", "-q", "--status", "running", "dispatcher") == "", "dispatcher did not crash") finally: recreate("dispatcher", {}) c.wait_ready(actor, attachment) print("PASS: COMMIT/publish crash recovered from outbox", flush=True) recreate("worker", {"CRASH_AFTER_OBJECTS": "1"}) attachment, _ = c.attachment(actor, chat) try: deadline = time.monotonic() + 30 while compose("ps", "-q", "--status", "running", "worker"): check(time.monotonic() < deadline, "worker did not crash") time.sleep(0.5) finally: recreate("worker", {}) time.sleep(72) c.wait_ready(actor, attachment) job_id = job_for(attachment) payload = {"schema_version": 1, "job_id": job_id, "attachment_id": attachment["id"], "pipeline_version": 1, "attempt": 1, "type": "attachment.process.v1", "created_at": "2026-11-30T00:00:00Z"} compose("exec", "-T", "broker", "redis-cli", "XADD", "images", "*", "payload", json.dumps(payload)) time.sleep(1) ready = c.wait_ready(actor, attachment) check(len(ready["variants"]) == 3, "duplicate variants") print("PASS: crash after S3 effect, lease recovery and duplicate delivery", flush=True) if __name__ == "__main__": main()