Skip to content

Commit e66c52d

Browse files
committed
Remove flush() in favor of processQueue(); stop heartbeats inside startHeartbeats
1 parent 4e9b490 commit e66c52d

6 files changed

Lines changed: 24 additions & 44 deletions

File tree

‎.agents/skills/exceptionless-javascript/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Read only the references needed for the request. Start with the runtime for setu
3838
- `markAsCritical()` marks the event critical; `markAsCritical(false)` leaves tags unchanged.
3939
- `config.serverUrl` also sets `configServerUrl` and `heartbeatServerUrl`; assign custom endpoint overrides after setting `serverUrl`.
4040
- Use lowercase log levels in new snippets: `"trace"`, `"debug"`, `"info"`, `"warn"`, `"error"`, `"fatal"`, `"off"`.
41-
- For short-lived Node/serverless work, call `await Exceptionless.flush()` (waits up to 10 seconds by default, returns `false` on timeout) or `await Exceptionless.processQueue()` after critical submissions.
41+
- For short-lived Node/serverless work, call `await Exceptionless.processQueue()` after critical submissions. If a send is already in progress, it waits for that send to finish.
4242

4343
## Integrator Guidance
4444

‎.agents/skills/exceptionless-javascript/references/troubleshooting.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ Events are queued and sent in the background. If the app exits or navigates imme
2525
```js
2626
import { Exceptionless } from "@exceptionless/browser";
2727

28-
// Waits at most 10 seconds by default; returns false if the queue didn't finish in time.
29-
await Exceptionless.flush();
28+
await Exceptionless.processQueue();
3029
```
3130

3231
This matters most for Node scripts, CLIs, serverless handlers, route handlers, tests, and hard redirects.

‎packages/core/src/ExceptionlessClient.ts‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,6 @@ export class ExceptionlessClient {
9292
await this.config.services.queue.process();
9393
}
9494

95-
/**
96-
* Sends queued events, waiting at most `timeoutMs`. Use it before a short-lived process,
97-
* serverless handler, or test exits.
98-
* @returns true when the queue finished processing within the timeout.
99-
*/
100-
public async flush(timeoutMs: number = 10000): Promise<boolean> {
101-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
102-
const timeout = new Promise<boolean>((resolve) => {
103-
timeoutId = setTimeout(() => resolve(false), timeoutMs);
104-
allowProcessToExitWithoutWaitingForTimerOrInterval(timeoutId);
105-
});
106-
107-
try {
108-
return await Promise.race([this.processQueue().then(() => true), timeout]);
109-
} finally {
110-
clearTimeout(timeoutId);
111-
}
112-
}
113-
11495
private updateSettingsTimer(startingUp: boolean = false) {
11596
this.suspendSettingsTimer();
11697

‎packages/core/src/plugins/default/HeartbeatPlugin.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export class HeartbeatPlugin implements IEventPlugin {
1717
}
1818

1919
public startup(context: PluginContext): Promise<void> {
20-
clearInterval(this._intervalId);
21-
this._intervalId = undefined;
20+
this.stopHeartbeats();
2221

2322
// Resume heartbeats for the current session (for example, when a hidden tab becomes visible again).
2423
const { client } = context;
@@ -30,8 +29,7 @@ export class HeartbeatPlugin implements IEventPlugin {
3029
}
3130

3231
public suspend(): Promise<void> {
33-
clearInterval(this._intervalId);
34-
this._intervalId = undefined;
32+
this.stopHeartbeats();
3533
return Promise.resolve();
3634
}
3735

@@ -40,8 +38,7 @@ export class HeartbeatPlugin implements IEventPlugin {
4038
return Promise.resolve();
4139
}
4240

43-
clearInterval(this._intervalId);
44-
this._intervalId = undefined;
41+
this.stopHeartbeats();
4542

4643
const { config } = context.client;
4744
if (!config.currentSessionIdentifier) {
@@ -61,8 +58,15 @@ export class HeartbeatPlugin implements IEventPlugin {
6158
}
6259

6360
private startHeartbeats(client: ExceptionlessClient): void {
61+
this.stopHeartbeats();
62+
6463
// Heartbeats always go to the session that is current when they fire.
6564
this._intervalId = setInterval(() => void client.submitSessionHeartbeat(), this._interval);
6665
allowProcessToExitWithoutWaitingForTimerOrInterval(this._intervalId);
6766
}
67+
68+
private stopHeartbeats(): void {
69+
clearInterval(this._intervalId);
70+
this._intervalId = undefined;
71+
}
6872
}

‎packages/core/src/queue/DefaultEventQueue.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class DefaultEventQueue implements IEventQueue {
9696
const { log } = this.config.services;
9797

9898
if (this._processing) {
99-
// Callers such as flush() need to wait for the run that is already sending events. An onEventsPosted
99+
// Callers such as processQueue() need to wait for the run that is already sending events. An onEventsPosted
100100
// handler calling back into the queue is part of that run, so it must not wait for it.
101101
return this._notifyingHandlers ? Promise.resolve() : this._processing;
102102
}

‎packages/core/test/ExceptionlessClientLifecycle.test.ts‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,14 @@ describe("ExceptionlessClient lifecycle", () => {
164164
expect(sessions[0].reference_id).not.toBe(sessions[1].reference_id);
165165
});
166166

167-
test("flush reports whether the queue finished in time", async () => {
168-
const { client } = createClient();
169-
client.config.apiKey = "UNIT_TEST_API_KEY";
170-
171-
expect(await client.flush(1000)).toBe(true);
172-
173-
vi.spyOn(client.config.services.queue, "process").mockReturnValue(new Promise(() => {}));
174-
expect(await client.flush(10)).toBe(false);
175-
});
176-
177-
test("flush waits for a submission that is already in flight", async () => {
167+
test("processQueue waits for a submission that is already in flight", async () => {
178168
const client = new ExceptionlessClient();
179169
client.config.apiKey = "UNIT_TEST_API_KEY";
180170
client.config.updateSettingsWhenIdleInterval = -1;
181171
const submissionClient = new StubSubmissionClient();
182172
client.config.services.submissionClient = submissionClient;
183173
let finishSubmission: () => void = () => {};
184-
vi.spyOn(submissionClient, "submitEvents").mockReturnValueOnce(
174+
const submitEvents = vi.spyOn(submissionClient, "submitEvents").mockReturnValueOnce(
185175
new Promise((resolve) => {
186176
finishSubmission = () => resolve(new Response(202, "", NaN, NaN, undefined));
187177
})
@@ -192,11 +182,17 @@ describe("ExceptionlessClient lifecycle", () => {
192182
});
193183

194184
const backgroundRun = client.processQueue();
195-
expect(await client.flush(10)).toBe(false);
185+
let finished = false;
186+
const secondRun = client.processQueue().then(() => {
187+
finished = true;
188+
});
189+
await new Promise((resolve) => setTimeout(resolve, 10));
190+
expect(finished).toBe(false);
196191

197192
finishSubmission();
198-
await backgroundRun;
199-
expect(await client.flush(1000)).toBe(true);
193+
await Promise.all([backgroundRun, secondRun]);
194+
expect(finished).toBe(true);
195+
expect(submitEvents).toHaveBeenCalledTimes(1);
200196
});
201197
});
202198

0 commit comments

Comments
 (0)