Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Guard scheduled/cron jobs against overlapping runs
Skip a scheduled job tick if the previous run hasn't finished, and reset the guard in finally.
Bad example
| 1 | cron.schedule('*/5 * * * *', async () => { |
| 2 | await processFiles(); // a slow run overlaps the next tick (and the startup invocation) |
| 3 | }); |
Explanation (EN)
If a run takes longer than the interval, or the app also kicks off a run on startup, two invocations execute concurrently and may double-process or race on shared state.
Objašnjenje (HR)
Ako pokretanje traje duze od intervala, ili app pokrene posao i pri startu, dvije instance se izvrsavaju paralelno i mogu dvostruko obraditi podatke ili se utrkivati nad dijeljenim stanjem.
Good example
| 1 | let isRunning = false; |
| 2 |
|
| 3 | async function runJob(): Promise<void> { |
| 4 | if (isRunning) { |
| 5 | logger.info('Job still running - skipping this tick'); |
| 6 | return; |
| 7 | } |
| 8 | isRunning = true; |
| 9 | try { |
| 10 | await processFiles(); |
| 11 | } finally { |
| 12 | isRunning = false; |
| 13 | } |
| 14 | } |
| 15 |
|
| 16 | cron.schedule('*/5 * * * *', runJob); |
Explanation (EN)
An entry guard that is reset in finally makes the job re-entrancy-safe regardless of run duration or an extra startup trigger.
Objašnjenje (HR)
Ulazni guard koji se resetira u finally cini posao sigurnim na ponovni ulaz bez obzira na trajanje ili dodatni okidac pri startu.
Exceptions / Tradeoffs (EN)
An in-process flag only protects a single process. For multi-instance deployments use a distributed lock (e.g. Redis SETNX, DB advisory lock) instead.
Iznimke / Tradeoffi (HR)
Zastavica unutar procesa stiti samo jedan proces. Za vise instanci koristi distribuirani lock (npr. Redis SETNX, DB advisory lock).