Skip to content

Commit 526278e

Browse files
committed
fix(server): scheduled run outcomes subscribe before the server is activated
The listener that records scheduled run outcomes was parked until server activation. Routes already serve before activation, so a Run now could dispatch a run whose turn ended before the listener subscribed. The domain-event stream is a live tail from its subscription cursor, so that outcome was never seen and the task stayed 'running' until its next run or a restart. The listener now starts while the service builds, before any route or the scheduler can dispatch. It keeps the live tail rather than the stored-event stream, which would replay the whole event history on every boot.
1 parent 3436e14 commit 526278e

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎apps/server/src/scheduledTasks/ScheduledTaskService.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as SqlClient from "effect/sql/SqlClient";
1919
import * as Deferred from "effect/Deferred";
2020
import * as Fiber from "effect/Fiber";
2121
import * as Option from "effect/Option";
22+
import * as PubSub from "effect/PubSub";
2223
import * as Queue from "effect/Queue";
2324
import * as Ref from "effect/Ref";
2425
import * as Schema from "effect/Schema";
@@ -28,6 +29,7 @@ import * as TestClock from "effect/testing/TestClock";
2829
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
2930
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
3031
import * as SecretRequests from "../secrets/SecretRequests.ts";
32+
import * as ServerActivation from "../serverActivation.ts";
3133
import * as SqlitePersistence from "../persistence/Sqlite.ts";
3234
import * as ScheduledTaskService from "./ScheduledTaskService.ts";
3335

@@ -721,3 +723,44 @@ it.effect("drops an earlier run's outcome once a newer run of the task starts",
721723
}),
722724
),
723725
);
726+
727+
it.effect("records the outcome of a run dispatched before the server is activated", () =>
728+
Effect.gen(function* () {
729+
const sql = yield* SqlClient.SqlClient;
730+
yield* insertRow(
731+
sql,
732+
{ id: "bound", next: null, enabled: 1, status: "never", threadId: "thread:bound" },
733+
"2026-09-09T12:00:00.000Z",
734+
);
735+
// A live tail, like streamDomainEvents: events published before the
736+
// listener subscribes are never delivered to it.
737+
const events = yield* PubSub.unbounded<OrchestrationV2DomainEvent>();
738+
const dispatched = yield* Queue.unbounded<MessageId>();
739+
// Routes serve before activation; this server is never activated.
740+
const activation = yield* Deferred.make<void>();
741+
const layer = ScheduledTaskService.layer.pipe(
742+
Layer.provide(
743+
Layer.mergeAll(
744+
Layer.mock(ThreadLaunchService.ThreadLaunchService)({}),
745+
Layer.mock(ThreadManagementService.ThreadManagementService)({
746+
sendToThread: (input) =>
747+
Queue.offer(dispatched, input.messageId).pipe(Effect.as({} as never)),
748+
streamDomainEvents: Stream.fromPubSub(events),
749+
}),
750+
Layer.mock(SecretRequests.SecretRequests)({}),
751+
NodeCrypto.layer,
752+
Scheduler.layer,
753+
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
754+
),
755+
),
756+
);
757+
yield* Effect.gen(function* () {
758+
const service = yield* ScheduledTaskService.ScheduledTaskService;
759+
yield* service.runNow({ id: ScheduledTaskId.make("bound") });
760+
yield* PubSub.publish(events, runEvent(yield* Queue.take(dispatched), "failed"));
761+
const task = yield* settledTask(service, "bound");
762+
assert.equal(task.lastRunStatus, "failed");
763+
assert.equal(task.lastRunError, "The run failed.");
764+
}).pipe(Effect.provide(layer));
765+
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
766+
);

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import * as Metrics from "../observability/Metrics.ts";
4545
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
4646
import * as SecretRequests from "../secrets/SecretRequests.ts";
4747
import * as Scheduler from "../scheduling/Scheduler.ts";
48-
import { forkParked } from "../serverActivation.ts";
4948
import { makeRunOutcomes, type RunOutcome } from "./RunOutcomes.ts";
5049
import { isMissedFixedTimeRun, isSameSchedule, nextScheduledRunAt } from "./Schedule.ts";
5150
import {
@@ -722,15 +721,19 @@ export const layer = Layer.effect(
722721
}),
723722
),
724723
);
725-
yield* forkParked(
726-
Stream.runForEach(threadManagement.streamDomainEvents, (event) => {
727-
const outcome = runOutcomes.observe(event);
728-
return outcome === null ? Effect.void : recordRunOutcome(outcome);
729-
}).pipe(
730-
Effect.catchCause((cause) =>
731-
Effect.logWarning("Scheduled run outcome stream failed", { cause }),
732-
),
724+
// Subscribed while the service builds, not parked until activation:
725+
// routes serve (and runNow can dispatch) before activation opens, and the
726+
// live tail only delivers events after its subscription cursor. The
727+
// replaying stored-event stream would close that gap too, but it reads the
728+
// whole event history on every boot.
729+
yield* Stream.runForEach(threadManagement.streamDomainEvents, (event) => {
730+
const outcome = runOutcomes.observe(event);
731+
return outcome === null ? Effect.void : recordRunOutcome(outcome);
732+
}).pipe(
733+
Effect.catchCause((cause) =>
734+
Effect.logWarning("Scheduled run outcome stream failed", { cause }),
733735
),
736+
Effect.forkScoped({ startImmediately: true }),
734737
);
735738

736739
// Best-effort escape hatch: if anything fails between markRunning and

0 commit comments

Comments
 (0)