Rename 'report' classes to 'listener'

- `CachingReport` -> `CacheListener`
- `CacheEntryReport` -> `CacheEntryListener`
This commit is contained in:
Daz DeBoer 2021-10-30 07:21:27 -06:00
parent c317ccac62
commit 75cec40e58
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
4 changed files with 93 additions and 89 deletions

View file

@ -1,90 +1,90 @@
import {CacheEntryReport, CachingReport} from '../src/cache-base' import {CacheEntryListener, CacheListener} from '../src/cache-base'
describe('caching report', () => { describe('caching report', () => {
describe('reports not fully restored', () => { describe('reports not fully restored', () => {
it('with one requested entry report', async () => { it('with one requested entry report', async () => {
const report = new CachingReport() const report = new CacheListener()
report.entryReport('foo').markRequested('1', ['2']) report.entry('foo').markRequested('1', ['2'])
report.entryReport('bar').markRequested('3').markRestored('4') report.entry('bar').markRequested('3').markRestored('4')
expect(report.fullyRestored).toBe(false) expect(report.fullyRestored).toBe(false)
}) })
}) })
describe('reports fully restored', () => { describe('reports fully restored', () => {
it('when empty', async () => { it('when empty', async () => {
const report = new CachingReport() const report = new CacheListener()
expect(report.fullyRestored).toBe(true) expect(report.fullyRestored).toBe(true)
}) })
it('with empty entry reports', async () => { it('with empty entry reports', async () => {
const report = new CachingReport() const report = new CacheListener()
report.entryReport('foo') report.entry('foo')
report.entryReport('bar') report.entry('bar')
expect(report.fullyRestored).toBe(true) expect(report.fullyRestored).toBe(true)
}) })
it('with restored entry report', async () => { it('with restored entry report', async () => {
const report = new CachingReport() const report = new CacheListener()
report.entryReport('bar').markRequested('3').markRestored('4') report.entry('bar').markRequested('3').markRestored('4')
expect(report.fullyRestored).toBe(true) expect(report.fullyRestored).toBe(true)
}) })
it('with multiple restored entry reportss', async () => { it('with multiple restored entry reportss', async () => {
const report = new CachingReport() const report = new CacheListener()
report.entryReport('foo').markRestored('4') report.entry('foo').markRestored('4')
report.entryReport('bar').markRequested('3').markRestored('4') report.entry('bar').markRequested('3').markRestored('4')
expect(report.fullyRestored).toBe(true) expect(report.fullyRestored).toBe(true)
}) })
}) })
describe('can be stringified and rehydrated', () => { describe('can be stringified and rehydrated', () => {
it('when empty', async () => { it('when empty', async () => {
const report = new CachingReport() const report = new CacheListener()
const stringRep = report.stringify() const stringRep = report.stringify()
const reportClone: CachingReport = CachingReport.rehydrate(stringRep) const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
expect(reportClone.cacheEntryReports).toEqual([]) expect(reportClone.cacheEntries).toEqual([])
// Can call methods on rehydrated // Can call methods on rehydrated
expect(reportClone.entryReport('foo')).toBeInstanceOf(CacheEntryReport) expect(reportClone.entry('foo')).toBeInstanceOf(CacheEntryListener)
}) })
it('with entry reports', async () => { it('with entry reports', async () => {
const report = new CachingReport() const report = new CacheListener()
report.entryReport('foo') report.entry('foo')
report.entryReport('bar') report.entry('bar')
report.entryReport('baz') report.entry('baz')
const stringRep = report.stringify() const stringRep = report.stringify()
const reportClone: CachingReport = CachingReport.rehydrate(stringRep) const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
expect(reportClone.cacheEntryReports.length).toBe(3) expect(reportClone.cacheEntries.length).toBe(3)
expect(reportClone.cacheEntryReports[0].entryName).toBe('foo') expect(reportClone.cacheEntries[0].entryName).toBe('foo')
expect(reportClone.cacheEntryReports[1].entryName).toBe('bar') expect(reportClone.cacheEntries[1].entryName).toBe('bar')
expect(reportClone.cacheEntryReports[2].entryName).toBe('baz') expect(reportClone.cacheEntries[2].entryName).toBe('baz')
expect(reportClone.entryReport('foo')).toBe(reportClone.cacheEntryReports[0]) expect(reportClone.entry('foo')).toBe(reportClone.cacheEntries[0])
}) })
it('with rehydrated entry report', async () => { it('with rehydrated entry report', async () => {
const report = new CachingReport() const report = new CacheListener()
const entryReport = report.entryReport('foo') const entryReport = report.entry('foo')
entryReport.markRequested('1', ['2', '3']) entryReport.markRequested('1', ['2', '3'])
entryReport.markSaved('4') entryReport.markSaved('4')
const stringRep = report.stringify() const stringRep = report.stringify()
const reportClone: CachingReport = CachingReport.rehydrate(stringRep) const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
const entryClone = reportClone.entryReport('foo') const entryClone = reportClone.entry('foo')
expect(entryClone.requestedKey).toBe('1') expect(entryClone.requestedKey).toBe('1')
expect(entryClone.requestedRestoreKeys).toEqual(['2', '3']) expect(entryClone.requestedRestoreKeys).toEqual(['2', '3'])
expect(entryClone.savedKey).toBe('4') expect(entryClone.savedKey).toBe('4')
}) })
it('with live entry report', async () => { it('with live entry report', async () => {
const report = new CachingReport() const report = new CacheListener()
const entryReport = report.entryReport('foo') const entryReport = report.entry('foo')
entryReport.markRequested('1', ['2', '3']) entryReport.markRequested('1', ['2', '3'])
const stringRep = report.stringify() const stringRep = report.stringify()
const reportClone: CachingReport = CachingReport.rehydrate(stringRep) const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
const entryClone = reportClone.entryReport('foo') const entryClone = reportClone.entry('foo')
// Check type and call method on rehydrated entry report // Check type and call method on rehydrated entry report
expect(entryClone).toBeInstanceOf(CacheEntryReport) expect(entryClone).toBeInstanceOf(CacheEntryListener)
entryClone.markSaved('4') entryClone.markSaved('4')
expect(entryClone.requestedKey).toBe('1') expect(entryClone.requestedKey).toBe('1')

View file

@ -40,41 +40,41 @@ class CacheKey {
} }
} }
export class CachingReport { export class CacheListener {
cacheEntryReports: CacheEntryReport[] = [] cacheEntries: CacheEntryListener[] = []
get fullyRestored(): boolean { get fullyRestored(): boolean {
return this.cacheEntryReports.every(x => !x.wasRequestedButNotRestored()) return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
} }
entryReport(name: string): CacheEntryReport { entry(name: string): CacheEntryListener {
for (const report of this.cacheEntryReports) { for (const entry of this.cacheEntries) {
if (report.entryName === name) { if (entry.entryName === name) {
return report return entry
} }
} }
const newReport = new CacheEntryReport(name) const newEntry = new CacheEntryListener(name)
this.cacheEntryReports.push(newReport) this.cacheEntries.push(newEntry)
return newReport return newEntry
} }
stringify(): string { stringify(): string {
return JSON.stringify(this) return JSON.stringify(this)
} }
static rehydrate(stringRep: string): CachingReport { static rehydrate(stringRep: string): CacheListener {
const rehydrated: CachingReport = Object.assign(new CachingReport(), JSON.parse(stringRep)) const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
const entryReports = rehydrated.cacheEntryReports const entries = rehydrated.cacheEntries
for (let index = 0; index < entryReports.length; index++) { for (let index = 0; index < entries.length; index++) {
const rawEntryReport = entryReports[index] const rawEntry = entries[index]
entryReports[index] = Object.assign(new CacheEntryReport(rawEntryReport.entryName), rawEntryReport) entries[index] = Object.assign(new CacheEntryListener(rawEntry.entryName), rawEntry)
} }
return rehydrated return rehydrated
} }
} }
export class CacheEntryReport { export class CacheEntryListener {
entryName: string entryName: string
requestedKey: string | undefined requestedKey: string | undefined
requestedRestoreKeys: string[] | undefined requestedRestoreKeys: string[] | undefined
@ -92,18 +92,18 @@ export class CacheEntryReport {
return this.requestedKey !== undefined && this.restoredKey === undefined return this.requestedKey !== undefined && this.restoredKey === undefined
} }
markRequested(key: string, restoreKeys: string[] = []): CacheEntryReport { markRequested(key: string, restoreKeys: string[] = []): CacheEntryListener {
this.requestedKey = key this.requestedKey = key
this.requestedRestoreKeys = restoreKeys this.requestedRestoreKeys = restoreKeys
return this return this
} }
markRestored(key: string): CacheEntryReport { markRestored(key: string): CacheEntryListener {
this.restoredKey = key this.restoredKey = key
return this return this
} }
markSaved(key: string): CacheEntryReport { markSaved(key: string): CacheEntryListener {
this.savedKey = key this.savedKey = key
return this return this
} }
@ -125,14 +125,14 @@ export abstract class AbstractCache {
this.cacheDebuggingEnabled = isCacheDebuggingEnabled() this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
} }
async restore(report: CachingReport): Promise<void> { async restore(listener: CacheListener): Promise<void> {
if (this.cacheOutputExists()) { if (this.cacheOutputExists()) {
core.info(`${this.cacheDescription} already exists. Not restoring from cache.`) core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
return return
} }
const cacheKey = this.prepareCacheKey() const cacheKey = this.prepareCacheKey()
const entryReport = report.entryReport(this.cacheName) const entryReport = listener.entry(this.cacheName)
entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys) entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)
this.debug( this.debug(
@ -153,7 +153,7 @@ export abstract class AbstractCache {
core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`) core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`)
try { try {
await this.afterRestore(report) await this.afterRestore(listener)
} catch (error) { } catch (error) {
core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`) core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
} }
@ -184,9 +184,9 @@ export abstract class AbstractCache {
} }
} }
protected async afterRestore(_report: CachingReport): Promise<void> {} protected async afterRestore(_listener: CacheListener): Promise<void> {}
async save(report: CachingReport): Promise<void> { async save(listener: CacheListener): Promise<void> {
if (!this.cacheOutputExists()) { if (!this.cacheOutputExists()) {
this.debug(`No ${this.cacheDescription} to cache.`) this.debug(`No ${this.cacheDescription} to cache.`)
return return
@ -206,7 +206,7 @@ export abstract class AbstractCache {
} }
try { try {
await this.beforeSave(report) await this.beforeSave(listener)
} catch (error) { } catch (error) {
core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`) core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
return return
@ -216,12 +216,12 @@ export abstract class AbstractCache {
const cachePath = this.getCachePath() const cachePath = this.getCachePath()
await this.saveCache(cachePath, cacheKey) await this.saveCache(cachePath, cacheKey)
report.entryReport(this.cacheName).markSaved(cacheKey) listener.entry(this.cacheName).markSaved(cacheKey)
return return
} }
protected async beforeSave(_report: CachingReport): Promise<void> {} protected async beforeSave(_listener: CacheListener): Promise<void> {}
protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> { protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> {
try { try {

View file

@ -5,7 +5,7 @@ import * as core from '@actions/core'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import {AbstractCache, CacheEntryReport, CachingReport} from './cache-base' import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base'
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils' import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'
const META_FILE_DIR = '.gradle-build-action' const META_FILE_DIR = '.gradle-build-action'
@ -22,13 +22,13 @@ export class GradleUserHomeCache extends AbstractCache {
this.gradleUserHome = this.determineGradleUserHome(rootDir) this.gradleUserHome = this.determineGradleUserHome(rootDir)
} }
async afterRestore(report: CachingReport): Promise<void> { async afterRestore(listener: CacheListener): Promise<void> {
await this.reportGradleUserHomeSize('as restored from cache') await this.reportGradleUserHomeSize('as restored from cache')
await this.restoreArtifactBundles(report) await this.restoreArtifactBundles(listener)
await this.reportGradleUserHomeSize('after restoring common artifacts') await this.reportGradleUserHomeSize('after restoring common artifacts')
} }
private async restoreArtifactBundles(report: CachingReport): Promise<void> { private async restoreArtifactBundles(listener: CacheListener): Promise<void> {
const processes: Promise<void>[] = [] const processes: Promise<void>[] = []
const bundleMetaFiles = await this.getBundleMetaFiles() const bundleMetaFiles = await this.getBundleMetaFiles()
@ -37,16 +37,16 @@ export class GradleUserHomeCache extends AbstractCache {
// Iterate over all bundle meta files and try to restore // Iterate over all bundle meta files and try to restore
for (const bundleMetaFile of bundleMetaFiles) { for (const bundleMetaFile of bundleMetaFiles) {
const bundle = path.basename(bundleMetaFile, '.cache') const bundle = path.basename(bundleMetaFile, '.cache')
const bundleEntryReport = report.entryReport(bundle) const entryListener = listener.entry(bundle)
const bundlePattern = bundlePatterns.get(bundle) const bundlePattern = bundlePatterns.get(bundle)
// Handle case where the 'artifactBundlePatterns' have been changed // Handle case where the 'artifactBundlePatterns' have been changed
if (bundlePattern === undefined) { if (bundlePattern === undefined) {
core.info(`Found bundle metafile for ${bundle} but no such bundle defined`) core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
bundleEntryReport.markRequested('BUNDLE_NOT_CONFIGURED') entryListener.markRequested('BUNDLE_NOT_CONFIGURED')
tryDelete(bundleMetaFile) tryDelete(bundleMetaFile)
} else { } else {
const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, bundleEntryReport) const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, entryListener)
// Run sequentially when debugging enabled // Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) { if (this.cacheDebuggingEnabled) {
await p await p
@ -62,15 +62,15 @@ export class GradleUserHomeCache extends AbstractCache {
bundle: string, bundle: string,
bundlePattern: string, bundlePattern: string,
bundleMetaFile: string, bundleMetaFile: string,
report: CacheEntryReport listener: CacheEntryListener
): Promise<void> { ): Promise<void> {
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim() const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
report.markRequested(cacheKey) listener.markRequested(cacheKey)
const restoredKey = await this.restoreCache([bundlePattern], cacheKey) const restoredKey = await this.restoreCache([bundlePattern], cacheKey)
if (restoredKey) { if (restoredKey) {
core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`) core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
report.markRestored(restoredKey) listener.markRestored(restoredKey)
} else { } else {
core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`) core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
tryDelete(bundleMetaFile) tryDelete(bundleMetaFile)
@ -88,10 +88,10 @@ export class GradleUserHomeCache extends AbstractCache {
return bundleFiles return bundleFiles
} }
async beforeSave(report: CachingReport): Promise<void> { async beforeSave(listener: CacheListener): Promise<void> {
await this.reportGradleUserHomeSize('before saving common artifacts') await this.reportGradleUserHomeSize('before saving common artifacts')
this.removeExcludedPaths() this.removeExcludedPaths()
await this.saveArtifactBundles(report) await this.saveArtifactBundles(listener)
await this.reportGradleUserHomeSize( await this.reportGradleUserHomeSize(
"after saving common artifacts (only 'caches' and 'notifications' will be stored)" "after saving common artifacts (only 'caches' and 'notifications' will be stored)"
) )
@ -107,12 +107,12 @@ export class GradleUserHomeCache extends AbstractCache {
} }
} }
private async saveArtifactBundles(report: CachingReport): Promise<void> { private async saveArtifactBundles(listener: CacheListener): Promise<void> {
const processes: Promise<void>[] = [] const processes: Promise<void>[] = []
for (const [bundle, pattern] of this.getArtifactBundles()) { for (const [bundle, pattern] of this.getArtifactBundles()) {
const bundleEntryReport = report.entryReport(bundle) const entryListener = listener.entry(bundle)
const p = this.saveArtifactBundle(bundle, pattern, bundleEntryReport) const p = this.saveArtifactBundle(bundle, pattern, entryListener)
// Run sequentially when debugging enabled // Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) { if (this.cacheDebuggingEnabled) {
await p await p
@ -123,7 +123,11 @@ export class GradleUserHomeCache extends AbstractCache {
await Promise.all(processes) await Promise.all(processes)
} }
private async saveArtifactBundle(bundle: string, artifactPath: string, report: CacheEntryReport): Promise<void> { private async saveArtifactBundle(
bundle: string,
artifactPath: string,
listener: CacheEntryListener
): Promise<void> {
const bundleMetaFile = this.getBundleMetaFile(bundle) const bundleMetaFile = this.getBundleMetaFile(bundle)
const globber = await glob.create(artifactPath, { const globber = await glob.create(artifactPath, {
@ -152,7 +156,7 @@ export class GradleUserHomeCache extends AbstractCache {
core.info(`Caching ${bundle} with cache key: ${cacheKey}`) core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
await this.saveCache([artifactPath], cacheKey) await this.saveCache([artifactPath], cacheKey)
this.writeBundleMetaFile(bundleMetaFile, cacheKey) this.writeBundleMetaFile(bundleMetaFile, cacheKey)
report.markSaved(cacheKey) listener.markSaved(cacheKey)
} }
for (const file of bundleFiles) { for (const file of bundleFiles) {

View file

@ -2,7 +2,7 @@ import {GradleUserHomeCache} from './cache-gradle-user-home'
import {ProjectDotGradleCache} from './cache-project-dot-gradle' import {ProjectDotGradleCache} from './cache-project-dot-gradle'
import * as core from '@actions/core' import * as core from '@actions/core'
import {isCacheDisabled, isCacheReadOnly} from './cache-utils' import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
import {CachingReport} from './cache-base' import {CacheListener} from './cache-base'
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
const CACHING_REPORT = 'CACHING_REPORT' const CACHING_REPORT = 'CACHING_REPORT'
@ -16,20 +16,20 @@ export async function restore(buildRootDirectory: string): Promise<void> {
await core.group('Restore Gradle state from cache', async () => { await core.group('Restore Gradle state from cache', async () => {
core.saveState(BUILD_ROOT_DIR, buildRootDirectory) core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
const cachingReport = new CachingReport() const cacheListener = new CacheListener()
await new GradleUserHomeCache(buildRootDirectory).restore(cachingReport) await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory) const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
if (cachingReport.fullyRestored) { if (cacheListener.fullyRestored) {
// Only restore the configuration-cache if the Gradle Home is fully restored // Only restore the configuration-cache if the Gradle Home is fully restored
await projectDotGradleCache.restore(cachingReport) await projectDotGradleCache.restore(cacheListener)
} else { } else {
// Otherwise, prepare the cache key for later save() // Otherwise, prepare the cache key for later save()
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state') core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
projectDotGradleCache.prepareCacheKey() projectDotGradleCache.prepareCacheKey()
} }
core.saveState(CACHING_REPORT, cachingReport.stringify()) core.saveState(CACHING_REPORT, cacheListener.stringify())
}) })
} }
@ -39,7 +39,7 @@ export async function save(): Promise<void> {
return return
} }
const cachingReport: CachingReport = CachingReport.rehydrate(core.getState(CACHING_REPORT)) const cachingReport: CacheListener = CacheListener.rehydrate(core.getState(CACHING_REPORT))
await core.group('Caching Gradle state', async () => { await core.group('Caching Gradle state', async () => {
const buildRootDirectory = core.getState(BUILD_ROOT_DIR) const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
@ -52,6 +52,6 @@ export async function save(): Promise<void> {
logCachingReport(cachingReport) logCachingReport(cachingReport)
} }
function logCachingReport(report: CachingReport): void { function logCachingReport(report: CacheListener): void {
core.info(JSON.stringify(report, null, 2)) core.info(JSON.stringify(report, null, 2))
} }