Skip to content

Commit 1fdde1d

Browse files
authored
Merge pull request #28 from dscho/try-to-detect-failed-downloads
Try to detect failed downloads and retry gently
2 parents 83f3990 + ff0cb3f commit 1fdde1d

File tree

3 files changed

+72
-35
lines changed

3 files changed

+72
-35
lines changed

dist/index.js

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/downloader.ts

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,33 @@ async function unzip(
6969
}
7070

7171
return new Promise<void>((resolve, reject) => {
72-
https.get(url, (res: Readable): void => {
73-
res
74-
.pipe(unzipper.Parse())
75-
.on('entry', entry => {
76-
if (!entry.path.startsWith(stripPrefix)) {
77-
process.stderr.write(
78-
`warning: skipping ${entry.path} because it does not start with ${stripPrefix}\n`
79-
)
80-
}
81-
const entryPath = `${outputDirectory}/${entry.path.substring(
82-
stripPrefix.length
83-
)}`
84-
progress(entryPath)
85-
if (entryPath.endsWith('/')) {
86-
mkdirp(entryPath.replace(/\/$/, ''))
87-
entry.autodrain()
88-
} else {
89-
entry.pipe(fs.createWriteStream(`${entryPath}`))
90-
}
91-
})
92-
.on('error', reject)
93-
.on('finish', progress)
94-
.on('finish', resolve)
95-
})
72+
https
73+
.get(url, (res: Readable): void => {
74+
res
75+
.on('error', reject)
76+
.pipe(unzipper.Parse())
77+
.on('entry', entry => {
78+
if (!entry.path.startsWith(stripPrefix)) {
79+
process.stderr.write(
80+
`warning: skipping ${entry.path} because it does not start with ${stripPrefix}\n`
81+
)
82+
}
83+
const entryPath = `${outputDirectory}/${entry.path.substring(
84+
stripPrefix.length
85+
)}`
86+
progress(entryPath)
87+
if (entryPath.endsWith('/')) {
88+
mkdirp(entryPath.replace(/\/$/, ''))
89+
entry.autodrain()
90+
} else {
91+
entry.pipe(fs.createWriteStream(`${entryPath}`))
92+
}
93+
})
94+
.on('error', reject)
95+
.on('finish', progress)
96+
.on('finish', resolve)
97+
})
98+
.on('error', reject)
9699
})
97100
}
98101

@@ -237,13 +240,30 @@ export async function get(
237240
)
238241
}
239242
const url = filtered[0].resource.downloadUrl
240-
await unzip(
241-
url,
242-
`${artifactName}/`,
243-
outputDirectory,
244-
verbose,
245-
flavor === 'full' ? unpackTarXZInZipFromURL : undefined
246-
)
243+
let delayInSeconds = 1
244+
for (;;) {
245+
try {
246+
await unzip(
247+
url,
248+
`${artifactName}/`,
249+
outputDirectory,
250+
verbose,
251+
flavor === 'full' ? unpackTarXZInZipFromURL : undefined
252+
)
253+
break
254+
} catch (e) {
255+
delayInSeconds *= 2
256+
if (delayInSeconds >= 60) {
257+
throw e
258+
}
259+
process.stderr.write(
260+
`Encountered problem downloading/extracting ${url}: ${e}; Retrying in ${delayInSeconds} seconds...\n`
261+
)
262+
await new Promise((resolve, _reject) =>
263+
setTimeout(resolve, delayInSeconds * 1000)
264+
)
265+
}
266+
}
247267
}
248268
return {artifactName, download, id}
249269
}

0 commit comments

Comments
 (0)