Files
mtusi2026lab5/compose.yaml

361 lines
11 KiB
YAML

name: messenger-lab5
x-s3-env: &s3-env
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
x-pipeline-env: &pipeline-env
DATABASE_URL_FILE: /run/secrets/database_url
BROKER_URL_FILE: /run/secrets/broker_url
PIPELINE_VERSION: ${PIPELINE_VERSION:-1}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
FAULT_INJECTION: ${FAULT_INJECTION:-0}
services:
postgres:
image: postgres:17.6-alpine3.22
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets: [postgres_password]
volumes:
- pgdata:/var/lib/postgresql/data
networks: [data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 2s
timeout: 3s
retries: 30
deploy:
resources:
limits: {cpus: "1.0", memory: 512M}
stop_grace_period: 30s
restart: unless-stopped
valkey:
image: valkey/valkey:8.1.4-alpine3.22
command: ["valkey-server", "/run/secrets/valkey_conf"]
secrets: [valkey_conf]
networks: [data]
healthcheck:
test: ["CMD-SHELL", "valkey-cli ping 2>&1 | grep -qE 'PONG|NOAUTH'"]
interval: 2s
timeout: 3s
retries: 30
deploy:
resources:
limits: {cpus: "0.25", memory: 96M}
restart: unless-stopped
s3:
image: chrislusf/seaweedfs:4.51
command:
- server
- -dir=/data
- -ip=127.0.0.1
- -ip.bind=127.0.0.1
- -s3
- -s3.ip.bind=0.0.0.0
- -s3.port=9000
- -s3.config=/run/secrets/seaweedfs_s3_config
- -master.volumeSizeLimitMB=1024
- -volume.max=0
- -s3.concurrentUploadLimitMB=128
- -filer.concurrentUploadLimitMB=128
- -volume.concurrentUploadLimitMB=128
environment:
GOMEMLIMIT: 450MiB
secrets: [seaweedfs_s3_config]
volumes:
- s3data:/data
networks: [data]
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9000/healthz && wget -q -O - http://127.0.0.1:9333/dir/status | grep -q '\"DataNodes\":\\[{'"]
interval: 2s
timeout: 3s
start_period: 5s
retries: 30
deploy:
resources:
limits: {cpus: "1.0", memory: 768M}
stop_grace_period: 20s
restart: unless-stopped
# Broker: quorum queues (Raft log on disk) + publisher confirms + manual consumer acks.
rabbitmq:
image: rabbitmq:4.1.4-management-alpine
hostname: rabbitmq # node name rabbit@rabbitmq must be stable for the data volume
secrets:
- source: rabbitmq_conf
target: /etc/rabbitmq/conf.d/20-messenger.conf
volumes:
- rabbitmqdata:/var/lib/rabbitmq
networks: [data, mgmt]
ports:
# management UI only on loopback, for the operator during the defence
- "127.0.0.1:${RABBITMQ_UI_PORT:-15672}:15672"
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "check_port_connectivity"]
interval: 5s
timeout: 10s
start_period: 20s
retries: 30
deploy:
resources:
limits: {cpus: "1.0", memory: 512M}
stop_grace_period: 30s
restart: unless-stopped
s3-init:
build: .
image: mtusi-messenger:lab5
command: ["python", "-m", "app.s3_bootstrap"]
environment:
<<: *s3-env
S3_ADMIN_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access_key_id
S3_ADMIN_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret_access_key
secrets: [s3_admin_access_key_id, s3_admin_secret_access_key]
networks: [data]
depends_on:
s3:
condition: service_healthy
deploy:
resources:
limits: {cpus: "0.25", memory: 128M}
restart: "no"
migrate:
build: .
image: mtusi-messenger:lab5
command: ["python", "-m", "app.migrate"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
secrets: [database_url]
networks: [data]
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits: {cpus: "0.25", memory: 128M}
restart: "no"
api:
build: .
image: mtusi-messenger:lab5
environment:
<<: [*s3-env, *pipeline-env]
APP_HOST: 0.0.0.0
APP_PORT: "8080"
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_STORE: postgres
SESSION_CACHE_MAX_SECONDS: ${SESSION_CACHE_MAX_SECONDS:-300}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
CACHE_URL_FILE: /run/secrets/cache_url
DB_POOL_MAX: ${DB_POOL_MAX:-10}
UPLOAD_CONCURRENCY: ${UPLOAD_CONCURRENCY:-16}
DECODE_CONCURRENCY: ${DECODE_CONCURRENCY:-2}
UPLOAD_SPOOL_BYTES: ${UPLOAD_SPOOL_BYTES:-1048576}
S3_PUT_CONCURRENCY: ${S3_PUT_CONCURRENCY:-4}
# the API never talks to the broker: it writes the outbox; broker_url is not mounted here
secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key]
networks: [edge, data]
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"]
interval: 5s
timeout: 3s
start_period: 5s
retries: 3
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: {cpus: "0.5", memory: 384M}
stop_signal: SIGTERM
stop_grace_period: 10s
restart: unless-stopped
# Outbox -> RabbitMQ (publisher confirms). Separate process: its crash/stop never blocks 202 uploads.
relay:
build: .
image: mtusi-messenger:lab5
command: ["python", "-m", "app.relay"]
environment:
<<: *pipeline-env
RELAY_POLL_SECONDS: ${RELAY_POLL_SECONDS:-1}
secrets: [database_url, broker_url]
networks: [data]
depends_on:
migrate:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import os,sys,time; sys.exit(0 if time.time()-os.path.getmtime('/tmp/relay-heartbeat') < 30 else 1)"]
interval: 10s
timeout: 3s
start_period: 15s
retries: 3
deploy:
resources:
limits: {cpus: "0.25", memory: 128M}
stop_grace_period: 10s
restart: unless-stopped
worker:
build: .
image: mtusi-messenger:lab5
command: ["python", "-m", "app.worker"]
environment:
<<: [*s3-env, *pipeline-env]
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2}
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
LEASE_SECONDS: ${LEASE_SECONDS:-20}
HEARTBEAT_SECONDS: ${HEARTBEAT_SECONDS:-5}
RETRY_BASE_SECONDS: ${RETRY_BASE_SECONDS:-2}
RETRY_MAX_SECONDS: ${RETRY_MAX_SECONDS:-60}
WORKER_MEMORY_LIMIT_MB: ${WORKER_MEMORY_LIMIT_MB:-768}
STUCK_QUEUED_SECONDS: ${STUCK_QUEUED_SECONDS:-300}
S3_READ_TIMEOUT: "15"
secrets: [database_url, broker_url, s3_access_key_id, s3_secret_access_key]
networks: [data]
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
rabbitmq:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import os,sys,time; sys.exit(0 if time.time()-os.path.getmtime('/tmp/worker-heartbeat') < 30 else 1)"]
interval: 10s
timeout: 3s
start_period: 20s
retries: 3
deploy:
replicas: ${WORKER_REPLICAS:-1}
resources:
limits: {cpus: "${WORKER_CPUS:-1.0}", memory: "${WORKER_MEMORY:-1024M}"}
stop_signal: SIGTERM
stop_grace_period: 30s
restart: unless-stopped
# Operator toolbox: `docker compose exec ops python -m app.ops ...` (replay, failed registry, inspect, faults).
ops:
build: .
image: mtusi-messenger:lab5
command: ["python", "-c", "import signal; signal.signal(signal.SIGTERM, lambda *a: exit(0)); signal.pause()"]
healthcheck:
test: ["CMD", "true"]
interval: 30s
start_period: 1s
environment:
<<: [*s3-env, *pipeline-env]
secrets: [database_url, broker_url, s3_access_key_id, s3_secret_access_key]
networks: [data]
depends_on:
migrate:
condition: service_completed_successfully
deploy:
resources:
limits: {cpus: "0.25", memory: 128M}
restart: unless-stopped
sweeper:
build: .
image: mtusi-messenger:lab5
command: ["python", "-m", "app.sweeper"]
healthcheck:
test: ["CMD", "python", "-c", "import os,sys,time; sys.exit(0 if time.time()-os.path.getmtime('/tmp/sweeper-heartbeat') < 3*int(os.environ.get('SWEEP_INTERVAL_SECONDS','60'))+30 else 1)"]
interval: 10s
timeout: 3s
start_period: 10s
retries: 3
environment:
<<: *s3-env
DATABASE_URL_FILE: /run/secrets/database_url
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-900}
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
SWEEP_INTERVAL_SECONDS: ${SWEEP_INTERVAL_SECONDS:-60}
secrets: [database_url, s3_access_key_id, s3_secret_access_key]
networks: [data]
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
deploy:
resources:
limits: {cpus: "0.25", memory: 128M}
restart: unless-stopped
lb:
image: haproxy:3.2.4-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:${APP_PORT:-8080}:8080"
networks: [edge]
depends_on:
api:
condition: service_started
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8405/lb-health"]
interval: 5s
timeout: 2s
retries: 3
deploy:
resources:
limits: {cpus: "0.5", memory: 128M}
restart: unless-stopped
networks:
edge: {}
mgmt: {} # only for the loopback-published RabbitMQ management UI
data:
internal: true
volumes:
pgdata:
s3data:
rabbitmqdata:
secrets:
postgres_password:
file: ./secrets/postgres_password
database_url:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
valkey_conf:
file: ./secrets/valkey.conf
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
s3_admin_access_key_id:
file: ./secrets/s3_admin_access_key_id
s3_admin_secret_access_key:
file: ./secrets/s3_admin_secret_access_key
seaweedfs_s3_config:
file: ./secrets/seaweedfs_s3.json
rabbitmq_conf:
file: ./secrets/rabbitmq.conf
broker_url:
file: ./secrets/broker_url