I have a date field in my Cron table. It's unclear to me if I need to convert this into a Date after loading for date math. Is this correct?
export const poorMansCron = async () => {
// Call crontask by API if more than 5 minutes since last call
const lastcall = (await db.select().from(Cron).where(eq(Cron.task, 'cronjob')))[0];
const fiveMinutes = 5 * 60 * 1000; // 5 minutes in milliseconds
if (!lastcall) {
// If no last call time is recorded, insert a new record
await db.insert().into(Cron).values({ task: 'cronjob', time: new Date() });
await crontasks();
} else if (lastcall && (new Date() - new Date(lastcall.time) > fiveMinutes)) {
// If more than 5 minutes have passed since the last call
await crontasks();
// Update the time after tasks are done
await db.update(Cron).set({ time: new Date() }).where(eq(Cron.task, 'cronjob'));
}
}