|
Expand Up
|
@@ -17,6 +17,7 @@ import * as Effect from "effect/Effect"; |
|
|
import * as Layer from "effect/Layer"; |
|
|
import * as Option from "effect/Option"; |
|
|
import * as PubSub from "effect/PubSub"; |
|
|
import * as Semaphore from "effect/Semaphore"; |
|
|
import * as Schema from "effect/Schema"; |
|
|
import * as Stream from "effect/Stream"; |
|
|
import * as SqlClient from "effect/unstable/sql/SqlClient"; |
|
Expand Down
Expand Up
|
@@ -224,6 +225,40 @@ const baseLayer: Layer.Layer< |
|
|
); |
|
|
} |
|
|
}); |
|
|
const publishStoredEvents = (events: ReadonlyArray<OrchestrationV2StoredEvent>) => |
|
|
eventStore.publishCommitted(events).pipe(Effect.andThen(publishLiveEvents(events))); |
|
|
|
|
|
// Transactions commit one at a time, but each writer publishes after its |
|
|
// commit. If a writer is descheduled in between, a later commit reaches |
|
|
// subscribers first, and clients drop any event at or below the newest |
|
|
// sequence they have applied. So a writer takes this lane as the last step |
|
|
// of its transaction and holds it until it has published. Publishing never |
|
|
// waits, so a writer that holds the transaction while it waits for the |
|
|
// lane is not blocked for long. |
|
|
const publishLane = yield* Semaphore.make(1); |
|
|
const commitThenPublish = <A, E, R>( |
|
|
transaction: Effect.Effect<A, E, R>, |
|
|
publish: (committed: A) => Effect.Effect<void>, |
|
|
) => |
|
|
Effect.suspend(() => { |
|
|
let holdsLane = false; |
|
|
const takeLane = publishLane.take(1).pipe( |
|
|
Effect.andThen( |
|
|
Effect.sync(() => { |
|
|
holdsLane = true; |
|
|
}), |
|
|
), |
|
|
Effect.uninterruptible, |
|
|
); |
|
|
return sql |
|
|
.withTransaction(Effect.tap(transaction, () => takeLane)) |
|
|
.pipe( |
|
|
Effect.tap(publish), |
|
|
Effect.ensuring( |
|
|
Effect.suspend(() => (holdsLane ? publishLane.release(1) : Effect.void)), |
|
|
), |
|
|
); |
|
|
}); |
|
|
|
|
|
// A user can answer after terminal normalization reads the pending request. |
|
|
// Recheck inside the write transaction so stale cleanup cannot erase answers. |
|
Expand Down
Expand Up
|
@@ -332,7 +367,7 @@ const baseLayer: Layer.Layer< |
|
|
"orchestration_v2.thread_id": input.events[0]?.threadId ?? null, |
|
|
}); |
|
|
|
|
|
const storedEvents = yield* sql.withTransaction( |
|
|
return yield* commitThenPublish( |
|
|
Effect.gen(function* () { |
|
|
const normalized = yield* normalizeEvents( |
|
|
input.guardPendingUserInputCancellations === true |
|
Expand All
|
@@ -347,13 +382,14 @@ const baseLayer: Layer.Layer< |
|
|
yield* effectOutbox.enqueue(input.effects); |
|
|
return committed; |
|
|
}), |
|
|
(storedEvents) => |
|
|
Effect.gen(function* () { |
|
|
if (input.effects.length > 0) { |
|
|
yield* effectOutbox.notifyAvailable(input.effects.length); |
|
|
} |
|
|
yield* publishStoredEvents(storedEvents); |
|
|
}), |
|
|
); |
|
|
if (input.effects.length > 0) { |
|
|
yield* effectOutbox.notifyAvailable(input.effects.length); |
|
|
} |
|
|
yield* eventStore.publishCommitted(storedEvents); |
|
|
yield* publishLiveEvents(storedEvents); |
|
|
return storedEvents; |
|
|
}); |
|
|
|
|
|
const writeIfRunCurrentEffect = Effect.fn("orchestrationV2.EventSink.writeIfRunCurrent")( |
|
Expand All
|
@@ -365,7 +401,7 @@ const baseLayer: Layer.Layer< |
|
|
"orchestration_v2.thread_id": input.threadId, |
|
|
}); |
|
|
|
|
|
const result = yield* sql.withTransaction( |
|
|
return yield* commitThenPublish( |
|
|
Effect.gen(function* () { |
|
|
const rows = yield* sql<{ |
|
|
readonly status: string; |
|
Expand Down
Expand Up
|
@@ -403,12 +439,8 @@ const baseLayer: Layer.Layer< |
|
|
yield* applyStoredEvents(storedEvents); |
|
|
return { committed: true as const, storedEvents }; |
|
|
}), |
|
|
(result) => (result.committed ? publishStoredEvents(result.storedEvents) : Effect.void), |
|
|
); |
|
|
if (result.committed) { |
|
|
yield* eventStore.publishCommitted(result.storedEvents); |
|
|
yield* publishLiveEvents(result.storedEvents); |
|
|
} |
|
|
return result; |
|
|
}, |
|
|
); |
|
|
|
|
Expand All
|
@@ -424,7 +456,7 @@ const baseLayer: Layer.Layer< |
|
|
"orchestration_v2.expected_last_run_ordinal": input.expectedLastRunOrdinal, |
|
|
}); |
|
|
|
|
|
const result = yield* sql.withTransaction( |
|
|
return yield* commitThenPublish( |
|
|
Effect.gen(function* () { |
|
|
const rows = yield* sql<{ |
|
|
readonly active_attempt_id: string | null; |
|
Expand Down
Expand Up
|
@@ -464,12 +496,8 @@ const baseLayer: Layer.Layer< |
|
|
yield* applyStoredEvents(storedEvents); |
|
|
return { committed: true as const, storedEvents }; |
|
|
}), |
|
|
(result) => (result.committed ? publishStoredEvents(result.storedEvents) : Effect.void), |
|
|
); |
|
|
if (result.committed) { |
|
|
yield* eventStore.publishCommitted(result.storedEvents); |
|
|
yield* publishLiveEvents(result.storedEvents); |
|
|
} |
|
|
return result; |
|
|
}); |
|
|
|
|
|
const existingCommandResult = (commandId: CommandId) => |
|
Expand All
|
@@ -490,7 +518,7 @@ const baseLayer: Layer.Layer< |
|
|
const commitCommandEffect = Effect.fn("orchestrationV2.EventSink.commitCommand")(function* ( |
|
|
input: Parameters<EventSinkV2Shape["commitCommand"]>[0], |
|
|
) { |
|
|
const result = yield* sql.withTransaction( |
|
|
const result = yield* commitThenPublish( |
|
|
Effect.gen(function* () { |
|
|
const reserved = yield* commandReceipts.insertIfAbsent({ |
|
|
commandId: input.commandId, |
|
Expand Down
Expand Up
|
@@ -538,15 +566,15 @@ const baseLayer: Layer.Layer< |
|
|
}); |
|
|
return { receipt, storedEvents, committed: true as const, cancelledEffectIds }; |
|
|
}), |
|
|
(result) => |
|
|
Effect.gen(function* () { |
|
|
yield* effectOutbox.signalCancellations(result.cancelledEffectIds); |
|
|
if (result.committed && input.effects.length > 0) { |
|
|
yield* effectOutbox.notifyAvailable(input.effects.length); |
|
|
} |
|
|
if (result.committed) yield* publishStoredEvents(result.storedEvents); |
|
|
}), |
|
|
); |
|
|
yield* effectOutbox.signalCancellations(result.cancelledEffectIds); |
|
|
if (result.committed && input.effects.length > 0) { |
|
|
yield* effectOutbox.notifyAvailable(input.effects.length); |
|
|
} |
|
|
if (result.committed) { |
|
|
yield* eventStore.publishCommitted(result.storedEvents); |
|
|
yield* publishLiveEvents(result.storedEvents); |
|
|
} |
|
|
return { |
|
|
receipt: result.receipt, |
|
|
storedEvents: result.storedEvents, |
|
Expand Down
Expand Up
|
@@ -593,7 +621,7 @@ const baseLayer: Layer.Layer< |
|
|
|
|
|
const commitProjectCommandEffect = Effect.fn("orchestrationV2.EventSink.commitProjectCommand")( |
|
|
function* (input: Parameters<EventSinkV2Shape["commitProjectCommand"]>[0]) { |
|
|
const result = yield* sql.withTransaction( |
|
|
const result = yield* commitThenPublish( |
|
|
Effect.gen(function* () { |
|
|
const reserved: CommandReceiptStore.ProjectCommandReceiptV2 = { |
|
|
commandId: input.commandId, |
|
Expand All
|
@@ -613,10 +641,9 @@ const baseLayer: Layer.Layer< |
|
|
yield* commandReceipts.upsert(receipt); |
|
|
return { receipt, event }; |
|
|
}), |
|
|
(result) => |
|
|
result.event === undefined ? Effect.void : eventStore.publishCommitted([result.event]), |
|
|
); |
|
|
if (result.event !== undefined) { |
|
|
yield* eventStore.publishCommitted([result.event]); |
|
|
} |
|
|
return { receipt: result.receipt, committed: result.event !== undefined }; |
|
|
}, |
|
|
); |
|
Expand Down
|
|