Unverified Commit b49446f8 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Improved cache reporting

- Fix count of saved entries
- Catch and report errors on save and restore
- Correctly report entries that are never requested
parent cbebff71
Loading
Loading
Loading
Loading
+2 −2
Changes for src/cache-base.ts: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -101,9 +101,9 @@ export class GradleStateCache {

            for (const entryListener of listener.cacheEntries) {
                if (entryListener === gradleHomeEntryListener) {
                    entryListener.markUnsaved('cache key not changed')
                    entryListener.markNotSaved('cache key not changed')
                } else {
                    entryListener.markUnsaved(`referencing '${this.cacheDescription}' cache entry not saved`)
                    entryListener.markNotSaved(`referencing '${this.cacheDescription}' cache entry not saved`)
                }
            }
            return
+1 −1
Changes for src/cache-extract-entries.ts: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ abstract class AbstractEntryExtractor {

        if (previouslyRestoredKey === cacheKey) {
            cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
            entryListener.markUnsaved('contents unchanged')
            entryListener.markNotSaved('contents unchanged')
        } else {
            core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
            await saveCache([pattern], cacheKey, entryListener)
+18 −7
Changes for src/cache-reporting.ts: 18 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -62,11 +62,11 @@ export class CacheEntryListener {
    requestedRestoreKeys: string[] | undefined
    restoredKey: string | undefined
    restoredSize: number | undefined
    notRestored: string | undefined

    savedKey: string | undefined
    savedSize: number | undefined

    unsaved: string | undefined
    notSaved: string | undefined

    constructor(entryName: string) {
        this.entryName = entryName
@@ -88,6 +88,11 @@ export class CacheEntryListener {
        return this
    }

    markNotRestored(message: string): CacheEntryListener {
        this.notRestored = message
        return this
    }

    markSaved(key: string, size: number | undefined): CacheEntryListener {
        this.savedKey = key
        this.savedSize = size
@@ -100,8 +105,8 @@ export class CacheEntryListener {
        return this
    }

    markUnsaved(message: string): CacheEntryListener {
        this.unsaved = message
    markNotSaved(message: string): CacheEntryListener {
        this.notSaved = message
        return this
    }
}
@@ -166,9 +171,15 @@ function renderEntryDetails(listener: CacheListener): string {
}

function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean): string {
    if (entry.notRestored) {
        return `(Entry not restored: ${entry.notRestored})`
    }
    if (cacheWriteOnly) {
        return '(Entry not restored: cache is write-only)'
    }
    if (entry.requestedKey === undefined) {
        return '(Entry not restored: not requested)'
    }
    if (entry.restoredKey === undefined) {
        return '(Entry not restored: no match found)'
    }
@@ -179,8 +190,8 @@ function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean):
}

function getSavedMessage(entry: CacheEntryListener, cacheReadOnly: boolean): string {
    if (entry.unsaved) {
        return `(Entry not saved: ${entry.unsaved})`
    if (entry.notSaved) {
        return `(Entry not saved: ${entry.notSaved})`
    }
    if (entry.savedKey === undefined) {
        if (cacheReadOnly) {
@@ -198,7 +209,7 @@ function getCount(
    cacheEntries: CacheEntryListener[],
    predicate: (value: CacheEntryListener) => number | undefined
): number {
    return cacheEntries.filter(e => predicate(e) !== undefined).length
    return cacheEntries.filter(e => predicate(e)).length
}

function getSize(
+3
Changes for src/cache-utils.ts: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -154,6 +154,7 @@ export async function restoreCache(
        }
        return restoredEntry
    } catch (error) {
        listener.markNotRestored((error as Error).message)
        handleCacheFailure(error, `Failed to restore ${cacheKey}`)
        return undefined
    }
@@ -166,6 +167,8 @@ export async function saveCache(cachePath: string[], cacheKey: string, listener:
    } catch (error) {
        if (error instanceof cache.ReserveCacheError) {
            listener.markAlreadyExists(cacheKey)
        } else {
            listener.markNotSaved((error as Error).message)
        }
        handleCacheFailure(error, `Failed to save cache entry with path '${cachePath}' and key: ${cacheKey}`)
    }