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:
Daz DeBoer 2022-08-16 15:29:23 -06:00
parent cbebff71e9
commit b49446f8e1
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
4 changed files with 24 additions and 10 deletions

View file

@ -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

View file

@ -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)

View file

@ -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(

View file

@ -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}`)
}