Open source CapCut alternative.
TypeScript
fix(cut): wake the render worker on its own hostname
David Lef56abd5b0a7e
diff --git a/site/.env.example b/site/.env.exampleindex dafa1b50..2c57b70a 100644--- a/site/.env.example+++ b/site/.env.example@@ -54,10 +54,10 @@ R2_ACCOUNT_ID="replace-with-r2-account-id"R2_ACCESS_KEY_ID="replace-with-r2-access-key-id"R2_SECRET_ACCESS_KEY="replace-with-r2-secret-access-key"-# Cut render worker wake-on-demand. URL is the deployed Worker's /wake endpoint;-# the secret must match the Worker's CUT_WAKE_SECRET. Unset in local dev (run-# the worker by hand; it polls on its own).-CUT_RENDER_WAKE_URL="replace-with-worker-wake-url"+# Cut render worker wake-on-demand. Holding this secret is what turns it on; it+# must match the Worker's CUT_WAKE_SECRET. The address it posts to is code+# (CUT_WORKER_WAKE_URL in src/cut/lib/hosts.ts). Unset in local dev (run the+# worker by hand; it polls on its own).CUT_RENDER_WAKE_SECRET="replace-with-wake-secret"# Shared-media edge delivery. Holding this secret is what turns it on: a shareddiff --git a/site/src/cut/lib/hosts.ts b/site/src/cut/lib/hosts.tsindex ba653af3..25e717e9 100644--- a/site/src/cut/lib/hosts.ts+++ b/site/src/cut/lib/hosts.ts@@ -17,6 +17,14 @@ export const DONKEYCUT_CANONICAL = "https://donkeycut.com";export const CUT_MEDIA_HOST = "media.donkeycut.com";export const CUT_MEDIA_ORIGIN = `https://${CUT_MEDIA_HOST}`;+// The Cut Worker's control plane — the /wake the hosted API posts to when it+// queues a render. Its own custom domain, claimed in wrangler.jsonc beside the+// media route, so it cannot be revoked by a deploy default the way the+// workers.dev address was: adding the media route turned workers.dev off and+// stranded every render for two days, with nothing to see but queued jobs.+export const CUT_WORKER_HOST = "worker.donkeycut.com";+export const CUT_WORKER_WAKE_URL = `https://${CUT_WORKER_HOST}/wake`;+function hostname(host: string | null | undefined): string {return host ? host.split(":")[0] : "";}diff --git a/site/src/cut/server/cloud/wake.ts b/site/src/cut/server/cloud/wake.tsindex 0db68348..58ffe0e2 100644--- a/site/src/cut/server/cloud/wake.ts+++ b/site/src/cut/server/cloud/wake.ts@@ -1,15 +1,28 @@// Wake the render worker's container. Fire-and-forget: a lost wake self-heals// because the client keeps polling its queued job, and every such poll wakes-// again. The URL is per-deployment cross-service wiring (the Worker's-// workers.dev address), so it rides env beside the secret; with neither set-// (local dev), the dev worker is run by hand and polls on its own.-const wakeUrl = () => process.env.CUT_RENDER_WAKE_URL;+// again. The address is a hostname, so it lives in code (lib/hosts.ts) with the+// route that serves it; the shared secret is the only environment input, and+// holding it is the single switch that turns waking on. Unset (local dev), the+// dev worker is run by hand and polls on its own.+import { CUT_WORKER_WAKE_URL } from "@/cut/lib/hosts";+const wakeSecret = () => process.env.CUT_RENDER_WAKE_SECRET;++// A wake that never lands strands every render — the container is not started+// and jobs queue forever — and retrying can't help, because the address itself+// is wrong. Nothing waits on the result, so the log is the only place it can+// show up; swallowing it is what let a dead wake go unnoticed for two days.export function wakeRenderWorker(): void {- const url = wakeUrl();- if (!url) return;- void fetch(url, {+ const secret = wakeSecret();+ if (!secret) return;+ void fetch(CUT_WORKER_WAKE_URL, {method: "POST",- headers: { authorization: `Bearer ${process.env.CUT_RENDER_WAKE_SECRET ?? ""}` },- }).catch(() => {});+ headers: { authorization: `Bearer ${secret}` },+ })+ .then((res) => {+ if (!res.ok) console.error(`[cut] render worker wake refused: HTTP ${res.status}`);+ })+ .catch((e: unknown) => {+ console.error("[cut] render worker wake failed:", e instanceof Error ? e.message : e);+ });}diff --git a/site/src/cut/worker/README.md b/site/src/cut/worker/README.mdindex 809430c8..d5faf0ce 100644--- a/site/src/cut/worker/README.md+++ b/site/src/cut/worker/README.md@@ -46,9 +46,12 @@ POSTs the Worker's `/wake` endpoint (bearer `CUT_WAKE_SECRET`) whenever itqueues a job and again on every poll of a still-queued job, so a lost wakeself-heals; `main.ts` exits after ~60s of empty queue so the container stopsbilling. Cold start is a few seconds. The hosted deployment needs-`CUT_RENDER_WAKE_URL` (the Worker's `/wake` URL) and `CUT_RENDER_WAKE_SECRET`-in its env to send wakes — without them, queued jobs wait for a manually run-worker.+`CUT_RENDER_WAKE_SECRET` in its env to send wakes — without it, queued jobs+wait for a manually run worker. The address is code, not config: the Worker+claims `worker.donkeycut.com` as its own custom domain, named in+`src/cut/lib/hosts.ts`. That is deliberate — the wake used to ride the+workers.dev address, and declaring the media route made wrangler turn+workers.dev off, stranding every render with nothing to see but queued jobs.GitHub Actions deploys automatically on push to `main` when the worker or theshared pipeline code changes (`.github/workflows/deploy-cut-worker.yml`).@@ -63,10 +66,8 @@ One-time setup:and the same for `R2_ACCOUNT_ID`, `R2_ACCESS_KEY_ID`,`R2_SECRET_ACCESS_KEY`, and `CUT_WAKE_SECRET` (any long random string).Use the Supabase connection-pooler URL for `DATABASE_URL`.-4. On the hosted site (Vercel env): `CUT_RENDER_WAKE_URL` = the deployed- Worker's URL + `/wake` (e.g.- `https://donkey-cut-worker.<subdomain>.workers.dev/wake`) and- `CUT_RENDER_WAKE_SECRET` = the same random string.+4. On the hosted site (Vercel env): `CUT_RENDER_WAKE_SECRET` = the same random+ string.Manual deploy from `site/`:diff --git a/site/src/cut/worker/cf/worker.ts b/site/src/cut/worker/cf/worker.tsindex c6e4f51d..163ecc9e 100644--- a/site/src/cut/worker/cf/worker.ts+++ b/site/src/cut/worker/cf/worker.ts@@ -6,7 +6,7 @@// This file is compiled by wrangler, not the site's tsconfig — workers// globals are typed loosely on purpose.import { Container, getContainer } from "@cloudflare/containers";-import { CUT_MEDIA_HOST } from "../../lib/hosts";+import { CUT_MEDIA_HOST, CUT_WORKER_HOST } from "../../lib/hosts";import { serveMedia, type MediaEnv } from "./media";type WorkerEnv = MediaEnv & {@@ -54,10 +54,13 @@ export default {ctx: { waitUntil(p: Promise<unknown>): void }): Promise<Response> {const url = new URL(request.url);- // Shared media arrives on its own hostname (see wrangler.jsonc routes) and- // is the only public traffic this Worker takes. Matching on the host keeps- // it clear of the container's control endpoints below.- if (url.hostname === CUT_MEDIA_HOST) return serveMedia(request, env, ctx);+ // Two hostnames, two jobs (both claimed in wrangler.jsonc): media is the+ // only public traffic this Worker takes, and the control plane is where the+ // hosted API wakes the container. The control host is matched first — the+ // media branch returns, so a /wake arriving there would be swallowed.+ if (url.hostname !== CUT_WORKER_HOST && url.hostname === CUT_MEDIA_HOST) {+ return serveMedia(request, env, ctx);+ }if (request.method === "POST" && url.pathname === "/wake") {// Starting the container bills CPU, so the wake is not public: callers// present the shared secret the hosted API holds.diff --git a/site/src/cut/worker/wrangler.jsonc b/site/src/cut/worker/wrangler.jsoncindex 74e80fb2..5ecfb11f 100644--- a/site/src/cut/worker/wrangler.jsonc+++ b/site/src/cut/worker/wrangler.jsonc@@ -27,11 +27,19 @@},// Shared media is served from the edge (cf/media.ts) with the bucket kept// private: this binding is the only way in. The Cache API is inert on- // workers.dev, so the route has to be a real hostname on the zone; it must- // match CUT_MEDIA_HOST in ../../lib/hosts.ts, which is what the handler- // matches on. Wrangler creates the DNS record and certificate on deploy.+ // workers.dev, so the route has to be a real hostname on the zone."r2_buckets": [{ "binding": "CUT_MEDIA", "bucket_name": "donkey-cut" }],- "routes": [{ "pattern": "media.donkeycut.com", "custom_domain": true }],+ // Both hostnames are declared here and named in ../../lib/hosts.ts, which is+ // what cf/worker.ts matches on; change a pattern and that constant together.+ // Wrangler creates the DNS record and certificate for each on deploy.+ // The control host carries /wake. It is a route on the zone rather than the+ // workers.dev address on purpose: declaring the media route made wrangler+ // default workers_dev to false, which took workers.dev down and stranded+ // every render until this moved off it.+ "routes": [+ { "pattern": "media.donkeycut.com", "custom_domain": true },+ { "pattern": "worker.donkeycut.com", "custom_domain": true }+ ],"migrations": [{ "tag": "v1", "new_sqlite_classes": ["CutRenderWorker"] }],// Share-copy jobs drain serially: one message per batch, one batch in// flight, so a burst of copy requests can't stampede R2 or the database.