mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-11-04 09:50:23 +08:00 
			
		
		
		
	Improved cache reporting
- Fix count of saved entries - Catch and report errors on save and restore - Correctly report entries that are never requested
This commit is contained in:
		@@ -101,9 +101,9 @@ export class GradleStateCache {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            for (const entryListener of listener.cacheEntries) {
 | 
					            for (const entryListener of listener.cacheEntries) {
 | 
				
			||||||
                if (entryListener === gradleHomeEntryListener) {
 | 
					                if (entryListener === gradleHomeEntryListener) {
 | 
				
			||||||
                    entryListener.markUnsaved('cache key not changed')
 | 
					                    entryListener.markNotSaved('cache key not changed')
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    entryListener.markUnsaved(`referencing '${this.cacheDescription}' cache entry not saved`)
 | 
					                    entryListener.markNotSaved(`referencing '${this.cacheDescription}' cache entry not saved`)
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -213,7 +213,7 @@ abstract class AbstractEntryExtractor {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if (previouslyRestoredKey === cacheKey) {
 | 
					        if (previouslyRestoredKey === cacheKey) {
 | 
				
			||||||
            cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
 | 
					            cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
 | 
				
			||||||
            entryListener.markUnsaved('contents unchanged')
 | 
					            entryListener.markNotSaved('contents unchanged')
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
 | 
					            core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
 | 
				
			||||||
            await saveCache([pattern], cacheKey, entryListener)
 | 
					            await saveCache([pattern], cacheKey, entryListener)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,11 +62,11 @@ export class CacheEntryListener {
 | 
				
			|||||||
    requestedRestoreKeys: string[] | undefined
 | 
					    requestedRestoreKeys: string[] | undefined
 | 
				
			||||||
    restoredKey: string | undefined
 | 
					    restoredKey: string | undefined
 | 
				
			||||||
    restoredSize: number | undefined
 | 
					    restoredSize: number | undefined
 | 
				
			||||||
 | 
					    notRestored: string | undefined
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    savedKey: string | undefined
 | 
					    savedKey: string | undefined
 | 
				
			||||||
    savedSize: number | undefined
 | 
					    savedSize: number | undefined
 | 
				
			||||||
 | 
					    notSaved: string | undefined
 | 
				
			||||||
    unsaved: string | undefined
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor(entryName: string) {
 | 
					    constructor(entryName: string) {
 | 
				
			||||||
        this.entryName = entryName
 | 
					        this.entryName = entryName
 | 
				
			||||||
@@ -88,6 +88,11 @@ export class CacheEntryListener {
 | 
				
			|||||||
        return this
 | 
					        return this
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    markNotRestored(message: string): CacheEntryListener {
 | 
				
			||||||
 | 
					        this.notRestored = message
 | 
				
			||||||
 | 
					        return this
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    markSaved(key: string, size: number | undefined): CacheEntryListener {
 | 
					    markSaved(key: string, size: number | undefined): CacheEntryListener {
 | 
				
			||||||
        this.savedKey = key
 | 
					        this.savedKey = key
 | 
				
			||||||
        this.savedSize = size
 | 
					        this.savedSize = size
 | 
				
			||||||
@@ -100,8 +105,8 @@ export class CacheEntryListener {
 | 
				
			|||||||
        return this
 | 
					        return this
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    markUnsaved(message: string): CacheEntryListener {
 | 
					    markNotSaved(message: string): CacheEntryListener {
 | 
				
			||||||
        this.unsaved = message
 | 
					        this.notSaved = message
 | 
				
			||||||
        return this
 | 
					        return this
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -166,9 +171,15 @@ function renderEntryDetails(listener: CacheListener): string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean): string {
 | 
					function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean): string {
 | 
				
			||||||
 | 
					    if (entry.notRestored) {
 | 
				
			||||||
 | 
					        return `(Entry not restored: ${entry.notRestored})`
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (cacheWriteOnly) {
 | 
					    if (cacheWriteOnly) {
 | 
				
			||||||
        return '(Entry not restored: cache is write-only)'
 | 
					        return '(Entry not restored: cache is write-only)'
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    if (entry.requestedKey === undefined) {
 | 
				
			||||||
 | 
					        return '(Entry not restored: not requested)'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (entry.restoredKey === undefined) {
 | 
					    if (entry.restoredKey === undefined) {
 | 
				
			||||||
        return '(Entry not restored: no match found)'
 | 
					        return '(Entry not restored: no match found)'
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -179,8 +190,8 @@ function getRestoredMessage(entry: CacheEntryListener, cacheWriteOnly: boolean):
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getSavedMessage(entry: CacheEntryListener, cacheReadOnly: boolean): string {
 | 
					function getSavedMessage(entry: CacheEntryListener, cacheReadOnly: boolean): string {
 | 
				
			||||||
    if (entry.unsaved) {
 | 
					    if (entry.notSaved) {
 | 
				
			||||||
        return `(Entry not saved: ${entry.unsaved})`
 | 
					        return `(Entry not saved: ${entry.notSaved})`
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (entry.savedKey === undefined) {
 | 
					    if (entry.savedKey === undefined) {
 | 
				
			||||||
        if (cacheReadOnly) {
 | 
					        if (cacheReadOnly) {
 | 
				
			||||||
@@ -198,7 +209,7 @@ function getCount(
 | 
				
			|||||||
    cacheEntries: CacheEntryListener[],
 | 
					    cacheEntries: CacheEntryListener[],
 | 
				
			||||||
    predicate: (value: CacheEntryListener) => number | undefined
 | 
					    predicate: (value: CacheEntryListener) => number | undefined
 | 
				
			||||||
): number {
 | 
					): number {
 | 
				
			||||||
    return cacheEntries.filter(e => predicate(e) !== undefined).length
 | 
					    return cacheEntries.filter(e => predicate(e)).length
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getSize(
 | 
					function getSize(
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -154,6 +154,7 @@ export async function restoreCache(
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return restoredEntry
 | 
					        return restoredEntry
 | 
				
			||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					        listener.markNotRestored((error as Error).message)
 | 
				
			||||||
        handleCacheFailure(error, `Failed to restore ${cacheKey}`)
 | 
					        handleCacheFailure(error, `Failed to restore ${cacheKey}`)
 | 
				
			||||||
        return undefined
 | 
					        return undefined
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -166,6 +167,8 @@ export async function saveCache(cachePath: string[], cacheKey: string, listener:
 | 
				
			|||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
        if (error instanceof cache.ReserveCacheError) {
 | 
					        if (error instanceof cache.ReserveCacheError) {
 | 
				
			||||||
            listener.markAlreadyExists(cacheKey)
 | 
					            listener.markAlreadyExists(cacheKey)
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            listener.markNotSaved((error as Error).message)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        handleCacheFailure(error, `Failed to save cache entry with path '${cachePath}' and key: ${cacheKey}`)
 | 
					        handleCacheFailure(error, `Failed to save cache entry with path '${cachePath}' and key: ${cacheKey}`)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user