fix(server): retry interrupted Git status refreshes

This commit is contained in:
Ben Davis committed 2026-06-26 11:30:42 -07:00
1 parent b9197b7e4c
commit 96afe3f821
2 files changed
+75 -4

No files matched your search

+7 -4
View File
@@ -1,5 +1,6 @@
import * as Arr from "effect/Array";
import * as Cache from "effect/Cache";
import * as Cause from "effect/Cause";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
@@ -75,6 +76,11 @@ const NON_REPOSITORY_STATUS_DETAILS = Object.freeze<GitVcsDriver.GitStatusDetail
behindCount: 0,
aheadOfDefaultCount: 0,
});
export const statusUpstreamRefreshCacheTimeToLive = <A, E>(exit: Exit.Exit<A, E>) => {
if (Exit.isSuccess(exit)) return STATUS_UPSTREAM_REFRESH_INTERVAL;
return Cause.hasInterrupts(exit.cause) ? Duration.zero : STATUS_UPSTREAM_REFRESH_FAILURE_COOLDOWN;
};
const NON_REPOSITORY_REMOTE_STATUS_DETAILS = Object.freeze<GitVcsDriver.GitRemoteStatusDetails>({
isRepo: false,
isDefaultBranch: false,
@@ -1046,10 +1052,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const statusRemoteRefreshCache = yield* Cache.makeWith(refreshStatusRemoteCacheEntry, {
capacity: STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY,
// Keep successful refreshes warm and briefly back off failed refreshes to avoid retry storms.
timeToLive: (exit) =>
Exit.isSuccess(exit)
? STATUS_UPSTREAM_REFRESH_INTERVAL
: STATUS_UPSTREAM_REFRESH_FAILURE_COOLDOWN,
timeToLive: statusUpstreamRefreshCacheTimeToLive,
});
const refreshStatusUpstreamIfStale = Effect.fn("refreshStatusUpstreamIfStale")(function* (
@@ -1,5 +1,6 @@
import { assert, it, describe } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Cache from "effect/Cache";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
@@ -21,6 +22,7 @@ import type {
} from "@t3tools/contracts";
import { GitManagerError } from "@t3tools/contracts";
import { statusUpstreamRefreshCacheTimeToLive } from "./GitVcsDriverCore.ts";
import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
@@ -797,6 +799,72 @@ describe("VcsStatusBroadcaster", () => {
}).pipe(Effect.provide(makeTestLayer(state)));
});
it.effect("restarts an interrupted refresh after a subscriber reconnects", () =>
Effect.gen(function* () {
const firstRefreshStarted = yield* Deferred.make<void>();
const secondRefreshStarted = yield* Deferred.make<void>();
const secondRemoteUpdated = yield* Deferred.make<void>();
let refreshLookups = 0;
const refreshCache = yield* Cache.makeWith(
() =>
Effect.gen(function* () {
refreshLookups += 1;
if (refreshLookups === 1) {
yield* Deferred.succeed(firstRefreshStarted, undefined);
return yield* Effect.never;
}
yield* Deferred.succeed(secondRefreshStarted, undefined);
}),
{
capacity: 1,
timeToLive: statusUpstreamRefreshCacheTimeToLive,
},
);
const testLayer = VcsStatusBroadcaster.layer.pipe(
Layer.provideMerge(NodeServices.layer),
Layer.provide(
Layer.mock(GitWorkflowService.GitWorkflowService)({
localStatus: () => Effect.succeed(baseLocalStatus),
refreshStatusUpstream: () => Cache.get(refreshCache, "repo"),
remoteStatus: () => Effect.succeed(baseRemoteStatus),
invalidateRemoteStatus: () => Effect.void,
} satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>),
),
);
yield* Effect.gen(function* () {
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
const firstScope = yield* Scope.make();
yield* broadcaster
.streamStatus(
{ cwd: "/repo" },
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.hours(1)) },
)
.pipe(Stream.runDrain, Effect.forkIn(firstScope));
yield* Deferred.await(firstRefreshStarted).pipe(Effect.timeout("1 second"));
yield* Scope.close(firstScope, Exit.void);
const secondScope = yield* Scope.make();
yield* Stream.runForEach(
broadcaster.streamStatus(
{ cwd: "/repo" },
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.hours(1)) },
),
(event) =>
event._tag === "remoteUpdated"
? Deferred.succeed(secondRemoteUpdated, undefined).pipe(Effect.ignore)
: Effect.void,
).pipe(Effect.forkIn(secondScope));
yield* Deferred.await(secondRefreshStarted).pipe(Effect.timeout("1 second"));
yield* Deferred.await(secondRemoteUpdated).pipe(Effect.timeout("1 second"));
assert.equal(refreshLookups, 2);
yield* Scope.close(secondScope, Exit.void);
}).pipe(Effect.provide(testLayer), TestClock.withLive);
}),
);
it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
const state = {
currentLocalStatus: baseLocalStatus,