From 0a5ede19a9a89ca7241f65e65b8ed025037b4dc0 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Wed, 19 Jan 2022 12:11:51 -0700 Subject: [PATCH] Improve cache-reporting when entry already exists --- src/cache-base.ts | 12 +++--------- src/cache-extract-entries.ts | 10 ++-------- src/cache-reporting.ts | 14 +++++++++++++- src/cache-utils.ts | 21 ++++++++++++++++----- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/cache-base.ts b/src/cache-base.ts index 4f73821..8bcd3ae 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -130,16 +130,13 @@ export class GradleStateCache { restoreKeys:[${cacheKey.restoreKeys}]` ) - const cacheResult = await restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys) - entryListener.markRequested(cacheKey.key, cacheKey.restoreKeys) - + const cacheResult = await restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys, entryListener) if (!cacheResult) { core.info(`${this.cacheDescription} cache not found. Will initialize empty.`) return } core.saveState(this.cacheResultStateKey, cacheResult.key) - entryListener.markRestored(cacheResult.key, cacheResult.size) core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult.key}`) @@ -186,11 +183,8 @@ export class GradleStateCache { core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKeyFromRestore}`) const cachePath = this.getCachePath() - const savedEntry = await saveCache(cachePath, cacheKeyFromRestore) - - if (savedEntry) { - listener.entry(this.cacheDescription).markSaved(savedEntry.key, savedEntry.size) - } + const entryListener = listener.entry(this.cacheDescription) + await saveCache(cachePath, cacheKeyFromRestore, entryListener) return } diff --git a/src/cache-extract-entries.ts b/src/cache-extract-entries.ts index f35e0b5..5e48ae7 100644 --- a/src/cache-extract-entries.ts +++ b/src/cache-extract-entries.ts @@ -118,12 +118,9 @@ abstract class AbstractEntryExtractor { pattern: string, listener: CacheEntryListener ): Promise { - listener.markRequested(cacheKey) - - const restoredEntry = await restoreCache([pattern], cacheKey) + const restoredEntry = await restoreCache([pattern], cacheKey, [], listener) if (restoredEntry) { core.info(`Restored ${artifactType} with key ${cacheKey} to ${pattern}`) - listener.markRestored(restoredEntry.key, restoredEntry.size) return new ExtractedCacheEntry(artifactType, pattern, cacheKey) } else { core.info(`Did not restore ${artifactType} with key ${cacheKey} to ${pattern}`) @@ -217,10 +214,7 @@ abstract class AbstractEntryExtractor { cacheDebug(`No change to previously restored ${artifactType}. Not saving.`) } else { core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`) - const savedEntry = await saveCache([pattern], cacheKey) - if (savedEntry !== undefined) { - entryListener.markSaved(savedEntry.key, savedEntry.size) - } + await saveCache([pattern], cacheKey, entryListener) } for (const file of matchingFiles) { diff --git a/src/cache-reporting.ts b/src/cache-reporting.ts index e73d390..c7880f9 100644 --- a/src/cache-reporting.ts +++ b/src/cache-reporting.ts @@ -76,6 +76,12 @@ export class CacheEntryListener { this.savedSize = size return this } + + markAlreadyExists(key: string): CacheEntryListener { + this.savedKey = key + this.savedSize = 0 + return this + } } export function logCachingReport(listener: CacheListener): void { @@ -112,12 +118,18 @@ function getSum( cacheEntries: CacheEntryListener[], predicate: (value: CacheEntryListener) => number | undefined ): string { + if (cacheEntries.length === 0) { + return '0' + } return formatSize(cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0)) } function formatSize(bytes: number | undefined): string { - if (bytes === undefined || bytes === 0) { + if (bytes === undefined) { return '' } + if (bytes === 0) { + return '0 (Entry already exists)' + } return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)` } diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 7e4d327..dfc7fd4 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -4,6 +4,8 @@ import * as crypto from 'crypto' import * as path from 'path' import * as fs from 'fs' +import {CacheEntryListener} from './cache-reporting' + const JOB_CONTEXT_PARAMETER = 'workflow-job-context' const CACHE_DISABLED_PARAMETER = 'cache-disabled' const CACHE_READONLY_PARAMETER = 'cache-read-only' @@ -49,23 +51,32 @@ export function hashStrings(values: string[]): string { export async function restoreCache( cachePath: string[], cacheKey: string, - cacheRestoreKeys: string[] = [] + cacheRestoreKeys: string[], + listener: CacheEntryListener ): Promise { + listener.markRequested(cacheKey, cacheRestoreKeys) try { - return await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys) + const restoredEntry = await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys) + if (restoredEntry !== undefined) { + listener.markRestored(restoredEntry.key, restoredEntry.size) + } + return restoredEntry } catch (error) { handleCacheFailure(error, `Failed to restore ${cacheKey}`) return undefined } } -export async function saveCache(cachePath: string[], cacheKey: string): Promise { +export async function saveCache(cachePath: string[], cacheKey: string, listener: CacheEntryListener): Promise { try { - return await cache.saveCache(cachePath, cacheKey) + const savedEntry = await cache.saveCache(cachePath, cacheKey) + listener.markSaved(savedEntry.key, savedEntry.size) } catch (error) { + if (error instanceof cache.ReserveCacheError) { + listener.markAlreadyExists(cacheKey) + } handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`) } - return undefined } export function cacheDebug(message: string): void {