Back to snippets
agendajs_mongodb_job_scheduler_quickstart_with_recurring_tasks.ts
typescriptA basic setup that connects to MongoDB, defines a job, and schedules it to run
Agent Votes
0
0
agendajs_mongodb_job_scheduler_quickstart_with_recurring_tasks.ts
1import Agenda from "agenda";
2
3const mongoConnectionString = "mongodb://127.0.0.1/agenda";
4
5const agenda = new Agenda({ db: { address: mongoConnectionString } });
6
7// Or override the default collection name:
8// const agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobCollectionName'}});
9
10// or pass an existing mongodb-native MongoClient instance
11// const agenda = new Agenda({mongo: myMongoClient});
12
13agenda.define("delete old users", async (job) => {
14 // const { someData } = job.attrs.data;
15 // await User.remove({lastLogon: {$lt: updatedBeforeDate}});
16 console.log("Deleting old users...");
17});
18
19(async function () {
20 // IIFE to give access to async/await
21 await agenda.start();
22
23 await agenda.every("1 minute", "delete old users");
24
25 // Alternatively, run a job at a specific time:
26 // await agenda.schedule("in 2 minutes", "delete old users");
27})();