Allow cache debug logging to be selectively enabled

- Move more messages out of main 'info' log
- Only log cache entry size when debugging
- Process cache entries sequentially when debugging
This commit is contained in:
Daz DeBoer 2021-09-12 14:08:22 -06:00
parent 7dfbe33bba
commit 1c72a31463
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
3 changed files with 36 additions and 10 deletions

View File

@ -8,6 +8,7 @@ on:
env: env:
CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}- CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-
CACHE_DEBUG_ENABLED: true
jobs: jobs:
# Use kotlin-dsl project to verify caching of generated jars and compiled scripts # Use kotlin-dsl project to verify caching of generated jars and compiled scripts
@ -34,4 +35,3 @@ jobs:
with: with:
build-root-directory: __tests__/samples/kotlin-dsl build-root-directory: __tests__/samples/kotlin-dsl
arguments: test --offline arguments: test --offline
gradle-cache-enabled: read-only

View File

@ -50,8 +50,13 @@ export class GradleUserHomeCache extends AbstractCache {
const processes: Promise<void>[] = [] const processes: Promise<void>[] = []
for (const markerFile of markerFiles) { for (const markerFile of markerFiles) {
const p = this.restoreCommonArtifact(markerFile) const p = this.restoreCommonArtifact(markerFile)
// Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) {
await p
}
processes.push(p) processes.push(p)
} }
await Promise.all(processes) await Promise.all(processes)
} }
@ -60,9 +65,6 @@ export class GradleUserHomeCache extends AbstractCache {
0, 0,
markerFile.length - MARKER_FILE_EXTENSION.length markerFile.length - MARKER_FILE_EXTENSION.length
) )
core.debug(
`Found marker file: ${markerFile}. Will attempt to restore ${artifactFile}`
)
if (!fs.existsSync(artifactFile)) { if (!fs.existsSync(artifactFile)) {
const key = path.relative(this.getGradleUserHome(), artifactFile) const key = path.relative(this.getGradleUserHome(), artifactFile)
@ -73,20 +75,23 @@ export class GradleUserHomeCache extends AbstractCache {
cacheKey cacheKey
) )
if (restoreKey) { if (restoreKey) {
core.info(`Restored ${cacheKey} from cache to ${artifactFile}`) this.debug(`Restored ${cacheKey} from cache to ${artifactFile}`)
} else { } else {
core.warning( core.warning(
`Failed to restore from ${cacheKey} to ${artifactFile}` `Failed to restore from ${cacheKey} to ${artifactFile}`
) )
} }
} else { } else {
core.debug( this.debug(
`Artifact file already exists, not restoring: ${artifactFile}` `Artifact file already exists, not restoring: ${artifactFile}`
) )
} }
} }
private async reportCacheEntrySize(label: string): Promise<void> { private async reportCacheEntrySize(label: string): Promise<void> {
if (!this.cacheDebuggingEnabled) {
return
}
const gradleUserHome = path.resolve(os.homedir(), '.gradle') const gradleUserHome = path.resolve(os.homedir(), '.gradle')
if (!fs.existsSync(gradleUserHome)) { if (!fs.existsSync(gradleUserHome)) {
return return
@ -130,8 +135,13 @@ export class GradleUserHomeCache extends AbstractCache {
const processes: Promise<void>[] = [] const processes: Promise<void>[] = []
for (const artifactFile of commonArtifactFiles) { for (const artifactFile of commonArtifactFiles) {
const p = this.saveCommonArtifact(artifactFile) const p = this.saveCommonArtifact(artifactFile)
// Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) {
await p
}
processes.push(p) processes.push(p)
} }
await Promise.all(processes) await Promise.all(processes)
} }
@ -144,7 +154,8 @@ export class GradleUserHomeCache extends AbstractCache {
artifactFile artifactFile
) )
const cacheKey = `gradle-artifact-${filePath}` const cacheKey = `gradle-artifact-${filePath}`
core.info(`Caching ${artifactFile} with cache key: ${cacheKey}`)
this.debug(`Caching ${artifactFile} with cache key: ${cacheKey}`)
try { try {
await cache.saveCache([artifactFile], cacheKey) await cache.saveCache([artifactFile], cacheKey)
} catch (error) { } catch (error) {
@ -165,7 +176,7 @@ export class GradleUserHomeCache extends AbstractCache {
// Write the marker file that will stand in place of the original // Write the marker file that will stand in place of the original
fs.writeFileSync(markerFile, 'cached') fs.writeFileSync(markerFile, 'cached')
} else { } else {
core.debug( this.debug(
`Marker file already exists: ${markerFile}. Not caching ${artifactFile}` `Marker file already exists: ${markerFile}. Not caching ${artifactFile}`
) )
} }

View File

@ -26,6 +26,10 @@ function getCacheEnabledValue(cacheName: string): string {
) )
} }
export function isCacheDebuggingEnabled(): boolean {
return process.env['CACHE_DEBUG_ENABLED'] ? true : false
}
function generateCacheKey(cacheName: string): CacheKey { function generateCacheKey(cacheName: string): CacheKey {
// Prefix can be used to force change all cache keys // Prefix can be used to force change all cache keys
const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || '' const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || ''
@ -80,11 +84,14 @@ export abstract class AbstractCache {
private cacheKeyStateKey: string private cacheKeyStateKey: string
private cacheResultStateKey: string private cacheResultStateKey: string
protected readonly cacheDebuggingEnabled: boolean
constructor(cacheName: string, cacheDescription: string) { constructor(cacheName: string, cacheDescription: string) {
this.cacheName = cacheName this.cacheName = cacheName
this.cacheDescription = cacheDescription this.cacheDescription = cacheDescription
this.cacheKeyStateKey = `CACHE_KEY_${cacheName}` this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
this.cacheResultStateKey = `CACHE_RESULT_${cacheName}` this.cacheResultStateKey = `CACHE_RESULT_${cacheName}`
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
} }
async restore(): Promise<void> { async restore(): Promise<void> {
@ -122,7 +129,7 @@ export abstract class AbstractCache {
async save(): Promise<void> { async save(): Promise<void> {
if (!this.cacheOutputExists()) { if (!this.cacheOutputExists()) {
core.debug(`No ${this.cacheDescription} to cache.`) this.debug(`No ${this.cacheDescription} to cache.`)
return return
} }
@ -130,7 +137,7 @@ export abstract class AbstractCache {
const cacheResult = core.getState(this.cacheResultStateKey) const cacheResult = core.getState(this.cacheResultStateKey)
if (!cacheKey) { if (!cacheKey) {
core.info( this.debug(
`${this.cacheDescription} existed prior to cache restore. Not saving.` `${this.cacheDescription} existed prior to cache restore. Not saving.`
) )
return return
@ -162,6 +169,14 @@ export abstract class AbstractCache {
return return
} }
protected debug(message: string): void {
if (this.cacheDebuggingEnabled) {
core.info(message)
} else {
core.debug(message)
}
}
protected abstract cacheOutputExists(): boolean protected abstract cacheOutputExists(): boolean
protected abstract getCachePath(): string[] protected abstract getCachePath(): string[]
} }