| ... | @@ -1,8 +1,9 @@ | ... | @@ -1,8 +1,9 @@ |
| 1 | import { execFile, spawn } from "node:child_process"; | 1 | import { execFile, spawn } from "node:child_process"; |
| 2 | import { createHash } from "node:crypto"; | 2 | import { createHash } from "node:crypto"; |
| 3 | import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; | 3 | import { copyFileSync, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; |
| 4 | import { connect, createServer } from "node:net"; | 4 | import { connect, createServer } from "node:net"; |
| 5 | import { basename } from "node:path"; | 5 | import { basename } from "node:path"; |
| | 6 | import { createInterface } from "node:readline"; |
| 6 | import { setTimeout as sleep } from "node:timers/promises"; | 7 | import { setTimeout as sleep } from "node:timers/promises"; |
| 7 | import { promisify } from "node:util"; | 8 | import { promisify } from "node:util"; |
| 8 | | 9 | |
| ... | @@ -13,12 +14,50 @@ const POLL_MS = 250; | ... | @@ -13,12 +14,50 @@ const POLL_MS = 250; |
| 13 | const DRAIN_MS = 500; | 14 | const DRAIN_MS = 500; |
| 14 | const RETRY_FAILED_MS = 15000; | 15 | const RETRY_FAILED_MS = 15000; |
| 15 | const GIT = ["-c", "safe.directory=*", "--git-dir", REPO]; | 16 | const GIT = ["-c", "safe.directory=*", "--git-dir", REPO]; |
| | 17 | const LOG = "/logs/server.log"; |
| | 18 | const LOG_MAX_BYTES = 10 * 1024 * 1024; |
| 16 | | 19 | |
| 17 | const out = promisify(execFile); | 20 | const out = promisify(execFile); |
| 18 | const log = (...args) => console.log("[deploy]", ...args); | 21 | |
| | 22 | let sink; |
| | 23 | let sinkBytes = 0; |
| | 24 | function openSink() { |
| | 25 | sink = createWriteStream(LOG, { flags: "a" }).on("error", () => {}); |
| | 26 | sinkBytes = existsSync(LOG) ? statSync(LOG).size : 0; |
| | 27 | } |
| | 28 | openSink(); |
| | 29 | |
| | 30 | /** Everything the container prints is also kept in LOG, with one rotated copy. */ |
| | 31 | function 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 | |
| | 44 | function 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 | |
| | 54 | const log = (...args) => { |
| | 55 | console.log("[deploy]", ...args); |
| | 56 | record(["[deploy]", ...args].join(" ")); |
| | 57 | }; |
| 19 | const run = (cmd, args, opts = {}, ok = [0]) => | 58 | const 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; |