mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 16:56:57 +00:00
214 lines
9.4 KiB
Plaintext
214 lines
9.4 KiB
Plaintext
---
|
|
title: Deploy with Distributed Workers
|
|
description: Configure a high-scale production deployment using dedicated background workers, DBOS, and S3-compatible storage.
|
|
---
|
|
|
|
For production environments or workloads with high-volume video processing and heavy AI agent usage, running background tasks on a single server can cause bottlenecks. Shumai resolves this by decoupling the web dashboard from background processing through dedicated worker nodes orchestrated by **DBOS Transact**.
|
|
|
|
All background tasks (media transcoding, AI embeddings, agent tasks) are dispatched to durable task queues (`transcode_queue` and `agent_queue`) hosted directly in PostgreSQL. No separate external cluster (like Temporal) is required.
|
|
|
|
---
|
|
|
|
## How it Works
|
|
|
|
In a distributed deployment, the core Shumai web application handles user requests, database updates, and file uploads, while delegating durable background workflows to specialized worker instances via PostgreSQL task queues.
|
|
|
|
```mermaid
|
|
graph TD
|
|
UserUpload[User Uploads Video / Triggers Agent] --> WebApp[Shumai Web UI / API]
|
|
WebApp -->|Enqueue Task| DB[(PostgreSQL / DBOS Queues)]
|
|
DB -->|Queue: transcode_queue| TranscodeWorker[Transcode Worker Machine]
|
|
DB -->|Queue: agent_queue| AgentWorker[Agent Worker Machine]
|
|
```
|
|
|
|
- **Transcoding Workers**: Can be run on GPU-accelerated or high-CPU compute instances.
|
|
- **AI Agent Workers**: Can be run on isolated, sandboxed hosts to safely execute automation scripts.
|
|
|
|
> [!IMPORTANT]
|
|
> **Storage Requirement**: Because the core application and the background workers run on separate machines in a distributed setup, **you must configure an S3-compatible storage backend** (such as AWS S3, Cloudflare R2, or MinIO). Local storage (`STORAGE_BACKEND=local`) is not supported in this mode because the workers and the web application do not share a common filesystem.
|
|
|
|
---
|
|
|
|
## Step 1: Deploy the Core Web Application
|
|
|
|
On your primary host machine, run PostgreSQL and the main Shumai web application.
|
|
|
|
To instruct the main web application to handle only API and UI traffic without running background worker queues locally, set `SHUMAI_WORKERS=none`:
|
|
|
|
```env
|
|
DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public
|
|
STORAGE_BACKEND=s3
|
|
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
|
|
S3_BUCKET=shumai
|
|
S3_REGION=us-east-1
|
|
S3_ACCESS_KEY_ID=your-access-key-id
|
|
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
|
SHUMAI_WORKERS=none
|
|
BETTER_AUTH_SECRET=your-random-32-character-secret-key
|
|
```
|
|
|
|
When started with `SHUMAI_WORKERS=none` (or `--workers=none`), the web server starts immediately without polling or consuming tasks from the transcode or agent queues.
|
|
|
|
---
|
|
|
|
## Step 2: Configure & Run Dedicated Workers
|
|
|
|
On your specialized worker machines, configure each worker with a `.env` file referencing the shared PostgreSQL database and S3 storage:
|
|
|
|
```env
|
|
DATABASE_URL=postgresql://shumai:shumai_password@<primary-host-ip>:5432/shumai_db?schema=public
|
|
STORAGE_BACKEND=s3
|
|
AWS_ENDPOINT_URL_S3=https://your-s3-endpoint.com
|
|
S3_BUCKET=shumai
|
|
S3_REGION=us-east-1
|
|
S3_ACCESS_KEY_ID=your-access-key-id
|
|
S3_SECRET_ACCESS_KEY=your-secret-access-key
|
|
CONCURRENCY_TRANSCODE=1
|
|
CONCURRENCY_AGENT=5
|
|
```
|
|
|
|
### Media Transcoding Worker
|
|
|
|
Run this on your media processing nodes (requires system installations of `ffmpeg` and `ffprobe`):
|
|
|
|
```bash
|
|
npm install -g @shumai-one/shumai-transcode
|
|
shumai-transcode
|
|
```
|
|
|
|
Alternatively, you can run the transcode worker using the dedicated lightweight Docker image:
|
|
|
|
```bash
|
|
docker run -d \
|
|
--name shumai-transcode-worker \
|
|
--env-file .env \
|
|
shumaione/shumai-transcode:latest
|
|
```
|
|
|
|
(You can also use the all-in-one image: `docker run -d --name shumai-transcode-worker --env-file .env shumaione/shumai:latest shumai-transcode`.)
|
|
|
|
### AI Agent Worker
|
|
|
|
Run this on your sandboxed agent execution nodes (requires sandbox dependencies like `bubblewrap` or `sandbox-exec`):
|
|
|
|
```bash
|
|
npm install -g @shumai-one/shumai-agent
|
|
shumai-agent
|
|
```
|
|
|
|
Alternatively, you can run the agent worker using the official Docker image:
|
|
|
|
```bash
|
|
docker run -d \
|
|
--name shumai-agent-worker \
|
|
--env-file .env \
|
|
--cap-add=SYS_ADMIN \
|
|
shumaione/shumai:latest shumai-agent
|
|
```
|
|
|
|
> [!IMPORTANT]
|
|
> **For Bun Users:** Since the worker package binaries contain a `#!/usr/bin/env node` shebang, running `shumai-transcode` or `shumai-agent` directly will run under Node.js. If you wish to run them using Bun, prefix the commands with `bun run --bun` (e.g., `bun run --bun shumai-transcode` or `bun run --bun shumai-agent`).
|
|
|
|
---
|
|
|
|
## Worker Concurrency & Multi-Instance Deployments
|
|
|
|
### Concurrency Tuning
|
|
|
|
Workers pull tasks from PostgreSQL queues based on configurable concurrency limits:
|
|
|
|
- `CONCURRENCY_TRANSCODE`: Maximum concurrent video transcoding jobs per worker (defaults to `1`).
|
|
- `CONCURRENCY_AGENT`: Maximum concurrent agent and embedding workflows per worker (defaults to `5`, recommend setting to `1` when paired with a local `embedding-server`).
|
|
|
|
### Replica Identity
|
|
|
|
When running multiple replicas of the transcode or agent worker across multiple machines or pods:
|
|
|
|
- `DbosExecutor` automatically derives a unique per-instance ID from the machine hostname and process ID to ensure startup recovery only re-enqueues tasks belonging to that specific instance.
|
|
- **Custom Worker ID**: Set `SHUMAI_WORKERS_ID` (or `SHUMAI_WORKER_ID`) if you wish to assign an explicit identifier.
|
|
- **Kubernetes**: In Kubernetes Pods, set `POD_NAME` (via Downward API `fieldRef: { fieldPath: metadata.name }`) to provide stable, unique per-pod identity across pod restarts.
|
|
|
|
---
|
|
|
|
## Kubernetes Deployment & Autoscaling (HPA)
|
|
|
|
Shumai provides an out-of-the-box Kubernetes configuration under `k8s/` showcasing horizontal scalability for media processing. Because DBOS manages task queues directly within PostgreSQL, transcode workers are completely stateless and can scale dynamically with workload demand.
|
|
|
|
### Architecture
|
|
|
|
```mermaid
|
|
graph TD
|
|
User([User / Web Browser]) -->|Port 3000| Server[Pod: shumai-server<br/>SHUMAI_WORKERS=agent]
|
|
Server -->|Enqueue to transcode_queue| DB[(PostgreSQL: pgvector18<br/>Service: shumai-postgres)]
|
|
Server -->|Direct Uploads / Stream Proxies| Storage[(Garage S3<br/>Service: garage:3900)]
|
|
|
|
DB -->|Poll transcode_queue| Worker1[Pod: shumai-transcode-1]
|
|
DB -->|Poll transcode_queue| WorkerN[Pod: shumai-transcode-N<br/>Autoscaled Replicas: 1..5]
|
|
Worker1 -->|Fetch Source / Store Proxies| Storage
|
|
WorkerN -->|Fetch Source / Store Proxies| Storage
|
|
|
|
HPA[HorizontalPodAutoscaler<br/>Target: 70% CPU<br/>min: 1, max: 5] -.->|Autoscale| WorkerN
|
|
```
|
|
|
|
### Components
|
|
|
|
The `k8s/` directory includes a self-contained Kustomize stack:
|
|
|
|
1. **`shumai-server`**: Main dashboard and REST API handling web traffic and AI agent tasks (`SHUMAI_WORKERS=agent`).
|
|
2. **`shumai-transcode`**: Dedicated transcoding worker pool running `shumaione/shumai-transcode:latest` with `CONCURRENCY_TRANSCODE=1`. Each pod receives its unique identity via the Kubernetes Downward API (`POD_NAME`).
|
|
3. **`garage`**: Lightweight in-cluster S3-compatible storage powered by `dxflrs/garage`, automatically initialized with bucket `shumai` and pre-configured credentials.
|
|
4. **`shumai-postgres`**: PostgreSQL database with `pgvector:pg18` hosting application data and DBOS workflow queues.
|
|
5. **`shumai-transcode-hpa`**: `HorizontalPodAutoscaler` (`autoscaling/v2`) targeting 70% CPU utilization, scaling between **1 and 5 replicas**.
|
|
|
|
### Deploying the Stack
|
|
|
|
Deploy the entire stack with a single command:
|
|
|
|
```bash
|
|
kubectl apply -k k8s/
|
|
```
|
|
|
|
Check the status of all pods:
|
|
|
|
```bash
|
|
kubectl get pods -n shumai
|
|
```
|
|
|
|
Inspect the HorizontalPodAutoscaler:
|
|
|
|
```bash
|
|
kubectl get hpa -n shumai
|
|
```
|
|
|
|
```text
|
|
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
|
|
shumai-transcode-hpa Deployment/shumai-transcode cpu: 10%/70% 1 5 1
|
|
```
|
|
|
|
### Accessing the Dashboard & Storage
|
|
|
|
In Kubernetes, services are created as internal `ClusterIP` services by default. To access the stack from your local development machine:
|
|
|
|
1. **Forward the Web Server**:
|
|
|
|
```bash
|
|
kubectl port-forward -n shumai svc/shumai-server 3000:3000
|
|
```
|
|
|
|
Open **`http://localhost:3000`** in your browser.
|
|
|
|
2. **Forward In-Cluster S3 Storage (Garage)**:
|
|
Shumai uses direct client-to-S3 presigned URLs for uploading and streaming media. `shumai-server` is configured with `AWS_PUBLIC_ENDPOINT_URL_S3: http://localhost:3900`. Forward port 3900 to allow your local browser to upload directly to Garage S3:
|
|
```bash
|
|
kubectl port-forward -n shumai svc/garage 3900:3900
|
|
```
|
|
|
|
> [!NOTE]
|
|
> In production environments, configure an Ingress controller (e.g. `ingress-nginx` or AWS ALB) with a public domain or set `AWS_ENDPOINT_URL_S3` to an external cloud S3/R2 bucket.
|
|
|
|
### How Autoscaling Operates
|
|
|
|
1. **Scale-Up**: When users upload video assets, the `shumai-server` creates transcode workflow tasks and submits them to `transcode_queue` in PostgreSQL. As the active transcode worker utilizes CPU for FFmpeg encoding, CPU usage crosses the 70% threshold, triggering the HPA to scale up additional `shumai-transcode` pods (up to 5 pods).
|
|
2. **Per-Pod Executor Identity**: Each newly spawned pod uses `POD_NAME` as its DBOS executor identity. Pods poll `transcode_queue` in parallel, safely picking up and executing transcode jobs concurrently.
|
|
3. **Graceful Scale-Down & Drainage**: Worker pods configure `terminationGracePeriodSeconds: 300` and handle `SIGTERM` gracefully by draining active DBOS steps before termination, ensuring zero video proxy corruption during scale-down.
|