Scheduling Jobs in TypeScript with Agenda

Oladipo

Oladipo Adesiyan

How to schedule Jobs in Typescript using Agenda

In this world where everything seems to be moving quickly and asynchronously, the effect of automation cannot be underestimated. From automatic gears to self-driven vehicles, to automatic heating and cooling, automation makes everything faster and less stressful.
In this tutorial, I will be walking you through how to automate processes and tasks using Agenda. Agenda is a job scheduling library for Typescript which runs on MongoDB database. Agenda provides you with vast resources such as concurrency, repeatable jobs, UI, priorities among others.

Why Use Agenda?

Scheduling Jobs: Agenda allows you to schedule jobs to run at specific times, intervals, or based on recurring schedules (cron jobs).
Persistence: By storing job data in MongoDB, Agenda ensures that jobs persist across server restarts and can be resumed if they were in progress.
Flexibility: Agenda provides a flexible API for defining, managing, and running jobs, making it suitable for various use cases such as email notifications, data processing, and more.

Prerequisites

To get started, it is crucial to have a solid understanding of TypeScript. You will need an existing TypeScript application where you can integrate the Agenda library, along with a MongoDB database for data storage. If you haven’t yet set up a TypeScript application, this article will walk you through the necessary steps. Similarly, if you need to set up a MongoDB database, this article will serve as a comprehensive guide. Having both components properly configured is essential for the seamless integration of Agenda.

Installation

After properly setting up your Typescript Application and MongoDB Database, if your preferred package manager is npm, open your terminal and type this command:
npm i agenda
For yarn users:
yarn add agenda
Note: You will also need a working Mongo database (v3) to point it to. — Agenda maintainers
This command installs the latest version of Agenda package into your typescript application.
Next, verify that your environment variables are properly configured. This setup enables the use of distinct MongoDB URIs for different environments and enhances security by protecting sensitive information.

Integrating Agenda

Create a file named agenda.config.ts within the configuration folder located in your src directory. This file will house all your Agenda configurations and setup instructions.
The collection value in your Agenda instance specifies the name of the collection where Agenda will store its records in your database. For this example, we will use "cronJobs" as the collection name.
This creates a new instance of Agenda . This instance is configured to connect to a MongoDB database using the connection string stored in env.MONGODB_URI, and it will use the "cronJobs" collection within that database to store jobs information.
With this, you have successfully integrated Agenda into your Typescript application. Restart your application and check to see if you have
Agenda Cron Job Connected
in your terminal. If you do, we can proceed to the next step, if not, spend a few minutes to debug the issue. You could start by checking if your MONGO_URI is correct. Additionally, ensure that your database is active, among other potential troubleshooting steps.
Following this step, we can advance to scheduling our jobs.

Assumptions

In this tutorial, we’ll assume that our TypeScript backend service is designed for a Content Management System (CMS) website. Writers can leverage this system to schedule the publication time for their content.
With routes, controllers, and models already established, we’ll head straight into constructing our scheduler.

Building our scheduler

Create a file named jobs.ts within the agenda folder located in your utils directory. This file will house all your Agenda configurations and setup instructions.
Ensure your job names are concise to avoid confusion, especially when creating similar jobs multiple times. This practice saves time and prevents ambiguity when identifying the purpose of each job.
In line 7, we’re extracting the attributes of the job’s data that has been given to it when the job was scheduled and lastly, we’re passing it to the scheduleContentPublicationfunction that will asynchronously make the content public.
Two things are still missing.
We have not registered the agenda jobs
We haven’t created a function to schedule the jobs
To register the jobs, include the following code in your server.ts, app.ts, or the file where your app instance is being created:
With this, you have initialized the job definitions with the agenda configuration.
Now, let us create a function that schedules our jobs.
The value publishDate value is the time in which your job is to run. For example; in 5 minutes, in 31 days, in 4 months. Agenda supports seconds, minutes, hours, days,weeks, months
This gives you an insight into how you can call your scheduler function and start your whole scheduling process.
Agenda offer other capabilities such as scheduling recurring jobs, concurrecy, job processing queues:
An instance of recurring job:
agenda.schedule('8:00 AM', 'sendDailyEmailSummary');
An instance of prioritized jobs:
agenda.define('criticalJob', { priority: 'high' }, async (job) => {});agenda.define('routineTask', { priority: 'low' }, async (job) => {});
An instance of a canceling a job:
agenda.cancel({ 'data.userId': '123' });
This is among the multiple capabilities of Agenda. To view more, check this out.

Conclusion

In this tutorial, we covered the installation of Agenda, integrating it into a TypeScript application, defining and scheduling jobs, and exploring its additional capabilities.”
In this tutorial, we majorly focused on an instance whereby an Engineer wants to schedule a job with a future date. However, there are more use cases for Agenda that I’d recommend so that you don’t get stuck on one.
If you’ve learned anything from this tutorial, please ensure to follow along, as I plan on sharing more insights from my journey in software engineering going forward.
With that being said, I’m excited to see the projects you all will build with Agenda.
Like this project

Posted Oct 1, 2025

Tutorial on using Agenda for job scheduling in TypeScript applications.

Likes

0

Views

0

Timeline

Sep 24, 2025 - Sep 25, 2025