Peer
Peer

Reputation: 241

PoolClearedOnNetworkError with mongodb | agenda in nodejs

I get following error in my application after some unspecified time:

node:internal/process/promises:289 triggerUncaughtException(err, true /* fromPromise */); ^

PoolClearedOnNetworkError: Connection to 127.0.0.1:27017 interrupted due to server monitor timeout at ConnectionPool.interruptInUseConnections (C:\Programming\twitch-followers\node_modules\agenda\node_modules\mongodb\lib\cmap\connection_pool.js:264:36) at C:\Programming\twitch-followers\node_modules\agenda\node_modules\mongodb\lib\cmap\connection_pool.js:251:41 at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { address: '127.0.0.1:27017', [Symbol(errorLabels)]: Set(1) { 'RetryableWriteError' } }

I use agenda.js for job scheduling and mongodb.

This is how I initialize agenda:

// Function to create indexes on the agendaJobs collection
async function createIndexes() {
  try {
    const db = mongoose.connection;
    await db
      .collection("agendaJobs")
      .createIndex({ lockedAt: 1, nextRunAt: 1, priority: -1, name: 1 });
    await db.collection("agendaJobs").createIndex({ lastFinishedAt: 1 });
    logger.log("application", "Indexes created successfully.");
  } catch (error) {
    logger.error("Error creating indexes:", error);
  }
}

const mongoConnectionString = config.dbURI;
const agenda = new Agenda({
  db: {
    address: mongoConnectionString,
    collection: "agendaJobs",
    options: {
      serverSelectionTimeoutMS: 60000, // Wait 60 seconds before timing out
      socketTimeoutMS: 600000, // Keep the socket open for 10 minutes
      connectTimeoutMS: 60000,
      maxPoolSize: 100, // Increase connection pool size
      minPoolSize: 5, // Reduced from 10
      maxIdleTimeMS: 30000, // Close idle connections after 30 seconds
    },
  },
  defaultConcurrency: 800, // Set global concurrency
  maxConcurrency: 800, // Set max concurrency

  defaultLockLifetime: 60000, // 60 seconds lock lifetime
});

Any ideas?

Upvotes: 0

Views: 168

Answers (0)

Related Questions