Skip to content

Commit bdc595d

Browse files
authored
fix(gateway): refresh health after account removal (#122620)
1 parent beb576c commit bdc595d

2 files changed

Lines changed: 44 additions & 15 deletions

File tree

‎src/gateway/server-methods/health.ts‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@ function shouldScheduleRequestRefresh(
3333
return true;
3434
}
3535

36-
function cachedAccountForRuntimeSnapshot(params: {
37-
cachedChannel: ChannelHealthSummary | undefined;
38-
accountId: string | undefined;
39-
}): ChannelHealthSummary | undefined {
40-
const accountId = params.accountId;
41-
if (accountId && params.cachedChannel?.accounts?.[accountId]) {
42-
return params.cachedChannel.accounts[accountId];
43-
}
44-
return undefined;
45-
}
46-
4736
function cachedLifecycleDiffersFromRuntime(params: {
4837
cachedAccount: ChannelHealthSummary | undefined;
4938
runtimeSnapshot: ChannelAccountSnapshot;
@@ -82,16 +71,19 @@ function cachedHealthDiffersFromRuntime(
8271
continue;
8372
}
8473
const cachedChannel = cached.channels[channelId];
74+
const cachedAccounts = cachedChannel?.accounts;
75+
if (
76+
Object.keys(cachedAccounts ?? {}).some((accountId) => !Object.hasOwn(accounts, accountId))
77+
) {
78+
return true;
79+
}
8580
for (const [accountId, runtimeSnapshot] of Object.entries(accounts)) {
8681
if (!runtimeSnapshot) {
8782
continue;
8883
}
8984
if (
9085
cachedLifecycleDiffersFromRuntime({
91-
cachedAccount: cachedAccountForRuntimeSnapshot({
92-
cachedChannel,
93-
accountId,
94-
}),
86+
cachedAccount: cachedAccounts?.[accountId],
9587
runtimeSnapshot,
9688
})
9789
) {

‎src/gateway/server-methods/server-methods.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,6 +4839,43 @@ describe("gateway healthHandlers.health cache freshness", () => {
48394839
});
48404840
expect(respond).toHaveBeenCalledWith(true, fresh, undefined);
48414841
});
4842+
4843+
it("refreshes cached health after hot reload removes a runtime account", async () => {
4844+
const current = createSingleChannelHealthSnapshot({
4845+
channelId: "discord",
4846+
label: "Discord",
4847+
running: true,
4848+
connected: true,
4849+
});
4850+
const cached = {
4851+
...current,
4852+
channels: {
4853+
discord: {
4854+
...current.channels.discord,
4855+
accounts: {
4856+
...current.channels.discord.accounts,
4857+
work: channelHealthAccount({ accountId: "work", running: true, connected: true }),
4858+
},
4859+
},
4860+
},
4861+
};
4862+
const { respond, refreshHealthSnapshot } = await requestHealthSnapshot({
4863+
cached,
4864+
fresh: current,
4865+
runtimeSnapshot: {
4866+
channels: {},
4867+
channelAccounts: {
4868+
discord: { default: { accountId: "default", running: true, connected: true } },
4869+
},
4870+
},
4871+
});
4872+
4873+
expect(refreshHealthSnapshot).toHaveBeenCalledWith({
4874+
probe: false,
4875+
includeSensitive: false,
4876+
});
4877+
expect(respond).toHaveBeenCalledWith(true, current, undefined);
4878+
});
48424879
});
48434880

48444881
describe("logs.tail", () => {

0 commit comments

Comments
 (0)