Invental/Review lessons/Background jobs when something fails

Background jobs when something fails

Queue dedup ignores jobs already running

A queue's “deduplicate by job id” usually only covers jobs still waiting. Once a job is active, a second trigger needs your own in-flight check, or it becomes a duplicate run.

01 · Before and afterThe pattern

TypeScript
// before: rely on the queue's id-based dedup
await queue.add(name, data, { jobId: `upgrade.${ws}.${app}` });
// after: check active jobs yourself and return the running one
const running = (await queue.getActive()).find(j => j.id.startsWith(prefix));
if (running) return { jobId: running.id };
if (conflicting.some(op => inFlight(op, ws, app))) throw new UserError(msg);
return queue.add(name, data, { jobId: prefix });

02 · Run it on your appThe check

If a user double-clicks “Upgrade” (or two admins click it), how many upgrade jobs run?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.