From 1c72a3146334be5fa2f28802367b5bd74ed5983b Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 12 Sep 2021 14:08:22 -0600 Subject: [PATCH] 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 --- .../integration-testing-kotlin-dsl.yml | 2 +- src/cache-gradle-user-home.ts | 25 +++++++++++++------ src/cache-utils.ts | 19 ++++++++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.github/workflows/integration-testing-kotlin-dsl.yml b/.github/workflows/integration-testing-kotlin-dsl.yml index 21492e2..6c05c15 100644 --- a/.github/workflows/integration-testing-kotlin-dsl.yml +++ b/.github/workflows/integration-testing-kotlin-dsl.yml @@ -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 diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index d34308b..0c3a9b1 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -50,8 +50,13 @@ export class GradleUserHomeCache extends AbstractCache { const processes: Promise[] = [] 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 { + 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[] = [] 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}` ) } diff --git a/src/cache-utils.ts b/src/cache-utils.ts index b7e607d..c5346c1 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -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 { @@ -122,7 +129,7 @@ export abstract class AbstractCache { async save(): Promise { 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[] }