also looking at this
fix(app): report a crash instead of vanishing, and cap the log
#55
4 files
+252
-5
| @@ -11,14 +11,14 @@ | ||
| 11 | 11 | * Run from source for development with `bun app/Desktop/launcher.ts`; Craft |
| 12 | 12 | * then comes from CRAFT_BIN or PATH (pantry installs it). |
| 13 | 13 | */ |
| 14 | import { appendFileSync, existsSync, mkdirSync } from 'node:fs' | |
| 14 | import { existsSync } from 'node:fs' | |
| 15 | 15 | import { dirname, join } from 'node:path' |
| 16 | 16 | import process from 'node:process' |
| 17 | 17 | import { format } from 'node:util' |
| 18 | 18 | import pkg from '../../package.json' |
| 19 | 19 | import { agentPath } from '../Uplink/agent-path' |
| 20 | 20 | import { acquireSingleInstance, startDesktopAgent } from '../Uplink/desktop-agent' |
| 21 | import { LOG_PATH } from '../Uplink/settings' | |
| 21 | import { installCrashHandlers, writeLog } from '../Uplink/log' | |
| 22 | 22 | |
| 23 | 23 | // Launched from Finder or from its own LaunchAgent, this process gets the bare |
| 24 | 24 | // launchd PATH, and everything below it inherits that: the engines, their |
| @@ -28,14 +28,31 @@ import { LOG_PATH } from '../Uplink/settings' | ||
| 28 | 28 | process.env.PATH = agentPath() |
| 29 | 29 | |
| 30 | 30 | // Launched from Finder or at login there is no terminal, so keep a log where |
| 31 | // Console.app looks for one. | |
| 31 | // Console.app looks for one. From a terminal the developer keeps stderr. | |
| 32 | 32 | if (!process.stdout.isTTY) { |
| 33 | mkdirSync(dirname(LOG_PATH), { recursive: true }) | |
| 34 | 33 | for (const level of ['log', 'warn', 'error'] as const) { |
| 35 | console[level] = (...args: unknown[]) => appendFileSync(LOG_PATH, `${new Date().toISOString()} ${format(...args)}\n`) | |
| 34 | console[level] = (...args: unknown[]) => writeLog(format(...args)) | |
| 36 | 35 | } |
| 37 | 36 | } |
| 38 | 37 | |
| 38 | /** | |
| 39 | * Whether the things `shutdown` closes over exist yet. A crash during startup | |
| 40 | * is both the one most worth reporting and the one where calling `shutdown` | |
| 41 | * would itself throw, so that case exits directly. The single instance lock is | |
| 42 | * reaped by the next start. | |
| 43 | */ | |
| 44 | let running = false | |
| 45 | ||
| 46 | installCrashHandlers({ | |
| 47 | shutdown: (code) => { | |
| 48 | if (!running) | |
| 49 | process.exit(code) | |
| 50 | // A hung agent.stop() must not keep a half dead process alive. | |
| 51 | setTimeout(() => process.exit(code), 3000).unref() | |
| 52 | void shutdown(code) | |
| 53 | }, | |
| 54 | }) | |
| 55 | ||
| 39 | 56 | if (!acquireSingleInstance()) { |
| 40 | 57 | console.log('[uplink] already running') |
| 41 | 58 | process.exit(0) |
| @@ -84,6 +101,9 @@ void new Response(craft.stderr).text().then((stderr) => { | ||
| 84 | 101 | |
| 85 | 102 | // The point is answering while you are away, so hold off idle sleep. |
| 86 | 103 | const keepAwake = Bun.spawn(['/usr/bin/caffeinate', '-i', '-w', String(process.pid)], { stdio: ['ignore', 'ignore', 'ignore'] }) |
| 104 | // Everything `shutdown` touches now exists, so a crash from here on can go | |
| 105 | // through it rather than exiting on the spot. | |
| 106 | running = true | |
| 87 | 107 | |
| 88 | 108 | let stopping = false |
| 89 | 109 | async function shutdown(code: number): Promise<never> { |