~/proyectos
cron-scheduler-ts
In-memory cron/interval scheduler for Node.js: zero runtime dependencies, full TypeScript support, pluggable persistence, missed-job detection, and zero drift via monotonic next-tick recomputation.
problem
Needed a cron-style scheduler for recurring tasks in Node.js projects without pulling in node-cron or agenda. Existing options either drag runtime dependencies, accumulate drift by rescheduling from the previous fire time, or miss jobs that should have run while the process was down.
architecture
ESM/CJS module with zero runtime deps. Each job owns its setTimeout and recomputes the next tick from new Date() on every fire, killing drift. Hand-rolled cron parser (5/6 fields, ranges, steps, aliases, month and weekday tokens). Pluggable StorageAdapter with in-memory and filesystem implementations (atomic write via .tmp + rename). Coalesced persistence to avoid disk thrash on high-frequency jobs.
decisions
- ›Per-job setTimeout instead of a global tick: every reschedule recomputes from wall-clock now, making accumulated drift impossible.
- ›Zero-dep cron parser hand-written: supports real-world formats (5/6 fields, */n, ranges, lists, @daily aliases, jan-dec / sun-sat tokens) without bloating the bundle.
- ›StorageAdapter as a minimal interface: any backend (file, SQLite, Postgres, Redis) fulfills the contract without coupling the core to a driver.
- ›Missed-job detection on startup: if lastRun + interval is less than now, missed is emitted with no replay and the job is rescheduled for the next future tick.
- ›Atomic write in FileSystemAdapter: write to .tmp then rename, to avoid corruption if the process dies mid-save.
diagrams
// job lifecycle
// internal structure
// missed-job detection