mirror of https://github.com/actions/toolkit
Merge pull request #1150 from actions/kotewar/segment-download-bug-try-2
Implemented custom promise as abortController isn't able to stop the workflow in real scenariospull/1160/head
commit
a4276ac40f
|
@ -249,12 +249,8 @@ export async function downloadCacheStorageSDK(
|
||||||
|
|
||||||
try {
|
try {
|
||||||
downloadProgress.startDisplayTimer()
|
downloadProgress.startDisplayTimer()
|
||||||
const abortSignal = AbortController.timeout(
|
const controller = new AbortController()
|
||||||
options.segmentTimeoutInMs || 3600000
|
const abortSignal = controller.signal
|
||||||
)
|
|
||||||
abortSignal.addEventListener('abort', () => {
|
|
||||||
core.warning('Aborting cache download as it exceeded the timeout.')
|
|
||||||
})
|
|
||||||
while (!downloadProgress.isDone()) {
|
while (!downloadProgress.isDone()) {
|
||||||
const segmentStart =
|
const segmentStart =
|
||||||
downloadProgress.segmentOffset + downloadProgress.segmentSize
|
downloadProgress.segmentOffset + downloadProgress.segmentSize
|
||||||
|
@ -265,17 +261,22 @@ export async function downloadCacheStorageSDK(
|
||||||
)
|
)
|
||||||
|
|
||||||
downloadProgress.nextSegment(segmentSize)
|
downloadProgress.nextSegment(segmentSize)
|
||||||
|
const result = await promiseWithTimeout(
|
||||||
const result = await client.downloadToBuffer(
|
options.segmentTimeoutInMs || 3600000,
|
||||||
segmentStart,
|
client.downloadToBuffer(segmentStart, segmentSize, {
|
||||||
segmentSize,
|
|
||||||
{
|
|
||||||
abortSignal,
|
abortSignal,
|
||||||
concurrency: options.downloadConcurrency,
|
concurrency: options.downloadConcurrency,
|
||||||
onProgress: downloadProgress.onProgress()
|
onProgress: downloadProgress.onProgress()
|
||||||
}
|
})
|
||||||
)
|
)
|
||||||
fs.writeFileSync(fd, result)
|
if (result === 'timeout') {
|
||||||
|
controller.abort()
|
||||||
|
throw new Error(
|
||||||
|
'Aborting cache download as the download time exceeded the timeout.'
|
||||||
|
)
|
||||||
|
} else if (Buffer.isBuffer(result)) {
|
||||||
|
fs.writeFileSync(fd, result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
downloadProgress.stopDisplayTimer()
|
downloadProgress.stopDisplayTimer()
|
||||||
|
@ -283,3 +284,18 @@ export async function downloadCacheStorageSDK(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const promiseWithTimeout = async (
|
||||||
|
timeoutMs: number,
|
||||||
|
promise: Promise<Buffer>
|
||||||
|
): Promise<unknown> => {
|
||||||
|
let timeoutHandle: NodeJS.Timeout
|
||||||
|
const timeoutPromise = new Promise(resolve => {
|
||||||
|
timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs)
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.race([promise, timeoutPromise]).then(result => {
|
||||||
|
clearTimeout(timeoutHandle)
|
||||||
|
return result
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue