This commit is contained in:
spiduler
2020-10-25 10:43:07 +09:00
parent f88f4c02fe
commit 1858a383d0

View File

@@ -41,15 +41,7 @@ class SeleniumBrowser {
}, },
destroy: (driver) => { destroy: (driver) => {
logger.info('Quitting driver') logger.info('Quitting driver')
return driver.quit().then(() => { return driver.quit().catch(err => {
logger.debug({
spare: this.pool.spareResourceCapacity,
size: this.pool.size,
available: this.pool.available,
borrowed: this.pool.borrowed,
pending: this.pool.pending
}, 'Pool info')
}).catch(err => {
logger.debug({ message: err.message }, 'Quitting error') logger.debug({ message: err.message }, 'Quitting error')
this.pool.release(driver) this.pool.release(driver)
}) })
@@ -65,7 +57,7 @@ class SeleniumBrowser {
}) })
} }
}, { }, {
max: 10, min: 1, testOnBorrow: true max: 2, min: 1, testOnBorrow: true
}); });
this.pool.on('factoryCreateError', function (err) { this.pool.on('factoryCreateError', function (err) {
@@ -75,6 +67,16 @@ class SeleniumBrowser {
this.pool.on('factoryDestroyError', function (err) { this.pool.on('factoryDestroyError', function (err) {
logger.error({ message: err.message }, 'pool error') logger.error({ message: err.message }, 'pool error')
}) })
setInterval(() => {
logger.debug({
spare: this.pool.spareResourceCapacity,
size: this.pool.size,
available: this.pool.available,
borrowed: this.pool.borrowed,
pending: this.pool.pending
}, 'Pool info')
}, 5000)
} }
getDriver() { getDriver() {
@@ -92,20 +94,22 @@ class SeleniumBrowser {
let uri = params.uri let uri = params.uri
logger.info('URL %s', uri) logger.info('URL %s', uri)
if (uri.indexOf('://') > 3) { if (uri.indexOf('://') > 3) {
// if (this.pool) {
return this.pool.acquire() return this.pool.acquire()
.then(async driver => { .then(async driver => {
let html = await driver.get(uri) let html = await driver.get(uri)
.then(() => driver.findElement(By.css('body'))) .then(() => driver.findElement(By.css('body')))
.then(body => body.getAttribute('innerHTML')) .then(body => body.getAttribute('innerHTML')).catch(err => {
return false
})
if(!html) {
this.pool.release(driver)
throw new Error('Init try got Empty html')
}
let htmlPromise = this.isCaptchaPage(html) ? this.resolveCaptcha(driver) : this.getHtml(driver, html, params) let htmlPromise = this.isCaptchaPage(html) ? this.resolveCaptcha(driver) : this.getHtml(driver, html, params)
html = await htmlPromise html = await htmlPromise
this.pool.destroy(driver) this.pool.destroy(driver)
return html return html
}) })
// }else {
// throw new Error('Pool is not ready')
// }
} else { } else {
throw new Error('Invalid URI - ' + uri) throw new Error('Invalid URI - ' + uri)
} }