forked from mtusi2026/mtusi2026lab5
feat(attachments): add durable Kafka image processing
This commit is contained in:
+270
@@ -0,0 +1,270 @@
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
deploy:
|
||||
replicas: 2
|
||||
command: ['sh', '-c', 'INSTANCE_ID=$$(hostname) exec node dist/main.js']
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
UPLOAD_MEMORY_PROFILE: ${UPLOAD_MEMORY_PROFILE:-0}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: ${DB_POOL_MAX:-15}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:${S3_PUBLIC_PORT:-9000}}
|
||||
DIRECT_UPLOAD_TTL_SECONDS: ${DIRECT_UPLOAD_TTL_SECONDS:-60}
|
||||
S3_ACCESS_KEY_ID: ${S3_API_ACCESS_KEY_ID:-LABAPI}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_API_SECRET_ACCESS_KEY:-local_api_storage_password}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
UPLOAD_BODY_TIMEOUT_MS: ${UPLOAD_BODY_TIMEOUT_MS:-15000}
|
||||
KAFKA_BROKERS: kafka:9092
|
||||
ATTACHMENT_QUEUE_CAPACITY: ${ATTACHMENT_QUEUE_CAPACITY:-64}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
kafka-init:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- curl
|
||||
- --fail
|
||||
- --silent
|
||||
- --show-error
|
||||
- --max-time
|
||||
- '2'
|
||||
- --output
|
||||
- /dev/null
|
||||
- http://127.0.0.1:8080/health/ready
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
start_period: 20s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
worker:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
command: ['node', 'dist/attachment-worker.main.js']
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
mem_limit: ${WORKER_MEMORY_LIMIT:-768m}
|
||||
memswap_limit: ${WORKER_MEMORY_LIMIT:-768m}
|
||||
tmpfs:
|
||||
- /tmp:size=32m,mode=1777
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: 2
|
||||
KAFKA_BROKERS: kafka:9092
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1}
|
||||
WORKER_PREFETCH_BYTES: ${WORKER_PREFETCH_BYTES:-1048576}
|
||||
ATTACHMENT_QUEUE_CAPACITY: ${ATTACHMENT_QUEUE_CAPACITY:-64}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID: ${S3_API_ACCESS_KEY_ID:-LABAPI}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_API_SECRET_ACCESS_KEY:-local_api_storage_password}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
kafka-init:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
disable: true
|
||||
stop_grace_period: 120s
|
||||
|
||||
kafka:
|
||||
image: apache/kafka:3.9.2
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
CLUSTER_ID: MkU3OEVBNTcwNTJENDM2Qk
|
||||
KAFKA_NODE_ID: 1
|
||||
KAFKA_PROCESS_ROLES: broker,controller
|
||||
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
|
||||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
|
||||
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@127.0.0.1:9093
|
||||
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
|
||||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
|
||||
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
|
||||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
||||
KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS: 1
|
||||
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
||||
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
|
||||
KAFKA_LOG_DIRS: /var/lib/kafka/data
|
||||
KAFKA_HEAP_OPTS: '-Xms128m -Xmx512m'
|
||||
volumes:
|
||||
- kafka_data:/var/lib/kafka/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'bash', '-c', 'true </dev/tcp/127.0.0.1/9092']
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
start_period: 90s
|
||||
retries: 12
|
||||
stop_grace_period: 30s
|
||||
|
||||
kafka-init:
|
||||
image: apache/kafka:3.9.2
|
||||
depends_on:
|
||||
kafka:
|
||||
condition: service_healthy
|
||||
entrypoint: ['/opt/kafka/bin/kafka-topics.sh']
|
||||
command: ['--bootstrap-server', 'kafka:9092', '--create', '--if-not-exists', '--topic', 'attachments.process.v1', '--partitions', '${ATTACHMENT_TOPIC_PARTITIONS:-1}', '--replication-factor', '1', '--config', 'retention.ms=-1', '--config', 'flush.messages=1']
|
||||
|
||||
reconciler:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
command: ['node', 'dist/Attachment/reconcile.cli.js', '--watch', '--apply']
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: 2
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID: ${S3_RECONCILER_ACCESS_KEY_ID:-LABRECONCILER}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_RECONCILER_SECRET_ACCESS_KEY:-local_reconciler_storage_password}
|
||||
ORPHAN_GRACE_SECONDS: ${ORPHAN_GRACE_SECONDS:-600}
|
||||
RECONCILE_INTERVAL_SECONDS: ${RECONCILE_INTERVAL_SECONDS:-60}
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ['CMD', 'node', 'dist/Attachment/reconcile-health.js']
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
start_period: 30s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
nginx:
|
||||
image: nginx:1.30.6-alpine
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- '127.0.0.1:${APP_PORT:-8080}:80'
|
||||
- '127.0.0.1:${S3_PUBLIC_PORT:-9000}:9000'
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/start-nginx.sh:/etc/nginx/start-nginx.sh:ro
|
||||
entrypoint: ['/bin/sh', '/etc/nginx/start-nginx.sh']
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ['CMD', 'wget', '-q', '-T', '2', '-t', '1', '-O', '/dev/null', 'http://127.0.0.1/health/ready']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 5
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
target: migrate
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
redis:
|
||||
image: redis:7.4.5-alpine
|
||||
restart: unless-stopped
|
||||
command:
|
||||
['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec']
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'redis-cli', 'ping']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
s3:
|
||||
image: rustfs/rustfs:1.0.4
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
RUSTFS_ACCESS_KEY: ${S3_ROOT_ACCESS_KEY_ID:-${S3_ACCESS_KEY_ID:-LABSTORAGE}}
|
||||
RUSTFS_SECRET_KEY: ${S3_ROOT_SECRET_ACCESS_KEY:-${S3_SECRET_ACCESS_KEY:-local_app_storage_password}}
|
||||
RUSTFS_VOLUMES: /data
|
||||
RUSTFS_ADDRESS: 0.0.0.0:9000
|
||||
RUSTFS_CONSOLE_ENABLE: 'false'
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'curl', '--fail', '--silent', 'http://127.0.0.1:9000/health']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 30s
|
||||
|
||||
s3-init:
|
||||
build:
|
||||
context: .
|
||||
target: storage-init
|
||||
environment:
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ACCESS_KEY_ID: ${S3_ROOT_ACCESS_KEY_ID:-${S3_ACCESS_KEY_ID:-LABSTORAGE}}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_ROOT_SECRET_ACCESS_KEY:-${S3_SECRET_ACCESS_KEY:-local_app_storage_password}}
|
||||
S3_API_ACCESS_KEY_ID: ${S3_API_ACCESS_KEY_ID:-LABAPI}
|
||||
S3_API_SECRET_ACCESS_KEY: ${S3_API_SECRET_ACCESS_KEY:-local_api_storage_password}
|
||||
S3_RECONCILER_ACCESS_KEY_ID: ${S3_RECONCILER_ACCESS_KEY_ID:-LABRECONCILER}
|
||||
S3_RECONCILER_SECRET_ACCESS_KEY: ${S3_RECONCILER_SECRET_ACCESS_KEY:-local_reconciler_storage_password}
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
kafka_data:
|
||||
Reference in New Issue
Block a user