First Commit. Duplicate bunext-sqlite preset

This commit is contained in:
2026-10-18 16:47:11 +01:00
commit 81e4a8aecc
718 changed files with 41695 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
import bunext from "@moduletrace/bunext";
import BunSQLite from "@moduletrace/bun-sqlite";
export default async function heartbeat() {
const db_alive = await BunSQLite.sql({
sql: `SELECT 1 AS alive`,
});
bunext.bunextLog.info(
`Cron: database heartbeat ${db_alive.success ? "ok" : "failed"}`,
);
return db_alive;
}
+18
View File
@@ -0,0 +1,18 @@
import bunext from "@moduletrace/bunext";
import cronHeartbeat from "./heartbeat";
import registerCronJob from "./register-cron-job";
export default function cron() {
if (process.env.NODE_ENV == "production" && !process.env.MAIN_SERVER) {
return;
}
bunext.bunextLog.watch(`Running Cron Services ...`);
registerCronJob({
title: "heartbeat",
schedule: "@hourly",
task: cronHeartbeat,
run_on_start: true,
});
}
+44
View File
@@ -0,0 +1,44 @@
import logEvent, { logError } from "@/src/functions/backend/logger";
type Params = {
title: string;
schedule: Bun.CronWithAutocomplete;
task: () => Promise<unknown>;
run_on_start?: boolean;
};
/**
* Registers an in-process `Bun.cron` job. Each run is logged first, and
* errors are caught and logged so a failing task never crashes the process
* (Bun.cron rejections otherwise surface as `unhandledRejection`).
* Schedules are interpreted in UTC.
*/
export default function registerCronJob({
title,
schedule,
task,
run_on_start,
}: Params) {
const run = async () => {
try {
await logEvent({
title: `Cron: ${title}`,
category: "cron",
plain_text: `CRON: ${title} ...`,
});
await task();
} catch (error) {
await logError({
title: `Cron: ${title} failed`,
category: "cron",
error,
}).catch(() => {});
}
};
if (run_on_start) {
run();
}
return Bun.cron(schedule, run);
}