Skip to content

Commit 9336338

Browse files
authored
Exit on disabled database (#245)
* Exit on disabled database * Add abortsignlar support to the send method * Abort controller comment * Use abort signal in flush/send method * Return instead of throwIfAborted * Abort signal comment in the database definition * Verify abort signal in the loop
1 parent 89357c7 commit 9336338

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

packages/sdk-core/src/modules/database/BacktraceDatabase.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AbortController, anySignal } from '../../common/AbortController';
12
import { IdGenerator } from '../../common/IdGenerator';
23
import { TimeHelper } from '../../common/TimeHelper';
34
import { BacktraceAttachment } from '../../model/attachment';
@@ -18,6 +19,12 @@ export class BacktraceDatabase implements BacktraceModule {
1819
return this._enabled;
1920
}
2021

22+
/**
23+
* Abort controller to cancel asynchronous database operations when
24+
* the database is being disabled by the user.
25+
*/
26+
private readonly _abortController = new AbortController();
27+
2128
private readonly _databaseRecordContext: BacktraceDatabaseContext;
2229
private readonly _storageProviders: BacktraceDatabaseStorageProvider[] = [];
2330

@@ -158,6 +165,7 @@ export class BacktraceDatabase implements BacktraceModule {
158165
public dispose() {
159166
this._enabled = false;
160167
clearInterval(this._intervalId);
168+
this._abortController.abort();
161169
}
162170

163171
/**
@@ -188,30 +196,40 @@ export class BacktraceDatabase implements BacktraceModule {
188196
/**
189197
* Sends all records available in the database to Backtrace and removes them
190198
* no matter if the submission process was successful or not.
199+
* @param abortSignal optional abort signal to cancel sending requests
191200
*/
192-
public async flush() {
201+
public async flush(abortSignal?: AbortSignal) {
193202
const start = TimeHelper.now();
194-
await this.send();
203+
await this.send(abortSignal);
195204
const records = this.get().filter((n) => n.timestamp <= start);
196205
for (const record of records) {
206+
if (abortSignal?.aborted) {
207+
return;
208+
}
197209
this.remove(record);
198210
}
199211
}
212+
200213
/**
201214
* Sends all records available in the database to Backtrace.
215+
* @param abortSignal optional abort signal to cancel sending requests
202216
*/
203-
public async send() {
217+
public async send(abortSignal?: AbortSignal) {
204218
for (let bucketIndex = 0; bucketIndex < this._databaseRecordContext.bucketCount; bucketIndex++) {
205219
// make a copy of records to not update the array after each remove
206220
const records = [...this._databaseRecordContext.getBucket(bucketIndex)];
221+
const signal = anySignal(abortSignal, this._abortController.signal);
207222

208223
for (const record of records) {
224+
if (!this.enabled) {
225+
return;
226+
}
209227
if (record.locked) {
210228
continue;
211229
}
212230
try {
213231
record.locked = true;
214-
const result = await this._requestHandler.send(record.data, record.attachments);
232+
const result = await this._requestHandler.send(record.data, record.attachments, signal);
215233
if (result.status === 'Ok') {
216234
this.remove(record);
217235
continue;

0 commit comments

Comments
 (0)