# Compute Worker (/deploy/compute-worker)

The compute worker generates speech, analyzes PDFs, makes previews and exports, and serves audio to browsers. Docker, source installs and the desktop app run it for you.

Run it separately on [Vercel](/deploy/vercel-deployment), where it cannot run inside the app, or to use a dedicated host. Deploy the same version as the app:

```text
ghcr.io/richardr1126/openreader-compute-worker:latest
```

## What it needs [#what-it-needs]

| Requirement | Details |
| --- | --- |
| **NATS JetStream** | The job queue, with file storage. Only the worker connects to it. |
| **S3-compatible storage** | The same bucket as the app. The embedded SeaweedFS cannot be shared. |
| **HTTPS to the app** | For provider keys, limits and account email settings. Plain HTTP works only on the same host. |
| **A public address** | Browsers load audio from it. HTTPS if the app is. |
| **Access to your TTS providers** | The worker [makes the speech requests](/configure/tts-providers#self-hosted-provider-addresses). |

The worker never connects to the database and never receives `AUTH_SECRET`.

## Shared values [#shared-values]

Generate each once (for example with `openssl rand -base64 32`) and set the same value on both the app and the worker:

| Variable | Used for |
| --- | --- |
| `COMPUTE_WORKER_TOKEN` | The app's requests to the worker. |
| `COMPUTE_CREDENTIAL_BROKER_TOKEN` | The worker's requests to the app. |
| `TTS_PLAYBACK_TOKEN_SECRET` | Signing the short-lived audio and reader links browsers use. |

None of them is ever sent to browsers.

## Worker settings [#worker-settings]

```dotenv
COMPUTE_WORKER_TOKEN=<shared>
COMPUTE_CREDENTIAL_BROKER_URL=https://reader.example.com/api/internal/compute/tts-credentials
COMPUTE_CREDENTIAL_BROKER_TOKEN=<shared>
TTS_PLAYBACK_TOKEN_SECRET=<shared>

NATS_URL=nats://nats.example.com:4222
# NATS_CREDS=<contents of a .creds file>, or NATS_CREDS_FILE=/path/to/user.creds

S3_BUCKET=<same as the app>
S3_REGION=<same as the app>
S3_ACCESS_KEY_ID=<same as the app>
S3_SECRET_ACCESS_KEY=<same as the app>
S3_INTERNAL_ENDPOINT=<the same storage, at an address the worker can reach>
# S3_FORCE_PATH_STYLE and S3_PREFIX: same as the app

LOG_FORMAT=json
COMPUTE_LOG_LEVEL=info
```

The worker listens on `PORT` (default `8081`), which most platforms set for you. The rest rarely needs changing; see [Environment Variables](/reference/environment-variables#compute-worker-and-model-configuration).

## App settings [#app-settings]

```dotenv
COMPUTE_WORKER_URL=https://worker.example.com
COMPUTE_WORKER_TOKEN=<shared>
COMPUTE_CREDENTIAL_BROKER_TOKEN=<shared>
TTS_PLAYBACK_TOKEN_SECRET=<shared>
# Only if browsers reach the worker at a different address:
# COMPUTE_WORKER_PUBLIC_URL=https://worker-public.example.com
```

With `COMPUTE_WORKER_URL` set, the app does not start its own worker. If this one is down, playback and document processing fail until it is back.

<Callout type="warn">
If the app is on HTTPS, the worker's public address must be too, or browsers block the audio.
</Callout>

## Railway [#railway]

<Steps>
<Step>
### Create NATS [#create-nats]

Use the official `nats:alpine` image, with a volume at `/data` and public networking off. Its configuration needs JetStream with `store_dir: "/data"`, so queued work stays on the volume, and `listen: "[::]:4222"`, for Railway's IPv6 private network.

To supply it, base64-encode the configuration into a service variable named `NATS_CONFIG_B64` and set this start command:

```sh
/bin/sh -c "echo $NATS_CONFIG_B64 | base64 -d > /tmp/nats.conf && exec nats-server -c /tmp/nats.conf"
```
</Step>

<Step>
### Create the worker [#create-the-worker]

Create a service from the worker image in the same project and environment, with public networking on.
</Step>

<Step>
### Set its variables [#set-its-variables]

Use the [worker settings](#worker-settings), with `NATS_URL` at the NATS private address, such as `nats://nats.railway.internal:4222`. If NATS uses JWT authentication, put a user's `.creds` contents in `NATS_CREDS`.
</Step>

<Step>
### Point the app at it [#point-the-app-at-it]

Set `COMPUTE_WORKER_URL=https://<railway-worker-domain>` on the app.
</Step>
</Steps>

### Let the worker sleep [#let-the-worker-sleep]

Set `COMPUTE_NATS_DISCONNECT_WHEN_IDLE=true`. The worker disconnects after two idle minutes and reconnects on the next request. Keep at least one worker connected if jobs must recover without a request.

### NATS cluster [#nats-cluster]

A cluster keeps queued work when one server is lost.

1. Run at least three servers, each its own service with its own empty volume.
2. Give each a unique `server_name`, and all of them the same `cluster` block: one cluster name, `listen: "[::]:6222"`, and `routes` to every server's private address.
3. List their client addresses in `NATS_URL`, separated by commas, and set `COMPUTE_NATS_REPLICAS=3` on the worker.

<Accordions>
<Accordion title="Move an existing server's streams to a cluster">
Adding a `cluster` block to a standalone server does not move its streams.

1. Stop the worker.
2. Back up every stream except `KV_OPENREADER_READER_PRESENCE`, which is no longer used.
3. Restore each backup into the new cluster: `nats stream restore <backup-directory> --replicas 3`. Check the replica counts.
4. Point the worker at the new cluster, then start it.
</Accordion>
</Accordions>

## Health checks [#health-checks]

| Path | Response |
| --- | --- |
| `GET /health/live` | `{ "ok": true }` while the process runs. |
| `GET /health/ready` | Adds `natsConnected`. The status is 200 either way. |

After deploying, open **Settings → Admin → Maintenance** in the app. The system check confirms the app can reach the worker and that your browser can load its audio.