From b952d99332a054941ad27516c5e83e7222f66c85 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sat, 21 Nov 2026 17:04:38 -0800 Subject: [PATCH] Spacebar: run the fork from Shale and redeploy on push Replace the pinned upstream image with a Node supervisor that watches shale.paperclover.net/discord-server.git, builds with the repo's tsc, starts the new server beside the old one and switches its TCP listener once /api/ping answers. A push is live in about 4 s; a failed build or start leaves the previous server running. Assisted-by: claude-sonnet-5.5 --- service/spacebar/build/Dockerfile | 3 + service/spacebar/build/supervisor.mjs | 208 ++++++++++++++++++++++++++ service/spacebar/service.pkl | 10 +- tools/spacebar.md | 44 ++++++ 4 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 service/spacebar/build/Dockerfile create mode 100644 service/spacebar/build/supervisor.mjs create mode 100644 tools/spacebar.md diff --git a/service/spacebar/build/Dockerfile b/service/spacebar/build/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..364837521609c0ffb39caab0015a26d3e40548ea --- /dev/null +++ b/service/spacebar/build/Dockerfile @@ -0,0 +1,3 @@ +FROM docker.io/library/node:26.13.1-bookworm@sha256:b7087bc4d973034b30854b378ba107f9fab622b47514cb7e2592e41b8bab8d49 +COPY supervisor.mjs /supervisor.mjs +CMD ["node", "/supervisor.mjs"] diff --git a/service/spacebar/build/supervisor.mjs b/service/spacebar/build/supervisor.mjs new file mode 100644 index 0000000000000000000000000000000000000000..ff15a229d385f9beadcfd8e60067b429903444f5 --- /dev/null +++ b/service/spacebar/build/supervisor.mjs @@ -0,0 +1,208 @@ +import { execFile, spawn } from "node:child_process"; +import { createHash } from "node:crypto"; +import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { connect, createServer } from "node:net"; +import { basename } from "node:path"; +import { setTimeout as sleep } from "node:timers/promises"; +import { promisify } from "node:util"; + +const { SOURCE_URL, BRANCH, PORT } = process.env; +const WORK = "/work"; +const REPO = `${WORK}/repo.git`; +const POLL_MS = 500; +const DRAIN_MS = 500; +const RETRY_FAILED_MS = 15000; + +const out = promisify(execFile); +const log = (...args) => console.log("[deploy]", ...args); +const run = (cmd, args, opts = {}, ok = [0]) => + new Promise((resolve, reject) => + spawn(cmd, args, { stdio: "inherit", ...opts }) + .once("error", reject) + .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))), + ); +const git = (...args) => out("git", ["--git-dir", REPO, ...args]).then((result) => result.stdout.trim()); + +let live = null; +let wanted = null; +let recentModules = null; +const releases = new Set(); +let failed = { rev: null, at: 0 }; + +createServer((client) => { + const target = live; + if (!target) return client.destroy(); + const upstream = connect(target.port, "127.0.0.1"); + target.sockets.add(client); + client.setNoDelay(true); + upstream.setNoDelay(true); + const end = () => { + client.destroy(); + upstream.destroy(); + target.sockets.delete(client); + }; + client.pipe(upstream).pipe(client); + client.on("error", end).on("close", end); + upstream.on("error", end).on("close", end); +}).listen(Number(PORT)); + +async function remoteHead() { + const response = await fetch(`${SOURCE_URL}/info/refs?service=git-upload-pack`, { signal: AbortSignal.timeout(5000) }); + if (!response.ok) throw new Error(`${SOURCE_URL} answered ${response.status}`); + return (await response.text()).match(new RegExp(`([0-9a-f]{40}) refs/heads/${BRANCH}(?=\\0|\\n)`))?.[1] ?? null; +} + +async function install(dir, modules) { + const staging = `${modules}.partial`; + rmSync(staging, { recursive: true, force: true }); + mkdirSync(staging, { recursive: true }); + for (const file of ["package.json", "package-lock.json"]) copyFileSync(`${dir}/${file}`, `${staging}/${file}`); + await run("npm", ["ci", "--ignore-scripts", "--no-audit", "--no-fund", "--loglevel=error"], { cwd: staging }); + const { packages } = JSON.parse(readFileSync(`${staging}/package-lock.json`)); + const native = Object.entries(packages) + .filter(([path, pkg]) => path && pkg.hasInstallScript) + .map(([path]) => path.split("node_modules/").pop()); + if (native.length) await run("npm", ["rebuild", "--loglevel=error", ...native], { cwd: staging }); + renameSync(staging, modules); +} + +async function healthy(port, proc, current) { + for (const deadline = Date.now() + 90000; Date.now() < deadline; await sleep(100)) { + if (proc.exitCode !== null || proc.signalCode !== null) throw new Error("server exited during startup"); + current(); + const ok = await fetch(`http://127.0.0.1:${port}/api/ping`, { signal: AbortSignal.timeout(1000) }).then((response) => response.ok, () => false); + if (ok) return; + } + throw new Error("server did not become healthy"); +} + +const freePort = () => + new Promise((resolve) => { + const probe = createServer().listen(0, "127.0.0.1", () => { + const { port } = probe.address(); + probe.close(() => resolve(port)); + }); + }); + +function sweep() { + const used = { rel: new Set(), modules: new Set([recentModules]) }; + for (const release of releases) { + used.rel.add(release.rev); + if (release.modules) used.modules.add(basename(release.modules)).add(`${basename(release.modules)}.partial`); + } + for (const [name, keep] of Object.entries(used)) { + for (const entry of readdirSync(`${WORK}/${name}`)) if (!keep.has(entry)) rmSync(`${WORK}/${name}/${entry}`, { recursive: true, force: true }); + } +} + +function retire(old) { + setTimeout(() => { + for (const socket of old.sockets) socket.destroy(); + old.proc.kill("SIGTERM"); + setTimeout(() => old.proc.kill("SIGKILL"), 5000).unref(); + }, DRAIN_MS); +} + +async function deploy() { + const target = wanted; + const current = () => { + if (wanted !== target) throw new Error("superseded by a newer push"); + }; + let release; + let last = performance.now(); + const start = last; + const laps = []; + const lap = (name) => { + const now = performance.now(); + laps.push(`${name} ${Math.round(now - last)}ms`); + last = now; + }; + + try { + await run("git", ["--git-dir", REPO, "fetch", "--quiet", "--depth", "1", "--no-tags", SOURCE_URL, `refs/heads/${BRANCH}`]); + const rev = await git("rev-parse", "FETCH_HEAD"); + if (rev === live?.rev) return; + const [committedAt, subject] = (await git("log", "-1", "--format=%ct%n%s", rev)).split("\n"); + log(`deploying ${rev.slice(0, 7)} ${subject}`); + lap("fetch"); + + release = { rev, dir: `${WORK}/rel/${rev}`, modules: null, port: 0, proc: null, sockets: new Set() }; + releases.add(release); + rmSync(release.dir, { recursive: true, force: true }); + mkdirSync(release.dir, { recursive: true }); + await run("bash", ["-c", 'set -o pipefail; git --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]); + lap("extract"); + + const lock = createHash("sha256"); + for (const file of ["package.json", "package-lock.json"]) lock.update(readFileSync(`${release.dir}/${file}`)); + release.modules = `${WORK}/modules/${lock.digest("hex").slice(0, 16)}`; + const cached = existsSync(release.modules); + if (!cached) await install(release.dir, release.modules); + symlinkSync(`${release.modules}/node_modules`, `${release.dir}/node_modules`); + lap(cached ? "deps cached" : "deps installed"); + + await run(`${release.dir}/node_modules/.bin/tsc`, ["-p", "tsconfig.json", "--declaration", "false", "--declarationMap", "false", "--incremental", "false"], { cwd: release.dir }); + writeFileSync(`${release.dir}/.rev`, JSON.stringify({ rev, shortRev: rev.slice(0, 7), lastModified: Number(committedAt) })); + lap("build"); + + current(); + release.port = await freePort(); + // The server keeps jwt.key and other state in its working directory. + release.proc = spawn(process.execPath, ["--enable-source-maps", `${release.dir}/dist/bundle/start.js`], { + cwd: "/data", + stdio: "inherit", + env: { ...process.env, PORT: String(release.port) }, + }); + release.proc.once("exit", (code, signal) => { + if (live === release) { + live = null; + log(`server ${rev.slice(0, 7)} exited (${code ?? signal})`); + } + releases.delete(release); + sweep(); + }); + try { + await healthy(release.port, release.proc, current); + } catch (error) { + release.proc.kill("SIGKILL"); + throw error; + } + lap("start"); + + const old = live; + live = release; + recentModules = basename(release.modules); + if (old) retire(old); + log(`live ${rev.slice(0, 7)} in ${((performance.now() - start) / 1000).toFixed(1)}s: ${laps.join(", ")}`); + } catch (error) { + log(`${target.slice(0, 7)} failed: ${error.message}`); + failed = { rev: target, at: Date.now() }; + if (release) releases.delete(release); + sweep(); + } +} + +mkdirSync(`${WORK}/rel`, { recursive: true }); +mkdirSync(`${WORK}/modules`, { recursive: true }); +await run("git", ["init", "--quiet", "--bare", REPO]); +process.on("SIGTERM", () => process.exit(0)); + +(async () => { + let problem = ""; + for (;; await sleep(POLL_MS)) { + try { + const head = await remoteHead(); + if (!head) throw new Error(`${SOURCE_URL} has no branch ${BRANCH}`); + wanted = head; + problem = ""; + } catch (error) { + if (error.message !== problem) log((problem = error.message)); + } + } +})(); + +for (;;) { + const retry = wanted === failed.rev && !live && Date.now() - failed.at > RETRY_FAILED_MS; + if (wanted && wanted !== live?.rev && (wanted !== failed.rev || retry)) await deploy(); + else await sleep(100); +} diff --git a/service/spacebar/service.pkl b/service/spacebar/service.pkl index 170af0a40bf6b0e17b0fc7fd58edec012a9603b4..0f828580464af6466847e873534680887fda1e58 100644 --- a/service/spacebar/service.pkl +++ b/service/spacebar/service.pkl @@ -21,8 +21,10 @@ secrets { } container { - image = "ghcr.io/spacebarchat/server@sha256:f50099777cee98d2d92290c792825ad02d705263253fae60d7b7e1f74fce46bb" - cpu = 300 + build = "build" + cpu = 1000 + memory = 3072 + extraHosts { "shale.paperclover.net:host-gateway" } http { containerPort = 3001 @@ -34,8 +36,12 @@ container { volumes { ["/data"] {} } + tmpfs { "/work:rw,exec,size=3g" } env { + ["SOURCE_URL"] = "https://shale.paperclover.net/discord-server.git" + ["BRANCH"] = "main" + ["HOME"] = "/work" ["CONFIG_PATH"] = "/spacebar/config.json" ["CONFIG_READONLY"] = "true" ["STORAGE_LOCATION"] = "/data/files" diff --git a/tools/spacebar.md b/tools/spacebar.md new file mode 100644 index 0000000000000000000000000000000000000000..963c1fbe0043167f75b39b4645982e837e729ccb --- /dev/null +++ b/tools/spacebar.md @@ -0,0 +1,44 @@ +# Spacebar + +`https://spacebar.paperclover.net` runs Clover's fork of +[spacebarchat/server](https://github.com/spacebarchat/server) from branch `main` of +`https://shale.paperclover.net/discord-server.git`. Pushing to that branch is the +deploy; `tools/deploy.py` is only needed to change the service definition. + +``` +~/dev/discord-server jj checkout, remotes: origin = Shale, upstream = GitHub + (`master` mirrors upstream, `main` is what runs) +jj bookmark set main -r @- && jj git push -b main +curl https://spacebar.paperclover.net/_spacebar/api/version # commit that is live +``` + +A push is live about 4 seconds later (3.5 to 4.6 s measured from the start of the push). Dependency changes +(`package.json` or the lockfile) add an `npm ci`, about 5 s. Previews run the same +supervisor against the same branch with a fresh database. + +## How it works + +[`supervisor.mjs`](../service/spacebar/build/supervisor.mjs) is the container's +only process, on a stock Node image with its working files in tmpfs `/work`. + +1. Every 500 ms it reads the branch tip from Shale's `info/refs`. +2. On a new tip it shallow-fetches, extracts the tree, installs `node_modules` + only when the dependency files changed, and builds with the repo's own + `node_modules/.bin/tsc` (TypeScript 7, about 1 s). +3. It starts the new server on a free port, waits for `/api/ping`, then points its + TCP listener on 3001 at the new one. The old server is stopped 500 ms later, so + open gateway sockets drop and clients reconnect. + +A tsc error, a crash, or a server that never answers `/api/ping` leaves the +previous server running; the reason is in the service log. A newer push cancels a +start that is still waiting. Rolling back is pushing the old commit again. + +- `assets/schemas.json` and `assets/openapi.json` deploy as committed. After + changing request or response schemas, run `npm run generate:schema` and + `npm run generate:openapi` and commit the results, as upstream does. +- The server runs with `/data` as its working directory, so `jwt.key`, uploads and + sessions survive deploys. +- All revisions share one database. A migration in a pushed commit runs on first + start and a revert does not undo it. The old and new server briefly overlap. +- Changing the supervisor, image or environment redeploys the container, which + takes about 9 s to rebuild and start from the branch. -- 2.54.1