0

I'm trying to use the agenda package to schedule some tasks in my Nodejs project, But I'm facing some timeout issues related to the agenda package, So my question is how to make the connection property in my project? using mongoose package to make MongoDB Atlas connection how to use the existing connection of MongoDB with my agenda?

In my current code when I do some tasks it doesn't show me anything in my Atlas but I can only see the AgendaJobs collection, but inside it i can't see anything after I schedule some tasks in my Nodejs project.

How do i do things properly?

db.js:

import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();

mongoose.set('strictQuery', false);

export const MongoDBConnection = async () => {
    try {
        const connection = await mongoose.connect('mymongourl');
        console.log('Connected to database');
        return connection.connection.getClient();
    } catch (err) {
        console.log('Error connecting to database: ' + err);
    }
};

I'm just calling this function in my index.js file like this MongoDBConnection()

Agenda Connection:

import { MongoDBConnection } from '../database/db.js';
const client = await MongoDBConnection();
const agenda = new Agenda({ mongo: client.db('AgendaJobs') });

agenda.on('ready', () => {
    console.log('Schedule the task')
    agenda.start();
});
cwecae
  • 43
  • 1
  • 6

1 Answers1

0

Basing on my answer on the code that is shown here - you're missing a lot. Per the docs,

Before you can use a job, you must define its processing behavior.

Core concept of the agenda package is that there is an agenda instance that queues, schedules and performs defined jobs, which are logged to the agendaJobs collection. You have the instance, but there are no defined jobs. When you call await agenda.start(), there are no definitions attached to this instance, meaning there is a disconnect between the instance and the jobs. You need to first create definitions for specific jobs, then decide how you want to create those jobs.

Here are some articles that helped me get started:

abischolz
  • 11
  • 3