mirror of
https://github.com/stablyai/orca.git
synced 2026-10-23 16:03:49 +00:00
* fix(app): await durable checkpoints before restart actions * fix(app): clear restart latch after refused reload * fix(persistence): invalidate hash after stale rename
67 lines
2.6 KiB
TypeScript
67 lines
2.6 KiB
TypeScript
import { ipcMain } from 'electron'
|
|
import type { ExecutionHostId } from '../../shared/execution-host'
|
|
import type { PersistedUIState, WorkspaceSessionState } from '../../shared/types'
|
|
import type { Store } from '../persistence'
|
|
|
|
type StageBeforeUnloadSyncArgs = {
|
|
sessions: { state: WorkspaceSessionState; hostId?: ExecutionHostId }[]
|
|
ui: Partial<PersistedUIState>
|
|
}
|
|
|
|
export type ShutdownCheckpointResult = { ok: boolean }
|
|
|
|
/** Matches the will-quit teardown budget so a stalled disk can't strand a restart. */
|
|
export const SHUTDOWN_CHECKPOINT_FLUSH_DEADLINE_MS = 20_000
|
|
|
|
function flushStagedStateWithDeadline(store: Store): Promise<ShutdownCheckpointResult> {
|
|
const controller = new AbortController()
|
|
let timer: ReturnType<typeof setTimeout> | null = null
|
|
const deadline = new Promise<ShutdownCheckpointResult>((resolve) => {
|
|
timer = setTimeout(() => {
|
|
controller.abort()
|
|
console.error('[app] Timed out persisting staged renderer state')
|
|
resolve({ ok: false })
|
|
}, SHUTDOWN_CHECKPOINT_FLUSH_DEADLINE_MS)
|
|
})
|
|
// Why not drain to stable: Store retries a superseded staged write without
|
|
// chasing unrelated live mutations, which the deadline would otherwise cut off.
|
|
const flush = store
|
|
.flushPendingOrThrowAsync({ signal: controller.signal, drainToStableGeneration: false })
|
|
.then((): ShutdownCheckpointResult => ({ ok: true }))
|
|
.catch((error): ShutdownCheckpointResult => {
|
|
console.error('[app] Failed to persist staged renderer state:', error)
|
|
return { ok: false }
|
|
})
|
|
return Promise.race([flush, deadline]).finally(() => {
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
}
|
|
})
|
|
}
|
|
|
|
export function registerRendererShutdownCheckpointHandler(store: Store): void {
|
|
// Why: beforeunload cannot await, so the sync reply only reports staging.
|
|
// Durability is joined out-of-band by paths that are about to navigate.
|
|
let pendingCheckpoint: Promise<ShutdownCheckpointResult> = Promise.resolve({ ok: true })
|
|
|
|
ipcMain.on('app:stage-before-unload-sync', (event, args: StageBeforeUnloadSyncArgs) => {
|
|
let ok = true
|
|
try {
|
|
for (const { state, hostId } of args.sessions) {
|
|
store.stageWorkspaceSessionBeforeUnload(state, hostId)
|
|
}
|
|
store.updateUI(args.ui)
|
|
} catch (error) {
|
|
console.error('[app] Failed to stage renderer state before unload:', error)
|
|
ok = false
|
|
}
|
|
pendingCheckpoint = ok ? flushStagedStateWithDeadline(store) : Promise.resolve({ ok: false })
|
|
event.returnValue = { ok }
|
|
})
|
|
|
|
ipcMain.handle(
|
|
'app:await-before-unload-checkpoint',
|
|
(): Promise<ShutdownCheckpointResult> => pendingCheckpoint
|
|
)
|
|
}
|