Skip to content

Commit

Permalink
fix scan event lost bug, and prevent browser restart when we only nee…
Browse files Browse the repository at this point in the history
…d a refresh
  • Loading branch information
huan committed Jun 12, 2016
1 parent b3aeb8b commit 037f39c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/puppet-web-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class Browser extends EventEmitter {
})
}

refresh() {
log.verbose('PuppetWebBrowser', 'refresh()')
return this.driver.navigate().refresh()
}

getPhantomJsDriver() {
// https://github.com/SeleniumHQ/selenium/issues/2069
// setup custom phantomJS capability
Expand Down Expand Up @@ -229,7 +234,7 @@ class Browser extends EventEmitter {

return this.driver.executeScript.apply(this.driver, arguments)
.catch(e => {
this.dead(e.message)
this.dead(e)
throw e
})
}
Expand Down
29 changes: 25 additions & 4 deletions src/puppet-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class PuppetWeb extends Puppet {
if (this.session) {
yield this.browser.loadSession(this.session)
.catch(e => { // fail safe
log.verbose('PuppetWeb', 'browser.loadSession() exception: %s', e.message || e)
log.verbose('PuppetWeb', 'browser.loadSession(%s) exception: %s', this.session, e.message || e)
})
}
yield this.browser.open()
Expand Down Expand Up @@ -243,14 +243,31 @@ class PuppetWeb extends Puppet {
}, TIMEOUT)
this.watchDogTimer.unref() // dont block quit

const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 min
const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 mins
if (this.session) {
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))
this.browser.saveSession(this.session)
this.watchDogLastSaveSession = Date.now()
}
}

// if web browser stay at login qrcode page long time,
// sometimes the qrcode will not refresh, leave there expired.
// so we need to refresh the page after a while
const REFRESH_TIMEOUT = 10 * 60 * 1000 // 10 mins
if (!this.logined()) {
if (!this.watchDogLastRefresh) {
this.watchDogLastRefresh = Date.now()
}
if (Date.now() - this.watchDogLastRefresh > REFRESH_TIMEOUT) {
log.warn('PuppetWeb', 'watchDog() refresh browser for not login for a long time')
this.browser.refresh()
this.watchDogLastRefresh = Date.now()
}
} else if (this.watchDogLastRefresh) {
this.watchDogLastRefresh = null
}
}

onServerDing(data) {
Expand Down Expand Up @@ -287,11 +304,15 @@ class PuppetWeb extends Puppet {
return
} else { // browser is alive, and we have a bridge to it
log.verbose('PuppetWeb', 'onServerDisconnect() re-initing bridge')
process.nextTick(() => {
// must use setTimeout to wait a while.
// because the browser has just refreshed, need some time to re-init to ready.
// if the browser is not ready, bridge init will fail,
// caused browser dead and have to be restarted. 2016/6/12
setTimeout(() => {
this.bridge.init()
.then(r => log.verbose('PuppetWeb', 'onServerDisconnect() bridge re-inited: %s', r))
.catch(e => log.error('PuppetWeb', 'onServerDisconnect() exception: [%s]', e))
})
}, 1000)
return
}
}
Expand Down

0 comments on commit 037f39c

Please sign in to comment.