Skip to content

Commit

Permalink
Add db optimize pragma
Browse files Browse the repository at this point in the history
Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com>
  • Loading branch information
M4RC3L05 committed Jun 8, 2024
1 parent 1bbca4c commit 5a722e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/apps/api/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Cron } from "@m4rc3l05/cron";
import { ProcessLifecycle } from "@m4rc3l05/process-lifecycle";
import { makeLogger } from "#src/common/logger/mod.ts";
import { gracefulShutdown } from "#src/common/process/mod.ts";
Expand All @@ -17,6 +18,39 @@ processLifecycle.registerService({
shutdown: (db) => db.close(),
});

processLifecycle.registerService({
name: "db-optimise",
boot: (pl) => {
const db = pl.getService<CustomDatabase>("db");
const cronInstance = new Cron((signal) => {
log.info("DB optimize runing");

try {
db.exec("pragma optimize");

log.info("DB optimize completed");
} catch (error) {
log.error("DB optimize failed", { error });
}

if (!signal.aborted) {
log.info(`Next db optimize at ${cronInstance.nextAt()}`);
}
}, {
when: "0 * * * *",
timezone: "UTC",
tickerTimeout: 300,
});

log.info(`Next db optimize at ${cronInstance.nextAt()}`);

cronInstance.start();

return cronInstance;
},
shutdown: (cron) => cron.stop(),
});

processLifecycle.registerService({
name: "api",
boot: (pl) => {
Expand Down
1 change: 1 addition & 0 deletions src/database/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const makeDatabase = () => {
db.exec("pragma temp_store = MEMORY");
// 4096 page_size * 10000 pages (cache_size) ≃ 40MB
db.exec("pragma cache_size = 10000");
db.exec("pragma optimize = 0x10002");
db.function("uuid_v4", () => globalThis.crypto.randomUUID());

return db;
Expand Down

0 comments on commit 5a722e4

Please sign in to comment.