34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { createRequire } from "node:module";
|
|
|
|
import type BetterSqlite3 from "better-sqlite3";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const Database = require("better-sqlite3") as typeof BetterSqlite3;
|
|
|
|
export class WorkerStorageStatus {
|
|
private readonly database: BetterSqlite3.Database;
|
|
|
|
constructor(databasePath: string) {
|
|
const nativeBinding = process.env.DADA_SQLITE_NATIVE_BINDING;
|
|
this.database = new Database(databasePath, nativeBinding ? { nativeBinding } : undefined);
|
|
this.database.pragma("busy_timeout = 5000");
|
|
}
|
|
|
|
getStatus(): "active" | "full" | "unavailable" {
|
|
const row = this.database.prepare("SELECT storage_status FROM local_backend_storage_state WHERE singleton = 1").get() as { storage_status: "active" | "full" | "unavailable" };
|
|
return row.storage_status;
|
|
}
|
|
|
|
markLogUnavailable() {
|
|
this.database.prepare(`
|
|
UPDATE local_backend_storage_state
|
|
SET storage_status = 'unavailable', measured_at = ?, version = version + 1
|
|
WHERE singleton = 1
|
|
`).run(new Date().toISOString());
|
|
}
|
|
|
|
close() {
|
|
this.database.close();
|
|
}
|
|
}
|