tail () { const ts = this.lastTs const nss = this.defs.map(def => def.ns) const filters = { ns: { $in: nss }, ts: { $gt: Timestamp.fromNumber(ts) } } const curOpts = { tailable: true, awaitdata: true, numberOfRetries: 60 * 60 * 24, // Number.MAX_VALUE, tailableRetryInterval: 1000 }
util.log(`Begin to watch... (from ${ts})`)
return new Promise((resolve, reject) =>
mongodb.connect(this.url2).then(db => {
this.db = db
const stream = db.collection('oplog.rs').find(filters, curOpts).stream()
stream
.on('data', log => {
if (log.op === 'n' || log.ts.toNumber() === ts) return
this.process(log)
})
.on('close', () => {
util.log('Stream closed....')
this.db = null
db.close()
resolve()
})
.on('error', err => {
this.db = null
db.close()
reject(err)
})
}))
}
目的是读取 monogodb 的 oplog 将数据同步到 mysql 里边
1
TomVista 2018-11-27 21:52:01 +08:00 via iPhone
return 函数对象形成的闭包导致,对象用完指定为 null,应该是这个理
|