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

## restart
$ python3 scripts/job-scenario.py restart
[   0.2s] user 83a421c6-238a-430e-b23d-7065f92692ea chat 37e2da02-e39f-4a5b-bc3e-99e3072079ad
[   0.2s] $ docker compose stop worker
[   1.2s] POST attachment -> 202 status=queued id=b5032f63-71ca-4684-8071-8e546a2b1d38 instance=64377e499417
[   1.2s] content while queued -> 409
[   1.2s] POST message with 1 attachment(s) -> 201 id=3133055e-c217-4956-8b29-6c195ad7d832
[   1.2s] POST message with 0 attachment(s) -> 201 id=485a4b3d-e797-4cd8-8c3a-b6fbd22e51fa
[  10.3s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[  10.3s] $ docker compose restart api rabbitmq
[  27.8s] queues after broker restart: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	1	0
attachment.jobs.dlq	quorum	true	0	0
[  27.8s] status after restarts: queued
[  27.8s] $ docker compose start worker
[  32.3s] GET attachment -> status=queued error_code=None
[  33.3s] GET attachment -> status=ready error_code=None
[  33.3s] original -> 200 sha256 matches upload: True
[  33.4s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  33.4s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  33.4s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  33.6s] job: 4e569601-78cf-43c7-8e00-d11cb4929ef8 | ready | 1 | 5 |
[  33.7s] variants rows: 3
[  33.9s] events: enqueued -> processing -> ready

## crash-after-store
$ python3 scripts/job-scenario.py crash-after-store
[   0.1s] user 00aee8d4-3de1-4190-ba59-145bbd9ab2e3 chat eb05fa1f-3a6f-412b-a924-23472af26d2b
[   0.1s] $ docker compose stop worker
[   0.7s] $ docker compose run --no-deps -e WORKER_FAULT=exit-after-store -e JOB_LEASE_SECONDS=15 worker
[   2.7s] POST attachment -> 202 status=queued id=6c09adc1-6c17-43ad-a8ed-2fa6582fe5c5 instance=25ff01cef182
[   3.4s]   one-off: {"timestamp":"2026-11-20T14:34:22.942Z","severity":"WARN","service.name":"messenger-worker","event":"worker","logger":"Worker","message":"fault injection: worker killed after storing objects, before commit and ack","trace_id":"0f573fa6f35061a106d47bf5eb093c3d","span_id":"39a42f14e6f1823b"}
[   3.6s] one-off process exit code 137 (137 = killed without ack)
[   3.6s] status after crash: processing
[   3.9s] lease: processing | 1 | t
[  11.7s] 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.7s] $ docker compose start worker
[  16.8s] GET attachment -> status=processing error_code=None
[  19.8s] GET attachment -> status=queued error_code=None
[  20.8s] GET attachment -> status=processing error_code=None
[  21.9s] GET attachment -> status=ready error_code=None
[  21.9s] original -> 200 sha256 matches upload: True
[  21.9s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  21.9s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  21.9s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  22.0s] job: d619929c-d660-4311-bb5e-0d3fbc1813ef | ready | 2 | 5 |
[  22.2s] variants rows: 3
[  22.3s] events: enqueued -> processing -> lease_expired -> processing -> ready
[  22.6s]   worker: {"timestamp":"2026-11-20T14:34:37.365Z","severity":"INFO","service.name":"messenger-worker","event":"job_processor","logger":"JobProcessor","message":"job d619929c-d660-4311-bb5e-0d3fbc1813ef: processing, not due or leased elsewhere, skipped","trace_id":"0f573fa6f35061a106d47bf5eb093c3d","span_id":"17a7f5d766544282"}
[  22.6s]   worker: {"timestamp":"2026-11-20T14:34:37.366Z","severity":"INFO","service.name":"messenger-worker","event":"job.handled","logger":"Worker","job_id":"d619929c-d660-4311-bb5e-0d3fbc1813ef","attachment_id":"6c09adc1-6c17-43ad-a8ed-2fa6582fe5c5","attempt":1,"redelivered":true,"outcome":"skipped","duration_ms":25,"trace_id":"0f573fa6f35061a106d47bf5eb093c3d","span_id":"17a7f5d766544282"}
[  22.6s]   worker: {"timestamp":"2026-11-20T14:34:40.735Z","severity":"INFO","service.name":"messenger-worker","event":"job.handled","logger":"Worker","job_id":"d619929c-d660-4311-bb5e-0d3fbc1813ef","attachment_id":"6c09adc1-6c17-43ad-a8ed-2fa6582fe5c5","attempt":2,"redelivered":false,"outcome":"success","duration_ms":159}
[  22.7s]   relay: {"timestamp":"2026-11-20T14:34:39.557Z","severity":"INFO","service.name":"messenger-relay","event":"outbox_relay","logger":"OutboxRelay","message":"lease expired: 1, stale queued republished: 0"}

## crash-after-commit
$ python3 scripts/job-scenario.py crash-after-commit
[   0.1s] user 34fae015-fbb7-48eb-ad9d-8f23ee67784c chat 801de767-107d-4eb7-92ff-b6aa38dc95cf
[   0.1s] $ docker compose stop worker
[   0.6s] $ docker compose run --no-deps -e WORKER_FAULT=exit-after-commit worker
[   2.7s] POST attachment -> 202 status=queued id=e02d5568-ce5b-485d-9649-f9b4d7387d41 instance=25ff01cef182
[   3.4s]   one-off: {"timestamp":"2026-11-20T14:34:45.757Z","severity":"WARN","service.name":"messenger-worker","event":"worker","logger":"Worker","message":"fault injection: worker killed after commit, before ack","trace_id":"e15e253daf15a01b76410070a68e7aff","span_id":"b5b5eb6d044c2f38"}
[   3.5s] one-off process exit code 137 (137 = killed without ack)
[   3.9s] status after crash: ready staging_key: staging/e02d5568-ce5b-485d-9649-f9b4d7387d41
[  11.7s] 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.7s] $ docker compose start worker
[  17.6s] staging_key after redelivery: NULL
[  17.6s] original -> 200 sha256 matches upload: True
[  17.6s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  17.6s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  17.6s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  17.8s] job: dbc6450f-1513-4fd0-9923-3c3339985866 | ready | 1 | 5 |
[  18.0s] variants rows: 3
[  18.2s] events: enqueued -> processing -> ready
[  18.5s]   worker: {"timestamp":"2026-11-20T14:34:59.494Z","severity":"INFO","service.name":"messenger-worker","event":"job_processor","logger":"JobProcessor","message":"job dbc6450f-1513-4fd0-9923-3c3339985866: ready, not due or leased elsewhere, skipped","trace_id":"e15e253daf15a01b76410070a68e7aff","span_id":"db61235444dfbbbc"}
[  18.5s]   worker: {"timestamp":"2026-11-20T14:34:59.494Z","severity":"INFO","service.name":"messenger-worker","event":"job.handled","logger":"Worker","job_id":"dbc6450f-1513-4fd0-9923-3c3339985866","attachment_id":"e02d5568-ce5b-485d-9649-f9b4d7387d41","attempt":1,"redelivered":true,"outcome":"skipped","duration_ms":50,"trace_id":"e15e253daf15a01b76410070a68e7aff","span_id":"db61235444dfbbbc"}

## outbox-gap
$ python3 scripts/job-scenario.py outbox-gap
[   0.1s] user 8646554f-5ce9-4e60-951f-9403d8e951d1 chat 196660eb-f3fc-4117-96dd-9287c6c85681
[   0.1s] $ docker compose stop relay
[   0.9s] POST attachment -> 202 status=queued id=14e198ac-f973-4692-9267-8dba3a1acbc0 instance=64377e499417
[   1.1s] outbox after COMMIT: 8 | NOT PUBLISHED | 0
[   8.8s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	0	0
attachment.jobs.dlq	quorum	true	0	0
[   8.8s] $ docker compose kill api
[   9.4s] API killed (SIGKILL) after COMMIT, nothing was published
[   9.4s] $ docker compose start api
[  18.6s] status after API restart: queued
[  18.6s] $ docker compose start relay
[  22.9s] GET attachment -> status=queued error_code=None
[  26.0s] GET attachment -> status=ready error_code=None
[  26.2s] outbox after relay recovery: 8 | 2026-11-20 14:35:26.35+00 | 1
[  26.2s] original -> 200 sha256 matches upload: True
[  26.2s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  26.2s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  26.3s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  26.4s] job: 29554704-45b1-4335-87fe-8bcb0621b000 | ready | 1 | 5 |
[  26.6s] variants rows: 3
[  26.7s] events: enqueued -> processing -> ready

## relay-crash
$ python3 scripts/job-scenario.py relay-crash
[   0.1s] user da9df65a-2adb-4552-b09a-063e143f7e42 chat 59b25b73-439e-4eb9-abf5-51a8851b5dd5
[   0.1s] $ docker compose stop relay
[   0.7s] $ docker compose run --no-deps -e RELAY_FAULT=exit-after-confirm relay
[   3.7s] POST attachment -> 202 status=queued id=95f87871-7463-4cec-b73e-556b32ce1f57 instance=64377e499417
[   4.3s]   one-off: {"timestamp":"2026-11-20T14:35:31.985Z","severity":"INFO","service.name":"messenger-relay","event":"outbox_relay","logger":"OutboxRelay","message":"published 1 job message(s), broker confirmed"}
[   4.3s]   one-off: {"timestamp":"2026-11-20T14:35:31.985Z","severity":"WARN","service.name":"messenger-relay","event":"outbox_relay","logger":"OutboxRelay","message":"fault injection: exit after broker confirm, before outbox is marked"}
[   4.4s] one-off process exit code 137 (137 = killed without ack)
[   4.8s] outbox after relay crash: 9 | NOT MARKED | 0
[   4.8s] $ docker compose start relay
[   9.6s] GET attachment -> status=ready error_code=None
[  12.8s] outbox after relay restart: 9 | 2026-11-20 14:35:39.707+00 | 1
[  12.8s] original -> 200 sha256 matches upload: True
[  12.8s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  12.8s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  12.8s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  13.0s] job: 5449c7df-1ee6-435d-a8e4-60b0f672d65e | ready | 1 | 5 |
[  13.1s] variants rows: 3
[  13.4s] events: enqueued -> processing -> ready
[  13.6s]   worker: {"timestamp":"2026-11-20T14:35:32.064Z","severity":"INFO","service.name":"messenger-worker","event":"job.handled","logger":"Worker","job_id":"5449c7df-1ee6-435d-a8e4-60b0f672d65e","attachment_id":"95f87871-7463-4cec-b73e-556b32ce1f57","attempt":1,"redelivered":false,"outcome":"success","duration_ms":79,"trace_id":"c24603daec760a57d467aff4b2a1d515","span_id":"99e7c21fe5d815b2"}
[  13.6s]   worker: {"timestamp":"2026-11-20T14:35:39.712Z","severity":"INFO","service.name":"messenger-worker","event":"job_processor","logger":"JobProcessor","message":"job 5449c7df-1ee6-435d-a8e4-60b0f672d65e: ready, not due or leased elsewhere, skipped","trace_id":"c24603daec760a57d467aff4b2a1d515","span_id":"ee8d2b5d22aac1b4"}
[  13.6s]   worker: {"timestamp":"2026-11-20T14:35:39.713Z","severity":"INFO","service.name":"messenger-worker","event":"job.handled","logger":"Worker","job_id":"5449c7df-1ee6-435d-a8e4-60b0f672d65e","attachment_id":"95f87871-7463-4cec-b73e-556b32ce1f57","attempt":1,"redelivered":false,"outcome":"skipped","duration_ms":6,"trace_id":"c24603daec760a57d467aff4b2a1d515","span_id":"ee8d2b5d22aac1b4"}

## retry-replay
$ python3 scripts/job-scenario.py retry-replay
[   0.1s] user beb0f294-3e65-411c-bda5-ce3f29e2c1d7 chat a253ff82-2c8c-457b-85cf-6b8c9beee260
[   0.1s] $ docker compose stop worker
[   0.7s] POST attachment -> 202 status=queued id=9832fbf4-2f69-484e-87e4-463d3e8ef3a7 instance=64377e499417
[   0.7s] $ docker compose stop s3
[   1.2s] $ docker start worker (without dependencies)
[   1.7s] GET attachment -> status=queued error_code=None
[   2.7s] GET attachment -> status=processing error_code=None
[   3.7s] GET attachment -> status=queued error_code=None
[   4.8s] GET attachment -> status=processing error_code=None
[   5.8s] GET attachment -> status=queued error_code=None
[   7.8s] GET attachment -> status=processing error_code=None
[   8.8s] GET attachment -> status=queued error_code=None
[  12.8s] GET attachment -> status=processing error_code=None
[  13.8s] GET attachment -> status=queued error_code=None
[  19.9s] GET attachment -> status=processing error_code=None
[  20.9s] GET attachment -> status=failed error_code=retry_exhausted
[  20.9s] POST message with 1 attachment(s) -> 409 id=None
[  20.9s] failed attachment in a new message -> 409
[  20.9s] retries:
[  21.0s]    1 | enqueued |  | 14:35:42.501
[  21.0s]    1 | processing |  | 14:35:44.160
[  21.0s]    1 | retry_scheduled | temporary failure: ApiError; next attempt 2 in 860 ms | 14:35:45.201
[  21.0s]    2 | processing |  | 14:35:46.335
[  21.0s]    2 | retry_scheduled | temporary failure: ApiError; next attempt 3 in 1537 ms | 14:35:47.349
[  21.0s]    3 | processing |  | 14:35:49.390
[  21.0s]    3 | retry_scheduled | temporary failure: ApiError; next attempt 4 in 3959 ms | 14:35:50.403
[  21.0s]    4 | processing |  | 14:35:54.463
[  21.0s]    4 | retry_scheduled | temporary failure: ApiError; next attempt 5 in 5443 ms | 14:35:55.491
[  21.0s]    5 | processing |  | 14:36:01.035
[  21.0s]    5 | failed | retry_exhausted: temporary failure: ApiError | 14:36:02.047
[  21.0s] $ replay.js --list
[  21.4s]   replay: {"job_id":"b88d0626-5470-4148-83eb-cac17feb8b4c","attachment_id":"9832fbf4-2f69-484e-87e4-463d3e8ef3a7","error_code":"retry_exhausted","attempt":5,"last_error":"temporary failure: ApiError","finished_at":"2026-11-20T14:36:02.047Z"}
[  21.4s] $ docker compose start s3
[  23.7s] $ replay.js 9832fbf4-2f69-484e-87e4-463d3e8ef3a7
[  24.0s]   replay: {"id":"9832fbf4-2f69-484e-87e4-463d3e8ef3a7","job_id":"b88d0626-5470-4148-83eb-cac17feb8b4c","result":"replayed"}
[  24.0s] GET attachment -> status=queued error_code=None
[  25.1s] GET attachment -> status=ready error_code=None
[  25.1s] original -> 200 sha256 matches upload: True
[  25.1s] thumbnail -> 200 image/jpeg 256x160 sha256 matches metadata: True
[  25.1s] crop      -> 200 image/jpeg 128x128 sha256 matches metadata: True
[  25.1s] optimized -> 200 image/webp 320x200 sha256 matches metadata: True
[  25.1s] $ replay.js 9832fbf4-2f69-484e-87e4-463d3e8ef3a7 (already ready)
[  25.4s]   replay: {"id":"9832fbf4-2f69-484e-87e4-463d3e8ef3a7","job_id":"b88d0626-5470-4148-83eb-cac17feb8b4c","result":"already_ready"}
[  25.6s] job: b88d0626-5470-4148-83eb-cac17feb8b4c | ready | 1 | 5 |
[  25.7s] variants rows: 3
[  25.9s] 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
[   2.1s] consumers: queue_name	ack_required	prefetch_count
attachment.jobs	true	2
[   2.2s] worker limits: cpus=1000000000 memory=805306368
[   2.5s] malformed message published
[  10.3s] queues: name	type	durable	messages_ready	messages_unacknowledged
attachment.jobs	quorum	true	0	0
attachment.jobs.dlq	quorum	true	1	0
[  10.4s]   worker: {"timestamp":"2026-11-20T14:36:10.245Z","severity":"WARN","service.name":"messenger-worker","event":"worker","logger":"Worker","message":"malformed job message moved to dead letter queue","trace_id":"733415ad4bdbd66df930af833ef44f18","span_id":"abc299a80e5c1e42"}

## Итог по всем заданиям стенда
$ 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":351}
