End all sessions on server startup
This commit is contained in:
parent
28920eca1e
commit
423fa8375d
3 changed files with 18 additions and 0 deletions
0
data/clarc.db
Normal file
0
data/clarc.db
Normal file
|
|
@ -31,6 +31,7 @@ let getPendingPromptsStmt: ReturnType<Database["prepare"]>;
|
||||||
let appendOutputStmt: ReturnType<Database["prepare"]>;
|
let appendOutputStmt: ReturnType<Database["prepare"]>;
|
||||||
let getSessionOutputStmt: ReturnType<Database["prepare"]>;
|
let getSessionOutputStmt: ReturnType<Database["prepare"]>;
|
||||||
let updateSessionStatsStmt: ReturnType<Database["prepare"]>;
|
let updateSessionStatsStmt: ReturnType<Database["prepare"]>;
|
||||||
|
let endAllSessionsStmt: ReturnType<Database["prepare"]>;
|
||||||
|
|
||||||
// Migration function to add new columns to existing tables
|
// Migration function to add new columns to existing tables
|
||||||
function runMigrations(): void {
|
function runMigrations(): void {
|
||||||
|
|
@ -153,6 +154,9 @@ export function initDb(
|
||||||
git_branch = ?, git_files_json = ?
|
git_branch = ?, git_files_json = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`);
|
`);
|
||||||
|
endAllSessionsStmt = db.prepare(
|
||||||
|
"UPDATE sessions SET ended_at = ? WHERE ended_at IS NULL",
|
||||||
|
);
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
@ -198,6 +202,11 @@ export function getActiveSessions(): Session[] {
|
||||||
return getActiveSessionsStmt.all() as Session[];
|
return getActiveSessionsStmt.all() as Session[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function endAllSessions(): number {
|
||||||
|
const result = endAllSessionsStmt.run(Date.now());
|
||||||
|
return result.changes;
|
||||||
|
}
|
||||||
|
|
||||||
// Prompt functions
|
// Prompt functions
|
||||||
|
|
||||||
export function createPrompt(
|
export function createPrompt(
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
createDevice,
|
createDevice,
|
||||||
createPrompt,
|
createPrompt,
|
||||||
createSession,
|
createSession,
|
||||||
|
endAllSessions,
|
||||||
endSession,
|
endSession,
|
||||||
getActiveSessions,
|
getActiveSessions,
|
||||||
getDeviceBySecret,
|
getDeviceBySecret,
|
||||||
|
|
@ -82,6 +83,14 @@ function broadcastSSE(event: SSEEvent): void {
|
||||||
const port = Number.parseInt(process.env.PORT || "7200", 10);
|
const port = Number.parseInt(process.env.PORT || "7200", 10);
|
||||||
initDb();
|
initDb();
|
||||||
|
|
||||||
|
// Clean up ghost sessions from previous runs
|
||||||
|
const cleanedSessions = endAllSessions();
|
||||||
|
if (cleanedSessions > 0) {
|
||||||
|
console.log(
|
||||||
|
`Cleaned up ${cleanedSessions} ghost session(s) from previous runs`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Auto-seed device from env if configured
|
// Auto-seed device from env if configured
|
||||||
const envSecret = process.env.CLAUDE_REMOTE_SECRET;
|
const envSecret = process.env.CLAUDE_REMOTE_SECRET;
|
||||||
if (envSecret) {
|
if (envSecret) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue