Skip to content

Commit 5b796c5

Browse files
committed
fix(gateway): refresh health when channel plugins change
1 parent a6a765c commit 5b796c5

2 files changed

Lines changed: 101 additions & 74 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function cachedLifecycleDiffersFromRuntime(params: {
4848
return true;
4949
}
5050
}
51-
return false;
51+
return params.cachedAccount === undefined;
5252
}
5353

5454
/** Checks whether cached channel health is stale against the live runtime snapshot. */
@@ -97,7 +97,12 @@ function cachedHealthDiffersFromRuntime(
9797
}
9898
}
9999

100-
return false;
100+
// Hot-unloaded plugins vanish from both runtime maps before cached health expires.
101+
return Object.keys(cached.channels).some(
102+
(channelId) =>
103+
!Object.hasOwn(runtime.channels, channelId) &&
104+
!Object.hasOwn(runtime.channelAccounts, channelId),
105+
);
101106
}
102107

103108
/** Merges cheap live runtime facts into a cached health summary before responding. */

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

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,80 +4912,102 @@ describe("gateway healthHandlers.health cache freshness", () => {
49124912
expect(payload?.configReload?.hotReloadStatus).toBe("disabled");
49134913
});
49144914

4915-
it("refreshes cached health when a runtime account is missing from the cached account summary", async () => {
4916-
const cached = createSingleChannelHealthSnapshot({
4917-
channelId: "discord",
4918-
label: "Discord",
4919-
running: true,
4920-
connected: true,
4921-
});
4922-
const fresh = {
4923-
...cached,
4924-
ts: cached.ts + 1,
4925-
channels: {
4926-
discord: {
4927-
...cached.channels.discord,
4928-
accounts: {
4929-
...cached.channels.discord.accounts,
4930-
work: channelHealthAccount({ accountId: "work", running: true, connected: true }),
4931-
},
4932-
},
4933-
},
4934-
};
4935-
const { respond, refreshHealthSnapshot } = await requestHealthSnapshot({
4936-
cached,
4937-
fresh,
4938-
runtimeSnapshot: {
4939-
channels: {},
4940-
channelAccounts: {
4941-
discord: { work: { accountId: "work", running: true, connected: true } },
4942-
},
4943-
},
4944-
});
4945-
4946-
expect(refreshHealthSnapshot).toHaveBeenCalledWith({
4947-
probe: false,
4948-
includeSensitive: false,
4949-
});
4950-
expect(respond).toHaveBeenCalledWith(true, fresh, undefined);
4951-
});
4915+
it.each([
4916+
{
4917+
change: "adds a running account",
4918+
previousAccountIds: ["default"],
4919+
nextAccountIds: ["default", "work"],
4920+
},
4921+
{
4922+
change: "adds an uninitialized account",
4923+
previousAccountIds: ["default"],
4924+
nextAccountIds: ["default", "work"],
4925+
uninitializedAccountId: "work",
4926+
},
4927+
{
4928+
change: "removes a runtime account",
4929+
previousAccountIds: ["default", "work"],
4930+
nextAccountIds: ["default"],
4931+
},
4932+
{
4933+
change: "removes an entire channel plugin",
4934+
previousAccountIds: ["default"],
4935+
nextAccountIds: [],
4936+
},
4937+
{
4938+
change: "re-adds an uninitialized channel plugin",
4939+
previousAccountIds: [],
4940+
nextAccountIds: ["default"],
4941+
uninitializedAccountId: "default",
4942+
},
4943+
])(
4944+
"refreshes cached health after hot reload $change",
4945+
async ({ previousAccountIds, nextAccountIds, uninitializedAccountId }) => {
4946+
const current = createSingleChannelHealthSnapshot({
4947+
channelId: "discord",
4948+
label: "Discord",
4949+
running: true,
4950+
connected: true,
4951+
});
4952+
const account = (accountId: string) =>
4953+
channelHealthAccount({ accountId, running: true, connected: true });
4954+
const summary = (accountIds: string[]) =>
4955+
accountIds.length === 0
4956+
? createHealthSnapshot({})
4957+
: {
4958+
...current,
4959+
channels: {
4960+
discord: {
4961+
...current.channels.discord,
4962+
accounts: Object.fromEntries(accountIds.map((id) => [id, account(id)])),
4963+
},
4964+
},
4965+
};
4966+
const runtime = (accountIds: string[], uninitialized?: string) => ({
4967+
channels:
4968+
previousAccountIds.length === 0 && accountIds.length > 0
4969+
? { discord: { accountId: accountIds[0] } }
4970+
: {},
4971+
channelAccounts:
4972+
accountIds.length === 0
4973+
? {}
4974+
: {
4975+
discord: Object.fromEntries(
4976+
accountIds.map((id) => [
4977+
id,
4978+
id === uninitialized ? { accountId: id } : account(id),
4979+
]),
4980+
),
4981+
},
4982+
});
4983+
const cached = summary(previousAccountIds);
4984+
const fresh = summary(nextAccountIds);
4985+
const refreshHealthSnapshot = vi
4986+
.fn()
4987+
.mockResolvedValueOnce(cached)
4988+
.mockResolvedValueOnce(fresh);
4989+
4990+
await requestHealthSnapshot({
4991+
cached,
4992+
refreshHealthSnapshot,
4993+
runtimeSnapshot: runtime(previousAccountIds),
4994+
});
4995+
expect(refreshHealthSnapshot).toHaveBeenCalledOnce();
49524996

4953-
it("refreshes cached health after hot reload removes a runtime account", async () => {
4954-
const current = createSingleChannelHealthSnapshot({
4955-
channelId: "discord",
4956-
label: "Discord",
4957-
running: true,
4958-
connected: true,
4959-
});
4960-
const cached = {
4961-
...current,
4962-
channels: {
4963-
discord: {
4964-
...current.channels.discord,
4965-
accounts: {
4966-
...current.channels.discord.accounts,
4967-
work: channelHealthAccount({ accountId: "work", running: true, connected: true }),
4968-
},
4969-
},
4970-
},
4971-
};
4972-
const { respond, refreshHealthSnapshot } = await requestHealthSnapshot({
4973-
cached,
4974-
fresh: current,
4975-
runtimeSnapshot: {
4976-
channels: {},
4977-
channelAccounts: {
4978-
discord: { default: { accountId: "default", running: true, connected: true } },
4979-
},
4980-
},
4981-
});
4997+
const { respond } = await requestHealthSnapshot({
4998+
cached,
4999+
refreshHealthSnapshot,
5000+
runtimeSnapshot: runtime(nextAccountIds, uninitializedAccountId),
5001+
});
49825002

4983-
expect(refreshHealthSnapshot).toHaveBeenCalledWith({
4984-
probe: false,
4985-
includeSensitive: false,
4986-
});
4987-
expect(respond).toHaveBeenCalledWith(true, current, undefined);
4988-
});
5003+
expect(refreshHealthSnapshot).toHaveBeenCalledTimes(2);
5004+
expect(refreshHealthSnapshot).toHaveBeenLastCalledWith({
5005+
probe: false,
5006+
includeSensitive: false,
5007+
});
5008+
expect(respond).toHaveBeenCalledWith(true, fresh, undefined);
5009+
},
5010+
);
49895011
});
49905012

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

0 commit comments

Comments
 (0)