Skip to content

Commit f1ff8cd

Browse files
committed
refactor(core): tighten restart recovery scan
1 parent 6c16d4d commit f1ff8cd

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

  • packages/core/src/session/execution

packages/core/src/session/execution/local.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,38 @@ export function terminal(exit: Exit.Exit<void, SessionRunner.RunError>, reason?:
2525

2626
export const sessionsInterruptedByShutdown = Effect.fn("SessionExecutionLocal.sessionsInterruptedByShutdown")(
2727
function* (db: Database.Interface["db"]) {
28-
const latest = yield* db.all<{ sessionID: string; type: string; reason: string | null }>(sql`
29-
SELECT aggregate_id AS sessionID, type, json_extract(data, '$.reason') AS reason
28+
const started = EventV2.versionedType(
29+
SessionEvent.Execution.Started.type,
30+
SessionEvent.Execution.Started.durable.version,
31+
)
32+
const succeeded = EventV2.versionedType(
33+
SessionEvent.Execution.Succeeded.type,
34+
SessionEvent.Execution.Succeeded.durable.version,
35+
)
36+
const failed = EventV2.versionedType(
37+
SessionEvent.Execution.Failed.type,
38+
SessionEvent.Execution.Failed.durable.version,
39+
)
40+
const interrupted = EventV2.versionedType(
41+
SessionEvent.Execution.Interrupted.type,
42+
SessionEvent.Execution.Interrupted.durable.version,
43+
)
44+
const latest = yield* db.all<{ sessionID: string }>(sql`
45+
SELECT aggregate_id AS sessionID
3046
FROM (
3147
SELECT aggregate_id, type, data,
3248
row_number() OVER (PARTITION BY aggregate_id ORDER BY seq DESC) AS rank
3349
FROM ${EventTable}
3450
WHERE type IN (
35-
${EventV2.versionedType(SessionEvent.Execution.Started.type, 1)},
36-
${EventV2.versionedType(SessionEvent.Execution.Succeeded.type, 1)},
37-
${EventV2.versionedType(SessionEvent.Execution.Failed.type, 1)},
38-
${EventV2.versionedType(SessionEvent.Execution.Interrupted.type, 1)}
51+
${started},
52+
${succeeded},
53+
${failed},
54+
${interrupted}
3955
)
4056
)
41-
WHERE rank = 1
57+
WHERE rank = 1 AND type = ${interrupted} AND json_extract(data, '$.reason') = 'shutdown'
4258
`)
43-
return latest
44-
.filter(
45-
(event) =>
46-
event.type === EventV2.versionedType(SessionEvent.Execution.Interrupted.type, 1) &&
47-
event.reason === "shutdown",
48-
)
49-
.map((event) => SessionSchema.ID.make(event.sessionID))
59+
return latest.map((event) => SessionSchema.ID.make(event.sessionID))
5060
},
5161
)
5262

@@ -128,10 +138,10 @@ const layer = Layer.effect(
128138
{ concurrency: "unbounded", discard: true },
129139
)
130140
}),
131-
"session-shutdown-recovery",
141+
`session-shutdown-recovery:${Database.path()}`,
132142
)
133143
.pipe(
134-
Effect.tapCause((cause) => Effect.logError("Failed to acquire Session recovery ownership", cause)),
144+
Effect.tapCause((cause) => Effect.logError("Failed to recover Sessions after shutdown", cause)),
135145
Effect.ignore,
136146
Effect.forkScoped,
137147
)

0 commit comments

Comments
 (0)