监听MongoDB驱动程序中的重新连接事件 [英] Listen to reconnect events in MongoDB driver

查看:337
本文介绍了监听MongoDB驱动程序中的重新连接事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MongoDB连接中添加事件侦听器,以在连接断开,每次重新连接尝试以及成功的重新连接尝试时运行某些东西.

I would like to add event listeners to a MongoDB connection to run something when the connection drops, each reconnection attempt and at a successful reconnection attempt.

我阅读了所有官方文档和API,但找不到解决方案.

I read all the official docs and the API, but I can't find a solution.

当前,我有这个,但是只有超时事件有效. //如果尚未初始化'MongoClient',请进行初始化并保存. if(!this.client)this.client = new MongoClient();

Currently, I have this, but only the timeout event works. // If we didn't already initialize a 'MongoClient', initialize one and save it. if(!this.client) this.client = new MongoClient();

    this.connection = await this.client.connect(connectionString, this.settings);

    this.client.server.on('connect', event => {
        console.log(event);
    });

    this.client.server.on('error', event => {
        console.log(event);
    });

    this.client.server.on('reconnect', event => {
        console.log(event);
    });

    this.client.server.on('connections', event => {
        console.log(event);
    });

    this.client.server.on('timeout', event => {
        console.log(event);
    });

    this.client.server.on('all', event => {
        console.log(event);
    });

我尝试了此处列出的事件,它们可以工作,但是没有重新连接"事件: http://mongodb.github.io/node -mongodb-native/2.2/reference/management/sdam-monitoring/

I tried the events listed here, and they work, but there is no "reconnect" event: http://mongodb.github.io/node-mongodb-native/2.2/reference/management/sdam-monitoring/

推荐答案

可以.基本上,尽管您需要从比MongoClient本身更低的级别上挖掘EventEmitter.

Sure you can. Basically though you need to tap into the EventEmitter at a lower level than basically off the MongoClient itself.

您可以清楚地看到这些东西存在,因为它们在日志记录"中可见,可以通过以下设置在驱动程序中将其打开:

You can clearly see that such things exist since they are visible in "logging", which can be turned on in the driver via the setting:

{ "loggerLevel": "info" }

从那时起,实际上只需要挖掘实际的源发射器即可.我已经在下面的清单中完成了这些操作,还包括了一个小技巧,可以从给定的发射中获取枚举事件,这是我本人在跟踪此事件时所公认的:

From then it's really just a matter of tapping into the actual source emitter. I've done these in the following listing, as well as including a little trick for getting the enumerated events from a given emitted, which was admittedly used by myself in tracking this down:

const MongoClient = require('mongodb').MongoClient;

function patchEmitter(emitter) {
  var oldEmit = emitter.emit;

  emitter.emit = function() {
    var emitArgs = arguments;

    console.log(emitArgs);

    oldEmit.apply(emitter, arguments);
  }

}


(async function() {

  let db;

  try {

    const client = new MongoClient();

    client.on('serverOpening', () => console.log('connected') );

    db = await client.connect('mongodb://localhost/test', {
      //loggerLevel: 'info'
    });

    //patchEmitter(db.s.topology);

    db.s.topology.on('close', () => console.log('Connection closed') );
    db.s.topology.on('reconnect', () => console.log('Reconnected') );


  } catch(e) {
    console.error(e)
  }

})()

因此定义了这两个侦听器:

So those two listeners defined:

    db.s.topology.on('close', () => console.log('Connection closed') );
    db.s.topology.on('reconnect', () => console.log('Reconnected') );

当连接断开并实现重新连接时,会触发.还有其他一些事情,例如重新连接尝试,这些事件也出现在事件发射器中,就像打开loggerLevel设置时所看到的那样.

Are going to fire when the connection drops, and when an reconnect is achieved. There are also other things like reconnect attempts which are also in the event emitter just like you would see with the loggerLevel setting turned on.

这篇关于监听MongoDB驱动程序中的重新连接事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆