在初始化议程实例时,我没有使用 mongodb 连接字符串,而是使用已经连接的 mongo 客户端实例。我面临的问题是议程的文档。每个都被添加到集合中,但是当达到指定的时间时,作业定义不会运行。仅当我使用已连接的 mongo 客户端实例时才会出现此问题,否则当我让议程处理连接时它可以正常工作。
我还注意到,当 ram 使用超过 85%+ 容量时,它会导致 MongoNetworkError。如果有人遇到类似问题并找到解决方案,请标记该答案。
`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};`