This directory contains a complete, production-ready Kubernetes deployment example for running Shumai on **Amazon Elastic Kubernetes Service (Amazon EKS)**, using **KEDA (Kubernetes Event-driven Autoscaling)** and **Scale-to-Zero On-Demand AWS Graviton5 instances** for compute-intensive media processing.
To demonstrate a fully self-hosted setup, it uses Garage as an S3-compatible object storage solution. However, if you're deploying on AWS, consider using **Amazon S3** instead of running your own S3-compatible storage server, as it is managed and highly scalable.
- **Zero Idle Cost**: The transcode node group [`eks/eks-cluster.yaml`](./eks-cluster.yaml) is configured with `minSize: 0` and `desiredCapacity: 0`. When no media transcoding tasks are active, **zero transcode EC2 instances exist**, costing **$0/hr**.
- **No Spot Interruption Risk**: Transcoding jobs run on AWS Graviton5 **On-Demand** instances (`c9g.xlarge`, ~$0.174/hr). This eliminates the risk of Spot instances being reclaimed mid-encode by AWS, preventing wasted CPU cycles and avoiding unexpected transcoding delays.
Standard Kubernetes Horizontal Pod Autoscaler (HPA) scales `Deployment` replicas based on metrics like CPU utilization. However, HPAs on Deployments suffer from severe limitations during video processing:
1. When a 1-minute video finishes on Pod A while a 30-minute video is still encoding on Pod B, CPU drops and HPA reduces replica count, which can arbitrarily terminate Pod B mid-encode.
2. StatefulSets scale down in strict ordinal order (`pod-4`, `pod-3`, etc.), which would terminate `pod-4` even if `pod-4` is the only pod currently transcoding!
In [`eks/shumai-transcode.yaml`](./shumai-transcode.yaml), the worker container runs with `SHUMAI_WORKER_IDLE_TIMEOUT_MS: "10000"`:
- **Concurrency Isolation**: Transcode concurrency is set to `1` per pod (`CONCURRENCY_TRANSCODE=1`). Each pod has exclusive access to the node's 4 vCPUs for FFmpeg.
- **Batch Processing Without Cold Starts**: Unlike single-task execution, a running worker continuously pulls tasks from `transcode_queue`. When a user uploads 100 images or multiple video files, a warm worker processes them back-to-back in seconds without restarting the container or control plane churn.
- **Automatic Scale-to-Zero**: When `activeWorkflows === 0` and no new task arrives within 10 consecutive seconds, the worker initiates graceful DBOS shutdown and exits with code `0`.
- The completed Job pod terminates, freeing the node so Cluster Autoscaler can terminate the EC2 instance down to zero.
If a worker node crashes abruptly (e.g. host kernel panic or hardware failure):
- Every active worker updates `WorkflowTask.heartbeat` in PostgreSQL every 5 seconds.
- The single core web server (`shumai-server`) runs a lightweight background sweep every 30 seconds (`workflowService.startStaleTaskRecovery()`).
- Any task stuck in `status = 'processing'` with `heartbeat < now() - 90s` and whose DBOS status is `'PENDING'` is automatically re-enqueued via `client.resumeWorkflow(task.id, { queueName })`.
- **Poison-Pill Protection**: If a malformed media file crashes workers repeatedly, after 3 recovery attempts the task and asset are marked as `failed` to prevent infinite restart loops.
- When Kubernetes drains a node or sends `SIGTERM`, `DBOS.shutdown({ workflowCompletionTimeoutMS: 290_000 })` gives the worker up to 290 seconds to finish its active FFmpeg step and upload proxies to S3.
- Heartbeats remain active throughout the shutdown drain period to prevent the web server from prematurely declaring the worker dead.
- If a slow worker resumes execution after ownership was transferred, DBOS raises `DBOSWorkflowConflictError` (code 5). Shumai catches this error and avoids overwriting the active task or asset status.
*(Note: In production with custom domains, point your DNS to the load balancers and configure `AWS_PUBLIC_ENDPOINT_URL_S3: https://s3.mycompany.com` in [`eks/config.yaml`](./config.yaml).)*
4. Watch Cluster Autoscaler provision dedicated `c9g.xlarge` EC2 instances for the Jobs.
5. As each video finishes transcoding, its pod exits `0` and completes immediately.
6. Once all jobs are done, KEDA holds 0 jobs, and Cluster Autoscaler terminates all transcode EC2 instances, returning the transcode pool to **0 nodes ($0/hr)**!