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 @@...@@ -1,8 +1,9 @@
1import { execFile, spawn } from "node:child_process";1import { execFile, spawn } from "node:child_process";
2import { createHash } from "node:crypto";2import { 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";
4import { connect, createServer } from "node:net";4import { connect, createServer } from "node:net";
5import { basename } from "node:path";5import { basename } from "node:path";
6import { createInterface } from "node:readline";
6import { setTimeout as sleep } from "node:timers/promises";7import { setTimeout as sleep } from "node:timers/promises";
7import { promisify } from "node:util";8import { promisify } from "node:util";
89
...@@ -13,12 +14,50 @@ const POLL_MS = 250;...@@ -13,12 +14,50 @@ const POLL_MS = 250;
13const DRAIN_MS = 500;14const DRAIN_MS = 500;
14const RETRY_FAILED_MS = 15000;15const RETRY_FAILED_MS = 15000;
15const GIT = ["-c", "safe.directory=*", "--git-dir", REPO];16const GIT = ["-c", "safe.directory=*", "--git-dir", REPO];
17const LOG = "/logs/server.log";
18const LOG_MAX_BYTES = 10 * 1024 * 1024;
1619
17const out = promisify(execFile);20const 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};
19const run = (cmd, args, opts = {}, ok = [0]) =>58const run = (cmd, args, opts = {}, ok = [0]) =>
20 new Promise((resolve, reject) =>59 new Promise((resolve, reject) =>
21 spawn(cmd, args, { stdio: "inherit", ...opts })60 capture(spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"], ...opts }))
22 .once("error", reject)61 .once("error", reject)
23 .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))),62 .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))),
24 );63 );
...@@ -145,11 +184,11 @@ async function deploy() {...@@ -145,11 +184,11 @@ async function deploy() {
145 current();184 current();
146 release.port = await freePort();185 release.port = await freePort();
147 // The server keeps jwt.key and other state in its working directory.186 // 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`], {
149 cwd: "/data",188 cwd: "/data",
150 stdio: "inherit",189 stdio: ["ignore", "pipe", "pipe"],
151 env: { ...process.env, PORT: String(release.port) },190 env: { ...process.env, PORT: String(release.port) },
152 });191 }));
153 release.proc.once("exit", (code, signal) => {192 release.proc.once("exit", (code, signal) => {
154 if (live === release) {193 if (live === release) {
155 live = null;194 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"...@@ -10,6 +10,7 @@ tagline = "Discord-compatible chat"
10launcher = false10launcher = false
11stageIsolation = "fresh"11stageIsolation = "fresh"
12healthyDeadline = "10m"12healthyDeadline = "10m"
13prepare = if (site.preview) null else "prepare.py"
1314
14requirements {15requirements {
15 new postgres.Database { name = "spacebar" }16 new postgres.Database { name = "spacebar" }
...@@ -37,6 +38,8 @@ container {...@@ -37,6 +38,8 @@ container {
37 ["/data"] {}38 ["/data"] {}
38 // Shale keeps each repository under its UUID, which renaming and visibility changes leave alone.39 // Shale keeps each repository under its UUID, which renaming and visibility changes leave alone.
39 ["/source.git"] { src = "\(site.root)/prod/shale/repositories_owned/006AH3H3QKH2KN318QXRNFBH4B"; readOnly = true }40 ["/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" } }
40 }43 }
41 tmpfs { "/work:rw,exec,size=3g" }44 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...@@ -36,6 +36,10 @@ A tsc error, a crash, or a server that never answers `/api/ping` leaves the
36previous server running; the reason is in the service log. A newer push cancels a36previous server running; the reason is in the service log. A newer push cancels a
37start that is still waiting. Rolling back is pushing the old commit again.37start 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.
39- `assets/schemas.json` and `assets/openapi.json` deploy as committed. After43- `assets/schemas.json` and `assets/openapi.json` deploy as committed. After
40 changing request or response schemas, run `npm run generate:schema` and44 changing request or response schemas, run `npm run generate:schema` and
41 `npm run generate:openapi` and commit the results, as upstream does.45 `npm run generate:openapi` and commit the results, as upstream does.