Unverified Commit 1c72a314 authored by Daz DeBoer's avatar Daz DeBoer
Browse files

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
parent 7dfbe33b
Loading
Loading
Loading
Loading
+1 −1
Changes for .github/workflows/integration-testing-kotlin-dsl.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ on:

env:
  CACHE_KEY_PREFIX: ${{github.workflow}}#${{github.run_number}}-
  CACHE_DEBUG_ENABLED: true

jobs:
  # Use kotlin-dsl project to verify caching of generated jars and compiled scripts
@@ -34,4 +35,3 @@ jobs:
      with:
        build-root-directory: __tests__/samples/kotlin-dsl
        arguments: test --offline
        gradle-cache-enabled: read-only
+18 −7
Changes for src/cache-gradle-user-home.ts: 18 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -50,8 +50,13 @@ export class GradleUserHomeCache extends AbstractCache {
        const processes: Promise<void>[] = []
        for (const markerFile of markerFiles) {
            const p = this.restoreCommonArtifact(markerFile)
            // Run sequentially when debugging enabled
            if (this.cacheDebuggingEnabled) {
                await p
            }
            processes.push(p)
        }

        await Promise.all(processes)
    }

@@ -60,9 +65,6 @@ export class GradleUserHomeCache extends AbstractCache {
            0,
            markerFile.length - MARKER_FILE_EXTENSION.length
        )
        core.debug(
            `Found marker file: ${markerFile}. Will attempt to restore ${artifactFile}`
        )

        if (!fs.existsSync(artifactFile)) {
            const key = path.relative(this.getGradleUserHome(), artifactFile)
@@ -73,20 +75,23 @@ export class GradleUserHomeCache extends AbstractCache {
                cacheKey
            )
            if (restoreKey) {
                core.info(`Restored ${cacheKey} from cache to ${artifactFile}`)
                this.debug(`Restored ${cacheKey} from cache to ${artifactFile}`)
            } else {
                core.warning(
                    `Failed to restore from ${cacheKey} to ${artifactFile}`
                )
            }
        } else {
            core.debug(
            this.debug(
                `Artifact file already exists, not restoring: ${artifactFile}`
            )
        }
    }

    private async reportCacheEntrySize(label: string): Promise<void> {
        if (!this.cacheDebuggingEnabled) {
            return
        }
        const gradleUserHome = path.resolve(os.homedir(), '.gradle')
        if (!fs.existsSync(gradleUserHome)) {
            return
@@ -130,8 +135,13 @@ export class GradleUserHomeCache extends AbstractCache {
        const processes: Promise<void>[] = []
        for (const artifactFile of commonArtifactFiles) {
            const p = this.saveCommonArtifact(artifactFile)
            // Run sequentially when debugging enabled
            if (this.cacheDebuggingEnabled) {
                await p
            }
            processes.push(p)
        }

        await Promise.all(processes)
    }

@@ -144,7 +154,8 @@ export class GradleUserHomeCache extends AbstractCache {
                artifactFile
            )
            const cacheKey = `gradle-artifact-${filePath}`
            core.info(`Caching ${artifactFile} with cache key: ${cacheKey}`)

            this.debug(`Caching ${artifactFile} with cache key: ${cacheKey}`)
            try {
                await cache.saveCache([artifactFile], cacheKey)
            } catch (error) {
@@ -165,7 +176,7 @@ export class GradleUserHomeCache extends AbstractCache {
            // Write the marker file that will stand in place of the original
            fs.writeFileSync(markerFile, 'cached')
        } else {
            core.debug(
            this.debug(
                `Marker file already exists: ${markerFile}. Not caching ${artifactFile}`
            )
        }
+17 −2
Changes for src/cache-utils.ts: 17 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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 {
    // Prefix can be used to force change all cache keys
    const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || ''
@@ -80,11 +84,14 @@ export abstract class AbstractCache {
    private cacheKeyStateKey: string
    private cacheResultStateKey: string

    protected readonly cacheDebuggingEnabled: boolean

    constructor(cacheName: string, cacheDescription: string) {
        this.cacheName = cacheName
        this.cacheDescription = cacheDescription
        this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
        this.cacheResultStateKey = `CACHE_RESULT_${cacheName}`
        this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
    }

    async restore(): Promise<void> {
@@ -122,7 +129,7 @@ export abstract class AbstractCache {

    async save(): Promise<void> {
        if (!this.cacheOutputExists()) {
            core.debug(`No ${this.cacheDescription} to cache.`)
            this.debug(`No ${this.cacheDescription} to cache.`)
            return
        }

@@ -130,7 +137,7 @@ export abstract class AbstractCache {
        const cacheResult = core.getState(this.cacheResultStateKey)

        if (!cacheKey) {
            core.info(
            this.debug(
                `${this.cacheDescription} existed prior to cache restore. Not saving.`
            )
            return
@@ -162,6 +169,14 @@ export abstract class AbstractCache {
        return
    }

    protected debug(message: string): void {
        if (this.cacheDebuggingEnabled) {
            core.info(message)
        } else {
            core.debug(message)
        }
    }

    protected abstract cacheOutputExists(): boolean
    protected abstract getCachePath(): string[]
}