mirror of
https://github.com/gradle/gradle-build-action
synced 2024-12-18 06:12:23 +00:00
Improve cache-reporting when entry already exists
This commit is contained in:
parent
a23ac1d61c
commit
0a5ede19a9
4 changed files with 34 additions and 23 deletions
|
@ -130,16 +130,13 @@ export class GradleStateCache {
|
||||||
restoreKeys:[${cacheKey.restoreKeys}]`
|
restoreKeys:[${cacheKey.restoreKeys}]`
|
||||||
)
|
)
|
||||||
|
|
||||||
const cacheResult = await restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
|
const cacheResult = await restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys, entryListener)
|
||||||
entryListener.markRequested(cacheKey.key, cacheKey.restoreKeys)
|
|
||||||
|
|
||||||
if (!cacheResult) {
|
if (!cacheResult) {
|
||||||
core.info(`${this.cacheDescription} cache not found. Will initialize empty.`)
|
core.info(`${this.cacheDescription} cache not found. Will initialize empty.`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
core.saveState(this.cacheResultStateKey, cacheResult.key)
|
core.saveState(this.cacheResultStateKey, cacheResult.key)
|
||||||
entryListener.markRestored(cacheResult.key, cacheResult.size)
|
|
||||||
|
|
||||||
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult.key}`)
|
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}`)
|
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKeyFromRestore}`)
|
||||||
const cachePath = this.getCachePath()
|
const cachePath = this.getCachePath()
|
||||||
const savedEntry = await saveCache(cachePath, cacheKeyFromRestore)
|
const entryListener = listener.entry(this.cacheDescription)
|
||||||
|
await saveCache(cachePath, cacheKeyFromRestore, entryListener)
|
||||||
if (savedEntry) {
|
|
||||||
listener.entry(this.cacheDescription).markSaved(savedEntry.key, savedEntry.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,12 +118,9 @@ abstract class AbstractEntryExtractor {
|
||||||
pattern: string,
|
pattern: string,
|
||||||
listener: CacheEntryListener
|
listener: CacheEntryListener
|
||||||
): Promise<ExtractedCacheEntry> {
|
): Promise<ExtractedCacheEntry> {
|
||||||
listener.markRequested(cacheKey)
|
const restoredEntry = await restoreCache([pattern], cacheKey, [], listener)
|
||||||
|
|
||||||
const restoredEntry = await restoreCache([pattern], cacheKey)
|
|
||||||
if (restoredEntry) {
|
if (restoredEntry) {
|
||||||
core.info(`Restored ${artifactType} with key ${cacheKey} to ${pattern}`)
|
core.info(`Restored ${artifactType} with key ${cacheKey} to ${pattern}`)
|
||||||
listener.markRestored(restoredEntry.key, restoredEntry.size)
|
|
||||||
return new ExtractedCacheEntry(artifactType, pattern, cacheKey)
|
return new ExtractedCacheEntry(artifactType, pattern, cacheKey)
|
||||||
} else {
|
} else {
|
||||||
core.info(`Did not restore ${artifactType} with key ${cacheKey} to ${pattern}`)
|
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.`)
|
cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
|
||||||
} else {
|
} else {
|
||||||
core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
|
core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
|
||||||
const savedEntry = await saveCache([pattern], cacheKey)
|
await saveCache([pattern], cacheKey, entryListener)
|
||||||
if (savedEntry !== undefined) {
|
|
||||||
entryListener.markSaved(savedEntry.key, savedEntry.size)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const file of matchingFiles) {
|
for (const file of matchingFiles) {
|
||||||
|
|
|
@ -76,6 +76,12 @@ export class CacheEntryListener {
|
||||||
this.savedSize = size
|
this.savedSize = size
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
markAlreadyExists(key: string): CacheEntryListener {
|
||||||
|
this.savedKey = key
|
||||||
|
this.savedSize = 0
|
||||||
|
return this
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logCachingReport(listener: CacheListener): void {
|
export function logCachingReport(listener: CacheListener): void {
|
||||||
|
@ -112,12 +118,18 @@ function getSum(
|
||||||
cacheEntries: CacheEntryListener[],
|
cacheEntries: CacheEntryListener[],
|
||||||
predicate: (value: CacheEntryListener) => number | undefined
|
predicate: (value: CacheEntryListener) => number | undefined
|
||||||
): string {
|
): string {
|
||||||
|
if (cacheEntries.length === 0) {
|
||||||
|
return '0'
|
||||||
|
}
|
||||||
return formatSize(cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0))
|
return formatSize(cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatSize(bytes: number | undefined): string {
|
function formatSize(bytes: number | undefined): string {
|
||||||
if (bytes === undefined || bytes === 0) {
|
if (bytes === undefined) {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
if (bytes === 0) {
|
||||||
|
return '0 (Entry already exists)'
|
||||||
|
}
|
||||||
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
|
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import * as crypto from 'crypto'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
|
||||||
|
import {CacheEntryListener} from './cache-reporting'
|
||||||
|
|
||||||
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
|
||||||
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
||||||
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
||||||
|
@ -49,23 +51,32 @@ export function hashStrings(values: string[]): string {
|
||||||
export async function restoreCache(
|
export async function restoreCache(
|
||||||
cachePath: string[],
|
cachePath: string[],
|
||||||
cacheKey: string,
|
cacheKey: string,
|
||||||
cacheRestoreKeys: string[] = []
|
cacheRestoreKeys: string[],
|
||||||
|
listener: CacheEntryListener
|
||||||
): Promise<cache.CacheEntry | undefined> {
|
): Promise<cache.CacheEntry | undefined> {
|
||||||
|
listener.markRequested(cacheKey, cacheRestoreKeys)
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveCache(cachePath: string[], cacheKey: string): Promise<cache.CacheEntry | undefined> {
|
export async function saveCache(cachePath: string[], cacheKey: string, listener: CacheEntryListener): Promise<void> {
|
||||||
try {
|
try {
|
||||||
return await cache.saveCache(cachePath, cacheKey)
|
const savedEntry = await cache.saveCache(cachePath, cacheKey)
|
||||||
|
listener.markSaved(savedEntry.key, savedEntry.size)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof cache.ReserveCacheError) {
|
||||||
|
listener.markAlreadyExists(cacheKey)
|
||||||
|
}
|
||||||
handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`)
|
handleCacheFailure(error, `Failed to save cache entry ${cacheKey}`)
|
||||||
}
|
}
|
||||||
return undefined
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cacheDebug(message: string): void {
|
export function cacheDebug(message: string): void {
|
||||||
|
|
Loading…
Reference in a new issue