提交 50ab45e9 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

better timer for watchdog to monitor scan event

上级 12e9096c
...@@ -117,6 +117,10 @@ function onServerScan(data) { ...@@ -117,6 +117,10 @@ function onServerScan(data) {
this.browser.saveSession(this.session) this.browser.saveSession(this.session)
.catch(() => {/* fail safe */}) .catch(() => {/* fail safe */})
} }
// feed watchDog a `scan` type of food
this.watchDog(data, {type: 'scan'})
this.emit('scan', data) this.emit('scan', data)
} }
......
...@@ -186,45 +186,54 @@ class PuppetWeb extends Puppet { ...@@ -186,45 +186,54 @@ class PuppetWeb extends Puppet {
watchDog(data, options) { watchDog(data, options) {
log.silly('PuppetWeb', 'watchDog(%s)', data) log.silly('PuppetWeb', 'watchDog(%s)', data)
options = options || {} options = options || {}
const TIMEOUT = options.timeout || 60000 // 60s default. can be override in options const timeout = options.timeout || 60000 // 60s default. can be override in options
const EVENT = options.event || 'food' // just a name const type = options.type || 'food' // just a name
if (this.watchDogTimer) { if (this.watchDogTimer) { clearTimeout(this.watchDogTimer) }
clearTimeout(this.watchDogTimer) this.watchDogTimer = setTimeout(() => this.watchDogReset(timeout), timeout)
}
this.watchDogTimer = setTimeout(() => {
const err = new Error('watchdog timeout after ' + Math.floor(TIMEOUT/1000) + ' seconds')
// this.emit('error', err)
Event.onBrowserDead.call(this, err)
}, TIMEOUT)
this.watchDogTimer.unref() // dont block quit this.watchDogTimer.unref() // dont block quit
const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 mins const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 mins
if (this.session) { if (this.session) {
if (this.watchDogLastSaveSession || Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) { if (this.watchDogLastSaveSession && Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) {
log.verbose('PuppetWeb', 'watchDog() saveSession(%s) after %d minutes', this.session, Math.floor(SAVE_SESSION_INTERVAL/1000/60)) log.verbose('PuppetWeb', 'watchDog() saveSession(%s) after %d minutes', this.session, Math.floor(SAVE_SESSION_INTERVAL/1000/60))
this.browser.saveSession(this.session) this.browser.saveSession(this.session)
} }
this.watchDogLastSaveSession = Date.now() this.watchDogLastSaveSession = Date.now()
} }
// if web browser stay at login qrcode page long time, /**
// sometimes the qrcode will not refresh, leave there expired. * if web browser stay at login qrcode page long time,
// so we need to refresh the page after a while * sometimes the qrcode will not refresh, leave there expired.
const REFRESH_TIMEOUT = 10 * 60 * 1000 // 10 mins * so we need to refresh the page after a while
*/
if (type === 'scan') { // watchDog was feed a 'scan' data
log.verbose('PuppetWeb', 'watchDog() got a food with type scan')
this.lastScanEventTime = Date.now()
}
if (!this.logined()) { if (!this.logined()) {
if (!this.watchDogLastRefresh) { const scanTimeout = 10 * 60 * 1000 // 10 mins
this.watchDogLastRefresh = Date.now() if (!this.lastScanEventTime) { // 1st scan event
this.lastScanEventTime = Date.now()
} }
if (Date.now() - this.watchDogLastRefresh > REFRESH_TIMEOUT) {
log.warn('PuppetWeb', 'watchDog() refresh browser for not login for a long time') if (Date.now() - this.lastScanEventTime > scanTimeout) {
this.browser.refresh() log.warn('PuppetWeb', 'watchDog() refresh browser for no food of type scan after %s mins', Math.floor(scanTimeout/1000/60))
this.watchDogLastRefresh = Date.now() this.watchDogLastRefresh = Date.now()
// fix the problem here
this.browser.refresh()
} }
} else if (this.watchDogLastRefresh) { } else if (this.lastScanEventTime) {
this.watchDogLastRefresh = null this.lastScanEventTime = null
} }
} }
watchDogReset(timeout) {
const e = new Error('watchdog reset after ' + Math.floor(timeout/1000) + ' seconds')
this.emit('error', e)
return Event.onBrowserDead.call(this, e)
}
send(message) { send(message) {
const to = message.get('to') const to = message.get('to')
const room = message.get('room') const room = message.get('room')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册