fix: prevent database is locked on concurrent startup
Set busy_timeout before journal_mode=WAL in db.ts so SQLite retries for 10s instead of failing immediately with the default 0ms timeout. Extract migration into a dedicated Docker Compose service so both jetstream and api wait for it to complete before opening the database.
This commit is contained in:
@@ -1,8 +1,17 @@
|
|||||||
services:
|
services:
|
||||||
|
migrate:
|
||||||
|
build: .
|
||||||
|
command: ["deno", "task", "migrate"]
|
||||||
|
volumes:
|
||||||
|
- ${DATA_VOLUME:-data}:/data
|
||||||
|
|
||||||
jetstream:
|
jetstream:
|
||||||
build: .
|
build: .
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: ["sh", "-c", "deno task migrate && deno task jetstream:prod"]
|
command: ["deno", "task", "jetstream:prod"]
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
volumes:
|
volumes:
|
||||||
- ${DATA_VOLUME:-data}:/data
|
- ${DATA_VOLUME:-data}:/data
|
||||||
|
|
||||||
@@ -10,6 +19,9 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: ["deno", "task", "server:prod"]
|
command: ["deno", "task", "server:prod"]
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
expose:
|
expose:
|
||||||
- "8080"
|
- "8080"
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import type { Note } from "./note.ts";
|
|||||||
|
|
||||||
export const db = new Database(Deno.env.get("SQLITE_PATH") ?? "notes.db");
|
export const db = new Database(Deno.env.get("SQLITE_PATH") ?? "notes.db");
|
||||||
try {
|
try {
|
||||||
|
db.exec("PRAGMA busy_timeout=10000");
|
||||||
db.exec("PRAGMA journal_mode=WAL");
|
db.exec("PRAGMA journal_mode=WAL");
|
||||||
db.exec("PRAGMA busy_timeout=5000");
|
|
||||||
const [row] = db.prepare("PRAGMA journal_mode").all<{ journal_mode: string }>();
|
const [row] = db.prepare("PRAGMA journal_mode").all<{ journal_mode: string }>();
|
||||||
console.log(`[db] journal_mode=${row.journal_mode}, busy_timeout=5000`);
|
console.log(`[db] journal_mode=${row.journal_mode}, busy_timeout=10000`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[db] failed to set PRAGMAs:", e);
|
console.error("[db] failed to set PRAGMAs:", e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user