The most reliable way you can develop a NodeJS application to manage side effects that absolutely need to happen is via application-level CRON jobs. The open-source community around NodeJS provides pretty decent packages that developers can use within their apps to write CRON jobs. I find AgendaJS to be the best for two reasons: it uses MongoDB as its persistence layer to queue, execute, and reschedule jobs; and because it exposes a developer-friendly API allowing for greater programmability.
Within src/loaders, the mongo instance is passed onto the agendaLoader which configures a new AgendaJS instance. AgendaJS allows developers to tweak and set some of its core features in whichever way they deem them fit. I find it best to load this configuration from the environment variables injected by the running container. The new AgendaJS instance is then passed onto the dependency injector where it’s passed as a dependency to the SchedulerService - a custom class that provides an interface to write and schedule all jobs via public methods.
The SchedulerService’s public methods can be passed in the necessary data for each job and the job can be scheduled via the AgandaJS instance passed to it upon instantiation. AgendaJS allows a sleek way to program retries and more. You can learn more about its API in its official documentation.
The job definitions are written within src/jobs. These jobs are loaded via a custom recursive loader function executed from within the main loading script. In the example project, the updateUserLastLogin.js file has the job definition of the AgendaJS job of the same name. Whatever poolTime you set for AgendaJS, it’ll read scheduled jobs from its associated MongoDB collection, and run the correct script. Failed jobs should be rescheduled with an exponential backoff mechanism.
Copyright © 2026. Salman Ahmad.