From 3d0da1ea1a934dcc48f3e94cb2c3dddae5a2e51a Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Fri, 10 Feb 2023 11:34:10 +0000 Subject: [PATCH] Add download progress for httpclient method --- packages/cache/src/internal/downloadUtils.ts | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/cache/src/internal/downloadUtils.ts b/packages/cache/src/internal/downloadUtils.ts index 265c34f7..5cee37d7 100644 --- a/packages/cache/src/internal/downloadUtils.ts +++ b/packages/cache/src/internal/downloadUtils.ts @@ -161,6 +161,26 @@ export class DownloadProgress { } } +async function displayDownloadProgress(socket: any, startTime: number): Promise { + while(!socket.complete) { + const transferredBytes = socket.bytesRead + const totalBytes = 100000 + const percentage = (100 * (transferredBytes / totalBytes)).toFixed( + 1 + ) + const elapsedTime = Date.now() - startTime + const downloadSpeed = ( + transferredBytes / + (1024 * 1024) / + (elapsedTime / 1000) + ).toFixed(1) + + core.info( + `Received ${transferredBytes} of ${totalBytes} (${percentage}%), ${downloadSpeed} MBs/sec` + ) + } +} + /** * Download the cache using the Actions toolkit http-client * @@ -171,6 +191,7 @@ export async function downloadCacheHttpClient( archiveLocation: string, archivePath: string ): Promise { + const startTime = Date.now() const writeStream = fs.createWriteStream(archivePath) const httpClient = new HttpClient('actions/cache') const downloadResponse = await retryHttpClientResponse( @@ -184,6 +205,8 @@ export async function downloadCacheHttpClient( core.debug(`Aborting download, socket timed out after ${SocketTimeout} ms`) }) + await displayDownloadProgress(downloadResponse.message.socket, startTime) + await pipeResponseToStream(downloadResponse, writeStream) // Validate download size.