Skip to content

Commit

Permalink
Pds v2 ensure sqlite ready (#1918)
Browse files Browse the repository at this point in the history
ensure sqlite is ready before making queries
  • Loading branch information
dholms authored Dec 4, 2023
1 parent ff50319 commit 93363a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/pds/src/actor-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ export class ActorStore {
throw new InvalidRequestError('Repo not found', 'NotFound')
}

return getDb(dbLocation, this.cfg.disableWalAutoCheckpoint)
const db = getDb(dbLocation, this.cfg.disableWalAutoCheckpoint)
try {
await db.ensureReady()
} catch (err) {
db.close()
throw err
}
return db
}

async read<T>(did: string, fn: ActorStoreReadFn<T>) {
Expand Down
9 changes: 8 additions & 1 deletion packages/pds/src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { dbLogger } from '../logger'
import { retrySqlite } from './util'

const DEFAULT_PRAGMAS = {
strict: 'ON', // @TODO strictness should live on table defs instead
// strict: 'ON', // @TODO strictness should live on table defs instead
}

export class Database<Schema> {
Expand Down Expand Up @@ -50,6 +50,13 @@ export class Database<Schema> {
await sql`PRAGMA journal_mode = WAL`.execute(this.db)
}

// run a simple select with retry logic to ensure the db is ready (not in wal recovery mode)
async ensureReady() {
await retrySqlite(async () => {
await sql`select 1`.execute(this.db)
})
}

async transactionNoRetry<T>(
fn: (db: Database<Schema>) => Promise<T>,
): Promise<T> {
Expand Down

0 comments on commit 93363a2

Please sign in to comment.