Skip to content

Commit

Permalink
feat: implement database connection watcher (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron authored May 5, 2024
1 parent 74843e6 commit 2752012
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-socks-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

fix: database not recovering from error/closed state
70 changes: 64 additions & 6 deletions packages/app/src/systems/Core/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import type {
NetworkData,
Vault,
} from '@fuel-wallet/types';
import type { Table } from 'dexie';
import type { DbEvents, PromiseExtended, Table } from 'dexie';
import Dexie from 'dexie';
import 'dexie-observable';
import { DATABASE_VERSION, VITE_FUEL_PROVIDER_URL } from '~/config';
import type { Transaction } from '~/systems/Transaction/types';

type FailureEvents = Extract<keyof DbEvents, 'close' | 'blocked'>;

export class FuelDB extends Dexie {
vaults!: Table<Vault, string>;
accounts!: Table<Account, string>;
Expand All @@ -24,6 +26,8 @@ export class FuelDB extends Dexie {
assets!: Table<AssetData, string>;
abis!: Table<AbiTable, string>;
errors!: Table<FuelWalletError, string>;
integrityCheckInterval?: NodeJS.Timeout;
restartAttempts = 0;
readonly alwaysOpen = true;

constructor() {
Expand Down Expand Up @@ -51,16 +55,70 @@ export class FuelDB extends Dexie {
id: createUUID(),
});
});
this.on('blocked', () => this.restart('closed'));
this.on('close', () => this.restart('blocked'));
this.on('blocked', () => this.restart('blocked'));
this.on('close', () => this.restart('close'));
this.on('message', (e) => {
console.log('fsk changed', e);
});
}

open() {
return this.safeOpen().finally(() =>
this.watchConnection()
) as PromiseExtended<Dexie>;
}

close(safeClose = false) {
if (safeClose) {
this.restartAttempts = 0;
clearInterval(this.integrityCheckInterval);
}
return super.close();
}

async safeOpen() {
try {
const result = await super.open();
this.restartAttempts = 0;
return result;
} catch (err) {
console.error('Failed to restart DB. Sending signal for restart');
this.restart('blocked');
throw err;
}
}

async ensureDatabaseOpen() {
if (this.isOpen() && !this.hasBeenClosed() && !this.hasFailed()) return;

if (this.restartAttempts > 3) {
console.error('Reached max attempts to open DB. Sending restart signal.');
this.restart('blocked');
return;
}

this.restartAttempts += 1;
console.warn('DB is not open. Attempting restart.');
await this.safeOpen();
}

watchConnection() {
if (!this.alwaysOpen) return;

clearInterval(this.integrityCheckInterval);
this.integrityCheckInterval = setInterval(() => {
this.ensureDatabaseOpen();
}, 1000);
}

async restart(eventName: 'blocked' | 'closed') {
async restart(eventName: FailureEvents) {
if (!this.alwaysOpen) {
return;
}
if (eventName !== 'closed') {
this.close();
if (eventName === 'close') {
clearInterval(this.integrityCheckInterval);
} else {
this.close(true);
}

this.open();
Expand Down

0 comments on commit 2752012

Please sign in to comment.