Messages queued while a session is busy used to live in the browser tab and were sent by that tab once the session went idle, so closing the tab (or losing the connection) stranded them. The web server now owns the queue: it persists to <data-dir>/message-queue.json, watches session.status on the global event hub, re-verifies idleness against OpenCode before sending, and delivers the head of the queue via prompt_async (or /command for slash commands) with the model, agent, variant, attachments, and agent mention captured at queue time. Failed sends stay queued and retry with backoff; a user abort holds delivery briefly; every change is broadcast so all clients see one queue. The shared UI store becomes a projection of the server queue outside VS Code (hydrate on connect, apply broadcasts, optimistic mutations settled on the server's copy, one-time upload of locally queued messages from older builds). Edit / send-now take the full message back from the server. A UI-driven auto-review run asks the server to hold that session's queue. VS Code keeps its local queue and foreground auto-send. Claude-Session: https://claude.ai/code/session_01HB9wdLQoZX2vfyDjwv6Rso
156 lines
4.1 KiB
JavaScript
156 lines
4.1 KiB
JavaScript
export const createGracefulShutdownRuntime = (dependencies) => {
|
|
const {
|
|
process,
|
|
shutdownTimeoutMs,
|
|
getExitOnShutdown,
|
|
getIsShuttingDown,
|
|
setIsShuttingDown,
|
|
syncToHmrState,
|
|
openCodeWatcherRuntime,
|
|
sessionRuntime,
|
|
sessionAssistRuntime,
|
|
sessionGoalRuntime,
|
|
contextObligatoryRuntime,
|
|
messageQueueRuntime,
|
|
scheduledTasksRuntime,
|
|
getHealthCheckInterval,
|
|
clearHealthCheckInterval,
|
|
getTerminalRuntime,
|
|
setTerminalRuntime,
|
|
getMessageStreamRuntime,
|
|
setMessageStreamRuntime,
|
|
shouldSkipOpenCodeStop,
|
|
getOpenCodePort,
|
|
getOpenCodeProcess,
|
|
setOpenCodeProcess,
|
|
killProcessOnPort,
|
|
waitForPortRelease,
|
|
getServer,
|
|
getUiAuthController,
|
|
setUiAuthController,
|
|
getActiveTunnelController,
|
|
setActiveTunnelController,
|
|
tunnelAuthController,
|
|
} = dependencies;
|
|
|
|
let shutdownPromise = null;
|
|
|
|
const runShutdown = async (options = {}) => {
|
|
if (getIsShuttingDown()) return;
|
|
|
|
setIsShuttingDown(true);
|
|
syncToHmrState();
|
|
console.log('Starting graceful shutdown...');
|
|
const exitProcess = typeof options.exitProcess === 'boolean' ? options.exitProcess : getExitOnShutdown();
|
|
|
|
openCodeWatcherRuntime.stop();
|
|
sessionRuntime.dispose();
|
|
sessionAssistRuntime?.stop?.();
|
|
sessionGoalRuntime?.stop?.();
|
|
contextObligatoryRuntime?.stop?.();
|
|
messageQueueRuntime?.stop?.();
|
|
scheduledTasksRuntime?.stop?.();
|
|
|
|
const healthCheckInterval = getHealthCheckInterval();
|
|
if (healthCheckInterval) {
|
|
clearHealthCheckInterval(healthCheckInterval);
|
|
}
|
|
|
|
const terminalRuntime = getTerminalRuntime();
|
|
if (terminalRuntime) {
|
|
try {
|
|
await terminalRuntime.shutdown();
|
|
} catch {
|
|
} finally {
|
|
setTerminalRuntime(null);
|
|
}
|
|
}
|
|
|
|
const messageStreamRuntime = getMessageStreamRuntime();
|
|
if (messageStreamRuntime) {
|
|
try {
|
|
await messageStreamRuntime.close();
|
|
} catch {
|
|
} finally {
|
|
setMessageStreamRuntime(null);
|
|
}
|
|
}
|
|
|
|
if (!shouldSkipOpenCodeStop()) {
|
|
const portToKill = getOpenCodePort();
|
|
const openCodeProcess = getOpenCodeProcess();
|
|
|
|
if (openCodeProcess) {
|
|
console.log('Stopping OpenCode process...');
|
|
try {
|
|
await openCodeProcess.close();
|
|
} catch (error) {
|
|
console.warn('Error closing OpenCode process:', error);
|
|
}
|
|
setOpenCodeProcess(null);
|
|
}
|
|
|
|
killProcessOnPort(portToKill);
|
|
if (!(await waitForPortRelease(portToKill, 5000))) {
|
|
console.warn(`Timed out waiting for OpenCode port ${portToKill} to be released during shutdown`);
|
|
}
|
|
} else {
|
|
console.log('Skipping OpenCode shutdown (external server)');
|
|
}
|
|
|
|
const server = getServer();
|
|
if (server) {
|
|
let closeTimeout = null;
|
|
try {
|
|
await Promise.race([
|
|
new Promise((resolve) => {
|
|
server.close(() => {
|
|
console.log('HTTP server closed');
|
|
resolve();
|
|
});
|
|
}),
|
|
new Promise((resolve) => {
|
|
closeTimeout = setTimeout(() => {
|
|
console.warn('Server close timeout reached, forcing shutdown');
|
|
resolve();
|
|
}, shutdownTimeoutMs);
|
|
}),
|
|
]);
|
|
} finally {
|
|
if (closeTimeout) {
|
|
clearTimeout(closeTimeout);
|
|
}
|
|
}
|
|
}
|
|
|
|
const uiAuthController = getUiAuthController();
|
|
if (uiAuthController) {
|
|
uiAuthController.dispose();
|
|
setUiAuthController(null);
|
|
}
|
|
|
|
const activeTunnelController = getActiveTunnelController();
|
|
if (activeTunnelController) {
|
|
console.log('Stopping active tunnel...');
|
|
activeTunnelController.stop();
|
|
setActiveTunnelController(null);
|
|
tunnelAuthController.clearActiveTunnel();
|
|
}
|
|
|
|
console.log('Graceful shutdown complete');
|
|
if (exitProcess) {
|
|
process.exit(0);
|
|
}
|
|
};
|
|
|
|
const gracefulShutdown = (options = {}) => {
|
|
if (shutdownPromise) return shutdownPromise;
|
|
shutdownPromise = runShutdown(options);
|
|
return shutdownPromise;
|
|
};
|
|
|
|
return {
|
|
gracefulShutdown,
|
|
};
|
|
};
|