Skip to content

Commit 7b21f61

Browse files
fix(server): keep-awake resubscribes when the run event stream fails
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5664796 commit 7b21f61

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎apps/server/src/background/KeepAwake.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,19 @@ export const holdWhileWanted = <E, R>(
7474
Stream.runDrain,
7575
);
7676

77-
// Reads and the inhibitor retry until they succeed; the cap keeps a host without an
78-
// inhibitor (e.g. no logind) from retrying and warning constantly.
77+
// Reads, the event subscription and the inhibitor retry until they succeed; the cap keeps a
78+
// host without an inhibitor (e.g. no logind) from retrying and warning constantly.
79+
const backoff = Schedule.exponential("5 seconds").pipe(
80+
Schedule.modifyDelay(({ duration }) =>
81+
Effect.succeed(Duration.min(duration, Duration.minutes(10))),
82+
),
83+
);
7984
const retryWithBackoff =
8085
(message: string) =>
8186
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
8287
effect.pipe(
8388
Effect.tapCause((cause) => Effect.logWarning(message, { cause })),
84-
Effect.retry(
85-
Schedule.exponential("5 seconds").pipe(
86-
Schedule.modifyDelay(({ duration }) =>
87-
Effect.succeed(Duration.min(duration, Duration.minutes(10))),
88-
),
89-
),
90-
),
89+
Effect.retry(backoff),
9190
);
9291

9392
class KeepAwakeInhibitorExited extends Schema.TaggedError<KeepAwakeInhibitorExited>()(
@@ -132,7 +131,14 @@ export const layer = Layer.effectDiscard(
132131
),
133132
),
134133
),
135-
).pipe(Stream.mapEffect(() => hasActiveRun));
134+
).pipe(
135+
Stream.mapEffect(() => hasActiveRun),
136+
// A failed subscription resubscribes, which re-reads the cursor and the snapshot.
137+
Stream.tapCause((cause) =>
138+
Effect.logWarning("keep-awake run event stream failed", { cause }),
139+
),
140+
Stream.retry(backoff),
141+
);
136142
const enabled = Stream.unwrap(
137143
Effect.gen(function* () {
138144
const changes = yield* settings.subscribeChanges;

0 commit comments

Comments
 (0)