Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Exit on disabled database #245

Merged
merged 7 commits into from
May 7, 2024
Merged
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
26 changes: 22 additions & 4 deletions packages/sdk-core/src/modules/database/BacktraceDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AbortController, anySignal } from '../../common/AbortController';
import { IdGenerator } from '../../common/IdGenerator';
import { TimeHelper } from '../../common/TimeHelper';
import { BacktraceAttachment } from '../../model/attachment';
Expand All @@ -18,6 +19,12 @@ export class BacktraceDatabase implements BacktraceModule {
return this._enabled;
}

/**
* Abort controller to cancel asynchronous database operations when
* the database is being disabled by the user.
*/
private readonly _abortController = new AbortController();

private readonly _databaseRecordContext: BacktraceDatabaseContext;
private readonly _storageProviders: BacktraceDatabaseStorageProvider[] = [];

Expand Down Expand Up @@ -158,6 +165,7 @@ export class BacktraceDatabase implements BacktraceModule {
public dispose() {
this._enabled = false;
clearInterval(this._intervalId);
this._abortController.abort();
}

/**
Expand Down Expand Up @@ -188,30 +196,40 @@ export class BacktraceDatabase implements BacktraceModule {
/**
* Sends all records available in the database to Backtrace and removes them
* no matter if the submission process was successful or not.
* @param abortSignal optional abort signal to cancel sending requests
*/
public async flush() {
public async flush(abortSignal?: AbortSignal) {
const start = TimeHelper.now();
await this.send();
await this.send(abortSignal);
const records = this.get().filter((n) => n.timestamp <= start);
for (const record of records) {
if (abortSignal?.aborted) {
return;
}
this.remove(record);
}
}

/**
* Sends all records available in the database to Backtrace.
* @param abortSignal optional abort signal to cancel sending requests
*/
public async send() {
public async send(abortSignal?: AbortSignal) {
for (let bucketIndex = 0; bucketIndex < this._databaseRecordContext.bucketCount; bucketIndex++) {
// make a copy of records to not update the array after each remove
const records = [...this._databaseRecordContext.getBucket(bucketIndex)];
const signal = anySignal(abortSignal, this._abortController.signal);

for (const record of records) {
if (!this.enabled) {
return;
}
if (record.locked) {
continue;
}
try {
record.locked = true;
const result = await this._requestHandler.send(record.data, record.attachments);
const result = await this._requestHandler.send(record.data, record.attachments, signal);
if (result.status === 'Ok') {
this.remove(record);
continue;
Expand Down
Loading