Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions apps/server/src/usage/HeadlessUsageRefresher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,50 @@ describe("HeadlessUsageRefresher", () => {
}),
);

it.effect("a healthy skip ends a failure streak: the next outage is logged, short retry", () =>
Effect.gen(function* () {
yield* startAt(NOW_MS);
const opencode = { driver: ProviderDriverKind.make("opencode") };
let healthy = false;
const fake = fakeTarget(provider(NOW_MS - HOUR, opencode), {
checksOnInterval: () => !healthy,
});
fake.setOutcome("noReading");
const log = logCollector();
const { sweep } = yield* make(deps([fake.target]));
const tick = (ms: number) =>
TestClock.adjust(ms).pipe(Effect.andThen(sweep), Effect.provide(log.layer));
// Three failures in a row: one warning.
yield* tick(0);
yield* tick(2 * INTERVAL);
yield* tick(4 * INTERVAL);
assert.equal(fake.calls(), 3);
assert.equal(log.warnings.length, 1);
// A client recovers usage; the instance is healthy, so it is skipped for days.
healthy = true;
const recoveredAt = NOW_MS + 6 * INTERVAL + MINUTE;
yield* TestClock.setTime(recoveredAt);
fake.setSnapshot(provider(recoveredAt, opencode));
yield* tick(0);
assert.equal(fake.calls(), 3);
assert.deepEqual(log.infos, ["headless usage refresh works again"]);
yield* tick(3 * 24 * HOUR);
assert.equal(fake.calls(), 3);
// A new outage: retried at once, logged as a new streak, retried after 2 intervals.
healthy = false;
yield* tick(0);
assert.equal(fake.calls(), 4);
assert.equal(log.warnings.length, 2);
yield* tick(2 * INTERVAL);
assert.equal(fake.calls(), 5);
// Skipped again while healthy: the logged end is not repeated.
healthy = true;
yield* tick(0);
yield* tick(0);
assert.equal(log.infos.length, 2);
}),
);

it.effect(
"leaves the refresh to upstream while a client lease holds, and pauses on a constrained host",
() =>
Expand All @@ -383,6 +427,28 @@ describe("HeadlessUsageRefresher", () => {
}),
);

it.effect("a host that locks during a sweep stops the rest of that sweep", () =>
Effect.gen(function* () {
yield* startAt(NOW_MS);
const first = fakeTarget(provider(NOW_MS - HOUR));
const second = fakeTarget(provider(NOW_MS - HOUR, {}, SECOND));
// The host locks while the first refresh runs, and unlocks later.
let locked = () => first.calls() > 0;
const { sweep } = yield* make(
deps([first.target, second.target], {
hostConstrained: () => Effect.sync(() => locked()),
}),
);
yield* sweep;
assert.equal(first.calls(), 1);
assert.equal(second.calls(), 0);
locked = () => false;
yield* sweep;
assert.equal(first.calls(), 1);
assert.equal(second.calls(), 1);
}),
);

it.effect("off switch: interval 0 refreshes nothing", () =>
Effect.gen(function* () {
yield* startAt(NOW_MS);
Expand Down
27 changes: 18 additions & 9 deletions apps/server/src/usage/HeadlessUsageRefresher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re
const failing = new Map<ProviderInstanceId, FailureState>();
const permit = yield* Semaphore.make(1);

const endStreak = Effect.fn("HeadlessUsageRefresher.endStreak")(function* (
instanceId: ProviderInstanceId,
) {
const previous = failing.get(instanceId);
if (previous === undefined) return;
failing.delete(instanceId);
yield* Effect.logInfo("headless usage refresh works again", {
instanceId,
failures: previous.failures,
});
});

const refreshOne = Effect.fn("HeadlessUsageRefresher.refreshOne")(function* (
target: RefreshTarget,
refreshUsage: NonNullable<RefreshTarget["snapshot"]["refreshUsage"]>,
Expand All @@ -131,13 +143,7 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re
const ok = Exit.isSuccess(exit) && !timedOut && gotNewReading(before, after);
const previous = failing.get(target.instanceId);
if (ok) {
if (previous !== undefined) {
failing.delete(target.instanceId);
yield* Effect.logInfo("headless usage refresh works again", {
instanceId: target.instanceId,
failures: previous.failures,
});
}
yield* endStreak(target.instanceId);
return;
}
const failures = (previous?.failures ?? 0) + 1;
Expand All @@ -162,7 +168,6 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re
if (Option.isNone(settings)) return;
const intervalMs = effectiveIntervalMs(settings.value);
if (intervalMs === null) return;
if (yield* deps.hostConstrained(settings.value)) return;
const targets = yield* deps.listTargets;
const live = new Set(targets.map((target) => target.instanceId));
for (const id of failing.keys()) if (!live.has(id)) failing.delete(id);
Expand All @@ -173,8 +178,10 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re
continue;
}
// Upstream's loop skips some healthy instances: a check of a healthy OpenCode one
// starts an OpenCode server. Skip them too.
// starts an OpenCode server. Skip them too; healthy also ends a failure streak, so the
// next outage is logged and retried as a new one.
if (target.snapshot.checksOnInterval && !(yield* target.snapshot.checksOnInterval)) {
yield* endStreak(target.instanceId);
continue;
}
const before = yield* target.snapshot.getSnapshot;
Expand All @@ -190,6 +197,8 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re
) {
continue;
}
// Read per refresh: the host may lock while an earlier refresh of this sweep runs.
if (yield* deps.hostConstrained(settings.value)) return;
if (yield* deps.clientRefreshes(target.instanceId)) continue;
yield* refreshOne(target, refreshUsage, before, intervalMs, nowMs);
}
Expand Down
Loading