mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-25 04:53:00 -05:00
45 lines
935 B
TypeScript
45 lines
935 B
TypeScript
import * as Sentry from "@sentry/react-router";
|
|
import { logger } from "../utils/logger";
|
|
|
|
const SENTRY_ENABLED = import.meta.env.VITE_SENTRY_ENABLED === "true";
|
|
|
|
export class Routine {
|
|
private name;
|
|
private func;
|
|
|
|
constructor({
|
|
name,
|
|
func,
|
|
}: {
|
|
name: string;
|
|
func: () => Promise<void>;
|
|
}) {
|
|
this.name = name;
|
|
this.func = func;
|
|
}
|
|
|
|
async run() {
|
|
logger.info(`Running routine: ${this.name}`);
|
|
const work = async () => {
|
|
const startTime = performance.now();
|
|
try {
|
|
await this.func();
|
|
} catch (error) {
|
|
logger.error(`Error running routine ${this.name}: ${error}`);
|
|
if (SENTRY_ENABLED) {
|
|
Sentry.captureException(error);
|
|
}
|
|
return;
|
|
}
|
|
const endTime = performance.now();
|
|
logger.info(`Routine ${this.name} completed in ${endTime - startTime}ms`);
|
|
};
|
|
|
|
if (SENTRY_ENABLED) {
|
|
await Sentry.startSpan({ name: this.name, op: "cron" }, work);
|
|
} else {
|
|
await work();
|
|
}
|
|
}
|
|
}
|