# Фоновая обработка: сценарии защиты уровней 3 и 4 (scripts/job-scenario.py)
# date: 2026-11-16T14:01:41Z
# commit: 484119a

## restart
$ python3 scripts/job-scenario.py restart
[   0.2s] user 82eeee3d-6dbd-4f88-89f7-f90af93592c1 chat 65981e2e-b48c-436f-8817-7adb465bc180
[   0.2s] $ docker compose stop worker
[   0.9s] POST attachment -> 202 status=queued id=eade9374-e447-4d1a-bcc1-b1bab3b3886b instance=87ae39d58075
[   0.9s] content while queued -> 409
[   1.0s] POST message with 1 attachment(s) -> 201 id=068bed4e-0750-4afe-94c3-409708601758
[   1.0s] POST message with 0 attachment(s) -> 201 id=73f5328e-b50e-463a-9b44-52e094985702
[   8.7s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[   8.7s] $ docker compose restart api rabbitmq
[  26.3s] queues after broker restart: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[  26.3s] status after restarts: queued
[  26.3s] $ docker compose start worker
[  31.5s] GET attachment -> status=queued error_code=None
[  32.5s] GET attachment -> status=ready error_code=None
[  32.5s] original -> 200 sha256 matches upload: True
[  32.5s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  32.6s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  32.6s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  32.7s] job: 27a4684d-3af8-4547-9610-07aba55b7657 | ready | 1 | 5 |
[  32.9s] variants rows: 3
[  33.1s] events: enqueued -> processing -> ready

## crash-after-store
$ python3 scripts/job-scenario.py crash-after-store
[   0.1s] user 870efd23-431d-4c5d-bb6a-fb69fe407107 chat 36e1d058-a50b-468a-8286-41c65ee181ad
[   0.1s] $ docker compose stop worker
[   0.6s] $ docker compose run --no-deps -e WORKER_FAULT=exit-after-store -e JOB_LEASE_SECONDS=15 worker
[   2.2s] POST attachment -> 202 status=queued id=052c555e-1e36-4ca6-aea6-cf7bfa933904 instance=1e38f40634b0
[   3.0s]   one-off: fault injection: worker killed after storing objects, before commit and ack
[   3.1s] one-off process exit code 137 (137 = killed without ack)
[   3.1s] status after crash: processing
[   3.6s] lease: processing | 1 | t
[  11.3s] queues (unacked message returned): name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[  11.3s] $ docker compose start worker
[  15.8s] GET attachment -> status=processing error_code=None
[  18.8s] GET attachment -> status=queued error_code=None
[  19.8s] GET attachment -> status=processing error_code=None
[  20.8s] GET attachment -> status=ready error_code=None
[  20.8s] original -> 200 sha256 matches upload: True
[  20.9s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  20.9s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  20.9s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  21.1s] job: 8922e0b2-1b70-4294-81f5-75dbb82b1006 | ready | 2 | 5 |
[  21.3s] variants rows: 3
[  21.5s] events: enqueued -> processing -> lease_expired -> processing -> ready
[  21.9s]   worker: job 8922e0b2-1b70-4294-81f5-75dbb82b1006: processing, not due or leased elsewhere, skipped
[  21.9s]   worker: job 8922e0b2-1b70-4294-81f5-75dbb82b1006 attempt 1 (redelivered): skipped
[  21.9s]   worker: job 8922e0b2-1b70-4294-81f5-75dbb82b1006 attempt 2: ready
[  22.0s]   relay: lease expired: 1, stale queued republished: 0

## crash-after-commit
$ python3 scripts/job-scenario.py crash-after-commit
[   0.1s] user 892084ce-d560-4b81-8a41-29b037a7f501 chat 15fb5c7e-bb70-4945-8ddf-d26d1d3569d3
[   0.1s] $ docker compose stop worker
[   0.9s] $ docker compose run --no-deps -e WORKER_FAULT=exit-after-commit worker
[   2.7s] POST attachment -> 202 status=queued id=377694eb-cef4-4db3-a1e4-af9053e26e70 instance=1e38f40634b0
[   3.7s]   one-off: fault injection: worker killed after commit, before ack
[   3.8s] one-off process exit code 137 (137 = killed without ack)
[   4.2s] status after crash: ready staging_key: staging/377694eb-cef4-4db3-a1e4-af9053e26e70
[  12.2s] queues (unacked message returned): name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[  12.2s] $ docker compose start worker
[  18.5s] staging_key after redelivery: NULL
[  18.5s] original -> 200 sha256 matches upload: True
[  18.5s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  18.5s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  18.5s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  18.7s] job: 4f105bfb-f45c-445f-9bf9-ffaae7c159e3 | ready | 1 | 5 |
[  18.8s] variants rows: 3
[  19.0s] events: enqueued -> processing -> ready
[  19.3s]   worker: job 4f105bfb-f45c-445f-9bf9-ffaae7c159e3: ready, not due or leased elsewhere, skipped
[  19.3s]   worker: job 4f105bfb-f45c-445f-9bf9-ffaae7c159e3 attempt 1 (redelivered): skipped

## outbox-gap
$ python3 scripts/job-scenario.py outbox-gap
[   0.1s] user f6bbc6d7-414b-47ba-a108-ee447cc4e169 chat 9092c599-a754-4f16-8b09-34f630f34c5b
[   0.1s] $ docker compose stop relay
[   0.7s] POST attachment -> 202 status=queued id=f92b9390-13c8-472a-a730-b2cd64f31a79 instance=87ae39d58075
[   1.0s] outbox after COMMIT: 8 | NOT PUBLISHED | 0
[   8.7s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	0	0
attachment.jobs.dlq	quorum	true	0	0
[   8.7s] $ docker compose kill api
[   9.3s] API killed (SIGKILL) after COMMIT, nothing was published
[   9.3s] $ docker compose start api
[  17.4s] status after API restart: queued
[  17.4s] $ docker compose start relay
[  22.0s] GET attachment -> status=queued error_code=None
[  24.1s] GET attachment -> status=ready error_code=None
[  24.2s] outbox after relay recovery: 8 | 2026-11-16 14:03:19.506+00 | 1
[  24.2s] original -> 200 sha256 matches upload: True
[  24.2s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  24.3s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  24.3s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  24.4s] job: 0ca33e89-2981-4966-93fd-1d591dd83973 | ready | 1 | 5 |
[  24.6s] variants rows: 3
[  24.8s] events: enqueued -> processing -> ready

## relay-crash
$ python3 scripts/job-scenario.py relay-crash
[   0.1s] user a1d5a875-1b08-48c9-926a-b7c0089f274c chat 660bdc6f-b600-44d4-ad7e-cc888fbe1413
[   0.1s] $ docker compose stop relay
[   1.0s] $ docker compose run --no-deps -e RELAY_FAULT=exit-after-confirm relay
[   3.0s] POST attachment -> 202 status=queued id=fb12fbe4-8102-43a9-92b5-bb331ecf9b9d instance=1e38f40634b0
[   3.6s]   one-off: published 1 job message(s), broker confirmed
[   3.6s]   one-off: fault injection: exit after broker confirm, before outbox is marked
[   3.7s] one-off process exit code 137 (137 = killed without ack)
[   4.1s] outbox after relay crash: 9 | NOT MARKED | 0
[   4.1s] $ docker compose start relay
[   8.8s] GET attachment -> status=ready error_code=None
[  11.9s] outbox after relay restart: 9 | 2026-11-16 14:03:31.241+00 | 1
[  12.0s] original -> 200 sha256 matches upload: True
[  12.0s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  12.0s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  12.0s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  12.1s] job: 1ef3a3cd-134f-47b7-83e4-fc74dc77ae29 | ready | 1 | 5 |
[  12.3s] variants rows: 3
[  12.4s] events: enqueued -> processing -> ready
[  12.6s]   worker: job 1ef3a3cd-134f-47b7-83e4-fc74dc77ae29 attempt 1: ready
[  12.6s]   worker: job 1ef3a3cd-134f-47b7-83e4-fc74dc77ae29: ready, not due or leased elsewhere, skipped
[  12.6s]   worker: job 1ef3a3cd-134f-47b7-83e4-fc74dc77ae29 attempt 1: skipped

## retry-replay
$ python3 scripts/job-scenario.py retry-replay
[   0.1s] user 766c68fb-94b6-4104-ac84-5166f401ee4f chat e9627c1d-9123-4962-9eed-d7a8282742b3
[   0.1s] $ docker compose stop worker
[   0.6s] POST attachment -> 202 status=queued id=5b6058ab-2875-47a3-9458-0f266742fd97 instance=1e38f40634b0
[   0.6s] $ docker compose stop s3
[   1.2s] $ docker start worker (without dependencies)
[   1.6s] GET attachment -> status=queued error_code=None
[   2.6s] GET attachment -> status=processing error_code=None
[   3.6s] GET attachment -> status=queued error_code=None
[   4.6s] GET attachment -> status=processing error_code=None
[   5.6s] GET attachment -> status=queued error_code=None
[   6.6s] GET attachment -> status=processing error_code=None
[   7.6s] GET attachment -> status=queued error_code=None
[  10.7s] GET attachment -> status=processing error_code=None
[  11.7s] GET attachment -> status=queued error_code=None
[  19.7s] GET attachment -> status=processing error_code=None
[  20.7s] GET attachment -> status=failed error_code=retry_exhausted
[  20.7s] POST message with 1 attachment(s) -> 409 id=None
[  20.7s] failed attachment in a new message -> 409
[  20.7s] retries:
[  20.9s]    1 | enqueued |  | 14:03:34.662
[  20.9s]    1 | processing |  | 14:03:36.116
[  20.9s]    1 | retry_scheduled | temporary failure: ApiError; next attempt 2 in 533 ms | 14:03:37.143
[  20.9s]    2 | processing |  | 14:03:37.813
[  20.9s]    2 | retry_scheduled | temporary failure: ApiError; next attempt 3 in 1378 ms | 14:03:38.824
[  20.9s]    3 | processing |  | 14:03:40.337
[  20.9s]    3 | retry_scheduled | temporary failure: ApiError; next attempt 4 in 2625 ms | 14:03:41.346
[  20.9s]    4 | processing |  | 14:03:44.387
[  20.9s]    4 | retry_scheduled | temporary failure: ApiError; next attempt 5 in 7110 ms | 14:03:45.394
[  20.9s]    5 | processing |  | 14:03:52.956
[  20.9s]    5 | failed | retry_exhausted: temporary failure: ApiError | 14:03:53.967
[  20.9s] $ replay.js --list
[  21.2s]   replay: {"job_id":"fdb3f527-6383-4917-a690-5a9c75bb3c09","attachment_id":"5b6058ab-2875-47a3-9458-0f266742fd97","error_code":"retry_exhausted","attempt":5,"last_error":"temporary failure: ApiError","finished_at":"2026-11-16T14:03:53.967Z"}
[  21.2s] $ docker compose start s3
[  23.3s] $ replay.js 5b6058ab-2875-47a3-9458-0f266742fd97
[  23.7s]   replay: {"id":"5b6058ab-2875-47a3-9458-0f266742fd97","job_id":"fdb3f527-6383-4917-a690-5a9c75bb3c09","result":"replayed"}
[  23.7s] GET attachment -> status=queued error_code=None
[  24.7s] GET attachment -> status=ready error_code=None
[  24.7s] original -> 200 sha256 matches upload: True
[  24.7s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  24.7s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  24.7s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  24.7s] $ replay.js 5b6058ab-2875-47a3-9458-0f266742fd97 (already ready)
[  25.0s]   replay: {"id":"5b6058ab-2875-47a3-9458-0f266742fd97","job_id":"fdb3f527-6383-4917-a690-5a9c75bb3c09","result":"already_ready"}
[  25.2s] job: fdb3f527-6383-4917-a690-5a9c75bb3c09 | ready | 1 | 5 |
[  25.3s] variants rows: 3
[  25.5s] events: enqueued -> processing -> retry_scheduled -> processing -> retry_scheduled -> processing -> retry_scheduled -> processing -> retry_scheduled -> processing -> failed -> replayed -> processing -> ready

## broker
$ python3 scripts/job-scenario.py broker
[   1.8s] consumers: queue_name	ack_required	prefetch_count
attachment.jobs	true	2
[   2.0s] worker limits: cpus=1000000000 memory=805306368
[   2.3s] malformed message published
[  10.1s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	0	0
attachment.jobs.dlq	quorum	true	1	0
[  10.2s]   worker: malformed job message moved to dead letter queue

## Итог по всем заданиям стенда
$ docker compose exec -T postgres psql -U messenger -d messenger -c SELECT status, count(*) FROM attachment_jobs GROUP BY status -c SELECT count(*) AS unpublished_outbox FROM outbox WHERE published_at IS NULL -c SELECT count(*) AS staging_left_on_ready FROM attachments WHERE status = 'ready' AND staging_key IS NOT NULL
 status | count 
--------+-------
 ready  |     9
(1 row)

 unpublished_outbox 
--------------------
                  0
(1 row)

 staging_left_on_ready 
-----------------------
                     0
(1 row)


## Последний проход reconciler
{"mode":"apply","grace_seconds":3600,"staging_expired":0,"scanned":0,"fresh_skipped":0,"referenced":0,"orphans":0,"deleted":0,"failed":0,"orphan_keys":[],"duration_ms":382}
