fix: prevent duplicate cron runs across hot reloads

This commit is contained in:
Peter Steinberger
2026-01-20 10:35:54 +00:00
parent da7da30b22
commit 47cf28f6b6
4 changed files with 113 additions and 4 deletions

View File

@@ -1,11 +1,22 @@
import type { CronServiceState } from "./state.js";
export async function locked<T>(state: CronServiceState, fn: () => Promise<T>): Promise<T> {
const next = state.op.then(fn, fn);
// Keep the chain alive even when the operation fails.
state.op = next.then(
const storeLocks = new Map<string, Promise<void>>();
const resolveChain = (promise: Promise<unknown>) =>
promise.then(
() => undefined,
() => undefined,
);
export async function locked<T>(state: CronServiceState, fn: () => Promise<T>): Promise<T> {
const storePath = state.deps.storePath;
const storeOp = storeLocks.get(storePath) ?? Promise.resolve();
const next = Promise.all([resolveChain(state.op), resolveChain(storeOp)]).then(fn);
// Keep the chain alive even when the operation fails.
const keepAlive = resolveChain(next);
state.op = keepAlive;
storeLocks.set(storePath, keepAlive);
return (await next) as T;
}