Search code examples
node.jsmongodbagenda

Agenda when instantiated with existing Mongodb connection, not running the job definition


Instead of using the mongodb connection string when initialising the agenda instance, i am using an already connected mongo client instance. The issue i am facing is that a document for the agenda.every gets added to the collection, but when the specified time is reached, job definition does not run. This issue happens only when i use an already connected mongo client instance, otherwise it works normally when i let agenda to handle the connection.

I also noticed that when ram usage exceeds 85%+ capacity it results in MongoNetworkError. If anyone faced similar issues and found a solution, please tag that answer along.

`agenda.then((agendaTmp: any) => {
    console.log("Received: ", agendaTmp.status);
    agendaTmp.agenda.define("test3", {}, (job: any, done: any) => {
      console.log("New agendaTmp schedule running");
      done();
    });
    agendaTmp.agenda.on("ready", async () => {
      console.log("Ready: ");
      agendaTmp.agenda.every("*/1 * * * *", "test3", {});
      await agendaTmp.agenda.start();
    });
}

// Agenda init file
import { Agenda } from "agenda";
import { MongoClient } from "mongodb";

const agenda = new Promise((resolve, reject) => {
  const client = new MongoClient(dbURL);
  client
    .connect()
    .then(() => {
      const database = client.db("scheduler");
      const collection = database.collection("newNotification");
   
      collection.findOne({}).then((result) => {
        console.log("In mongo query: ", result);
      });

      let agendaTmp = new Agenda({
        processEvery: "40 seconds",
      });
      agendaTmp.mongo(database, "newNotification");

      return resolve({"agenda":agendaTmp, "status":true});
    })
    .catch(() => {
      return reject(false);
    });
});

export {agenda};`


Solution

  • Our team could finally resolve this issue:

    1. Agenda when instantiated with existing Mongodb connection is found to be working when tried in a sample application.

    2. For MongoNetworkError on high RAM usage, used 'useUnifiedTopology' option: new Agenda({ db: { address: mongoDbUrl, collection: "newNotification", options: { useUnifiedTopology: true } }, processEvery: "40 seconds"});