authoravatarclover caruso<git@paperclover.net>2026-11-21 20:18:13-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 00:44:03-08:00
log0fe2d0b138d0ef5d5c1560e0cd6537eddfd8db88
tree2f670b157ba8d90d79e3ea702e92e9ef045d44b2
parent3dc3f61c5d3bbda749756a50026ab3694f886b5d
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Spacebar: write the server log where the sandbox can read it

The supervisor now mirrors everything the container prints (npm, tsc, the server, deploy results) to /logs/server.log with timestamps and one rotated copy. Production mounts that directory from the sandbox workspace, created by a prepare script; previews use their own volume. Assisted-by: claude-sonnet-5.5

4 files changed, 69 insertions(+), 6 deletions(-)

service/spacebar/build/supervisor.mjs+45-6
......@@ -1,8 +1,9 @@
11import { execFile, spawn } from "node:child_process";
22import { createHash } from "node:crypto";
3import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
3import { copyFileSync, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
44import { connect, createServer } from "node:net";
55import { basename } from "node:path";
6import { createInterface } from "node:readline";
67import { setTimeout as sleep } from "node:timers/promises";
78import { promisify } from "node:util";
89
......@@ -13,12 +14,50 @@ const POLL_MS = 250;
1314const DRAIN_MS = 500;
1415const RETRY_FAILED_MS = 15000;
1516const GIT = ["-c", "safe.directory=*", "--git-dir", REPO];
17const LOG = "/logs/server.log";
18const LOG_MAX_BYTES = 10 * 1024 * 1024;
1619
1720const out = promisify(execFile);
18const log = (...args) => console.log("[deploy]", ...args);
21
22let sink;
23let sinkBytes = 0;
24function openSink() {
25 sink = createWriteStream(LOG, { flags: "a" }).on("error", () => {});
26 sinkBytes = existsSync(LOG) ? statSync(LOG).size : 0;
27}
28openSink();
29
30/** Everything the container prints is also kept in LOG, with one rotated copy. */
31function record(line) {
32 const text = `${new Date().toISOString()} ${line}\n`;
33 if (sinkBytes + text.length > LOG_MAX_BYTES) {
34 sink.end();
35 try {
36 renameSync(LOG, `${LOG}.1`);
37 } catch {}
38 openSink();
39 }
40 sink.write(text);
41 sinkBytes += text.length;
42}
43
44function capture(proc) {
45 for (const [stream, output] of [[proc.stdout, process.stdout], [proc.stderr, process.stderr]]) {
46 createInterface({ input: stream }).on("line", (line) => {
47 output.write(`${line}\n`);
48 record(line);
49 });
50 }
51 return proc;
52}
53
54const log = (...args) => {
55 console.log("[deploy]", ...args);
56 record(["[deploy]", ...args].join(" "));
57};
1958const run = (cmd, args, opts = {}, ok = [0]) =>
2059 new Promise((resolve, reject) =>
21 spawn(cmd, args, { stdio: "inherit", ...opts })
60 capture(spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"], ...opts }))
2261 .once("error", reject)
2362 .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))),
2463 );
......@@ -145,11 +184,11 @@ async function deploy() {
145184 current();
146185 release.port = await freePort();
147186 // The server keeps jwt.key and other state in its working directory.
148 release.proc = spawn(process.execPath, ["--enable-source-maps", `${release.dir}/dist/bundle/start.js`], {
187 release.proc = capture(spawn(process.execPath, ["--enable-source-maps", `${release.dir}/dist/bundle/start.js`], {
149188 cwd: "/data",
150 stdio: "inherit",
189 stdio: ["ignore", "pipe", "pipe"],
151190 env: { ...process.env, PORT: String(release.port) },
152 });
191 }));
153192 release.proc.once("exit", (code, signal) => {
154193 if (live === release) {
155194 live = null;
service/spacebar/prepare.py created+17
......@@ -0,0 +1,17 @@
1#!/usr/bin/env python3
2"""Create the server log directory in the sandbox workspace, owned by the service."""
3import json
4import os
5from pathlib import Path
6import sys
7
8uid = json.load(sys.stdin)["uid"]
9work = Path("/srv/work")
10if not os.path.ismount(work):
11 raise ValueError("The work SSD is not mounted; refusing to create the log directory on the root disk.")
12logs = work / "snowcord-server/logs"
13if not logs.parent.is_dir():
14 raise ValueError(f"{logs.parent} does not exist; create the sandbox workspace first.")
15logs.mkdir(mode=0o755, exist_ok=True)
16os.chown(logs, uid, uid)
17logs.chmod(0o755)
service/spacebar/service.pkl+3
......@@ -10,6 +10,7 @@ tagline = "Discord-compatible chat"
1010launcher = false
1111stageIsolation = "fresh"
1212healthyDeadline = "10m"
13prepare = if (site.preview) null else "prepare.py"
1314
1415requirements {
1516 new postgres.Database { name = "spacebar" }
......@@ -37,6 +38,8 @@ container {
3738 ["/data"] {}
3839 // Shale keeps each repository under its UUID, which renaming and visibility changes leave alone.
3940 ["/source.git"] { src = "\(site.root)/prod/shale/repositories_owned/006AH3H3QKH2KN318QXRNFBH4B"; readOnly = true }
41 // Production writes the server log into the sandbox workspace; previews keep their own.
42 ["/logs"] { when (!site.preview) { src = "\(site.root)/work/snowcord-server/logs" } }
4043 }
4144 tmpfs { "/work:rw,exec,size=3g" }
4245
tools/spacebar.md+4
......@@ -36,6 +36,10 @@ A tsc error, a crash, or a server that never answers `/api/ping` leaves the
3636previous server running; the reason is in the service log. A newer push cancels a
3737start that is still waiting. Rolling back is pushing the old commit again.
3838
39- The supervisor also writes everything the container prints (npm, tsc, the server, deploy
40 results) to `/logs/server.log`, with one rotated copy at 10 MB. Production mounts that
41 from `/srv/work/snowcord-server/logs`, so `server-logs.sh` beside `push.sh` in the sandbox
42 prints it (`-f` follows); previews keep their own.
3943- `assets/schemas.json` and `assets/openapi.json` deploy as committed. After
4044 changing request or response schemas, run `npm run generate:schema` and
4145 `npm run generate:openapi` and commit the results, as upstream does.