Better handling of .cache files that are not restored

- Remove any .cache file that is not restored
- Report on any .cache file that exists but has no config
This commit is contained in:
Daz DeBoer 2021-10-30 06:24:34 -06:00
parent 8ba5a0033b
commit f0f68e07c3
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D

View File

@ -37,52 +37,50 @@ export class GradleUserHomeCache extends AbstractCache {
private async restoreArtifactBundles(report: CachingReport): Promise<void> { private async restoreArtifactBundles(report: CachingReport): Promise<void> {
const processes: Promise<void>[] = [] const processes: Promise<void>[] = []
// This is special logic that allows the tests to simulate a "not restored" state by configuring an empty set of bundles
// This is similar to how the primary implementation should work:
// - Iterate over bundle meta-files as the basis for restoring content.
// - Leave bundle meta-files for successful restore.
// - Remove bundle meta-files that are not restored.
if (this.getArtifactBundles().size === 0) {
const bundleMetaFiles = await this.getBundleMetaFiles() const bundleMetaFiles = await this.getBundleMetaFiles()
const bundlePatterns = this.getArtifactBundles()
// 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')
core.info(`Found bundle metafile for ${bundle} but no such bundle configured`)
report.addEntryReport(bundleMetaFile).markRequested('BUNDLE_NOT_CONFIGURED')
tryDelete(bundleMetaFile)
}
}
for (const [bundle, pattern] of this.getArtifactBundles()) {
const bundleEntryReport = report.addEntryReport(bundle) const bundleEntryReport = report.addEntryReport(bundle)
const p = this.restoreArtifactBundle(bundle, pattern, bundleEntryReport) const bundlePattern = bundlePatterns.get(bundle)
// Handle case where the 'artifactBundlePatterns' have been changed
if (bundlePattern === undefined) {
core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
bundleEntryReport.markRequested('BUNDLE_NOT_CONFIGURED')
tryDelete(bundleMetaFile)
return
} else {
const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, bundleEntryReport)
// Run sequentially when debugging enabled // Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) { if (this.cacheDebuggingEnabled) {
await p await p
} }
processes.push(p) processes.push(p)
} }
}
await Promise.all(processes) await Promise.all(processes)
} }
private async restoreArtifactBundle(bundle: string, artifactPath: string, report: CacheEntryReport): Promise<void> { private async restoreArtifactBundle(
const bundleMetaFile = this.getBundleMetaFile(bundle) bundle: string,
if (fs.existsSync(bundleMetaFile)) { bundlePattern: string,
bundleMetaFile: string,
report: CacheEntryReport
): Promise<void> {
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim() const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
report.markRequested(cacheKey) report.markRequested(cacheKey)
const restoredKey = await this.restoreCache([artifactPath], cacheKey) const restoredKey = await this.restoreCache([bundlePattern], cacheKey)
if (restoredKey) { if (restoredKey) {
core.info(`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`) core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
report.markRestored(restoredKey) report.markRestored(restoredKey)
} else { } else {
core.info(`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`) core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
// TODO Remove the .cache file here? tryDelete(bundleMetaFile)
}
} else {
this.debug(`No metafile found to restore ${bundle}: ${bundleMetaFile}`)
} }
} }