Adding 5s sleep between retry and fixing retry logic.

This commit is contained in:
Vahagn Khachatryan
2019-02-03 08:55:57 +00:00
parent 086cd4c72b
commit 5365385bd5

View File

@@ -1,4 +1,9 @@
var concurrency = 5 var concurrency = 5
var retry_delay = 15/* seconds */
function sleep(delay) {
return new Promise((resolve) => setTimeout(resolve, delay*1000));
}
function getCurrentTab() { function getCurrentTab() {
console.debug("Querying active tab."); console.debug("Querying active tab.");
@@ -65,7 +70,8 @@ class Book{
.catch((error)=>{ .catch((error)=>{
if (!retry){ if (!retry){
console.warn(`Failed ${url} - ${error.message}`) console.warn(`Failed ${url} - ${error.message}`)
return this.downloadResource(url, true) return sleep(retry_delay)
.then(()=>{return this.downloadResource(url, true)})
} else { } else {
console.error(`Giving up on ${url} - ${error.message}`) console.error(`Giving up on ${url} - ${error.message}`)
throw `Failed second try on ${url} - ${error.message}` throw `Failed second try on ${url} - ${error.message}`
@@ -76,7 +82,8 @@ class Book{
return res return res
} else if (!retry){ } else if (!retry){
console.warn(`Failed ${url} - ${res.status} - ${res.statusText}`) console.warn(`Failed ${url} - ${res.status} - ${res.statusText}`)
return this.downloadResource(url, true) return sleep(retry_delay)
.then(()=>{return this.downloadResource(url, true)})
} else { } else {
console.error(`Giving up on ${url} - ${res.status} - ${res.statusText}`) console.error(`Giving up on ${url} - ${res.status} - ${res.statusText}`)
throw `Failed second try on ${url} - ${res.status} - ${res.statusText}` throw `Failed second try on ${url} - ${res.status} - ${res.statusText}`
@@ -174,22 +181,26 @@ class Book{
downloadContent(){ downloadContent(){
return Promise.map(Object.keys(this.book_files), (url) => { return Promise.map(Object.keys(this.book_files), (url) => {
return this.downloadAndReport(url, this.book_files[url].filename) return this.downloadContentFile(url)
.then((res) => {
if (res.ok){
this.book_files[url].headers = res.headers
this.book_files[url].mime = res.headers.get('Content-Type')
return res.arrayBuffer().then((arrBuffer)=>{
this.book_files[url].body = arrBuffer
})
}
})
.catch((error)=>{
throw `Failed to download book content: "${error}"`
})
},{concurrency: concurrency}) },{concurrency: concurrency})
} }
downloadContentFile(url){
return this.downloadAndReport(url, this.book_files[url].filename)
.then((res) => {
if (res.ok){
this.book_files[url].headers = res.headers
this.book_files[url].mime = res.headers.get('Content-Type')
return res.arrayBuffer().then((arrBuffer)=>{
this.book_files[url].body = arrBuffer
})
}
})
.catch((error)=>{
throw `Failed to download book content: "${error}"`
})
}
insertBookFile(url, filename){ insertBookFile(url, filename){
this.book_files[url] = { this.book_files[url] = {
url: url, url: url,