Skip to content

Commit

Permalink
fix(app-service): fix #19 watch error while mongo conn losed
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 28, 2021
1 parent 66e9d7a commit 53a8aae
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/app-service/src/lib/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-11-17 14:11:16
* @LastEditTime: 2021-12-28 10:18:03
* @Description:
*/

Expand Down Expand Up @@ -38,10 +38,23 @@ accessor.ready.then(async () => {

// watch database operation event through `WatchStream` of mongodb
const db = accessor.db
const stream = db.watch([], { fullDocument: 'updateLookup' })
stream.on("change", (doc) => { DatabaseChangeEventCallBack(doc) })
process.on('SIGINT', () => stream.close())
process.on('SIGTERM', () => stream.close())

function startWatchChangeStream() {
logger.info('start watching change stream')
const stream = db.watch([], { fullDocument: 'updateLookup' })
stream.on("change", (doc) => { DatabaseChangeEventCallBack(doc) })
stream.on('error', (err) => {
logger.error('stream watch error: ', err)
setTimeout(() => {
logger.info('restart watching change stream...')
startWatchChangeStream()
}, 3000)
})
process.on('SIGINT', () => stream.close())
process.on('SIGTERM', () => stream.close())
}

startWatchChangeStream()

// emit `App:ready` event
SchedulerInstance.emit('App:ready')
Expand Down

0 comments on commit 53a8aae

Please sign in to comment.