First Commit. Duplicate bunext-sqlite preset
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user