feat(puchase history): purhcase history changed to long running no notification
This commit is contained in:
@@ -18,7 +18,9 @@ export interface JobInfo {
|
||||
|
||||
// Store running cronjobs
|
||||
export const runningCrons: Record<string, Cron> = {};
|
||||
const activeRuns = new Set<string>();
|
||||
const log = createLogger({ module: "system", subModule: "croner" });
|
||||
const cronStats: Record<string, { created: number; replaced: number }> = {};
|
||||
|
||||
// how to se the times
|
||||
// * ┌──────────────── (optional) second (0 - 59)
|
||||
@@ -38,17 +40,36 @@ const log = createLogger({ module: "system", subModule: "croner" });
|
||||
* @param name Name of the job we want to run
|
||||
* @param schedule Cron expression (example: `*\/5 * * * * *`)
|
||||
* @param task Async function that will run
|
||||
* @param source we can add where it came from to assist in getting this tracked down, more for debugging
|
||||
*/
|
||||
export const createCronJob = async (
|
||||
name: string,
|
||||
schedule: string, // cron string with 8 8 IE: */5 * * * * * every 5th second
|
||||
task: () => Promise<void>, // what function are we passing over
|
||||
source = "unknown",
|
||||
) => {
|
||||
// get the timezone based on the os timezone set
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
|
||||
// initial go so just store it this is more for debugging to see if something crazy keeps happening
|
||||
if (!cronStats[name]) {
|
||||
cronStats[name] = { created: 0, replaced: 0 };
|
||||
}
|
||||
|
||||
// Destroy existing job if it exist
|
||||
if (runningCrons[name]) {
|
||||
cronStats[name].replaced += 1;
|
||||
log.warn(
|
||||
{
|
||||
job: name,
|
||||
source,
|
||||
oldSchedule: runningCrons[name].getPattern?.(),
|
||||
newSchedule: schedule,
|
||||
replaceCount: cronStats[name].replaced,
|
||||
},
|
||||
`Cron job "${name}" already existed and is being replaced`,
|
||||
);
|
||||
|
||||
runningCrons[name].stop();
|
||||
}
|
||||
|
||||
@@ -61,6 +82,13 @@ export const createCronJob = async (
|
||||
name: name,
|
||||
},
|
||||
async () => {
|
||||
if (activeRuns.has(name)) {
|
||||
log.warn({ jobName: name }, "Skipping overlapping cron execution");
|
||||
return;
|
||||
}
|
||||
|
||||
activeRuns.add(name);
|
||||
|
||||
const startedAt = new Date();
|
||||
const start = Date.now();
|
||||
|
||||
@@ -91,14 +119,19 @@ export const createCronJob = async (
|
||||
.where(eq(jobAuditLog.id, executionId));
|
||||
} catch (e: any) {
|
||||
if (executionId) {
|
||||
await db.update(jobAuditLog).set({
|
||||
finishedAt: new Date(),
|
||||
durationMs: Date.now() - start,
|
||||
status: "error",
|
||||
errorMessage: e.message,
|
||||
errorStack: e.stack,
|
||||
});
|
||||
await db
|
||||
.update(jobAuditLog)
|
||||
.set({
|
||||
finishedAt: new Date(),
|
||||
durationMs: Date.now() - start,
|
||||
status: "error",
|
||||
errorMessage: e.message,
|
||||
errorStack: e.stack,
|
||||
})
|
||||
.where(eq(jobAuditLog.id, executionId));
|
||||
}
|
||||
} finally {
|
||||
activeRuns.delete(name);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user