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 const bundleMetaFiles = await this.getBundleMetaFiles()
// This is similar to how the primary implementation should work: const bundlePatterns = this.getArtifactBundles()
// - 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()
for (const bundleMetaFile of bundleMetaFiles) { // Iterate over all bundle meta files and try to restore
const bundle = path.basename(bundleMetaFile, '.cache') for (const bundleMetaFile of bundleMetaFiles) {
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)
// Run sequentially when debugging enabled
if (this.cacheDebuggingEnabled) { // Handle case where the 'artifactBundlePatterns' have been changed
await p 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
if (this.cacheDebuggingEnabled) {
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,
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim() bundleMetaFile: string,
report.markRequested(cacheKey) report: CacheEntryReport
): Promise<void> {
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
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 {
core.info(`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`)
// TODO Remove the .cache file here?
}
} else { } else {
this.debug(`No metafile found to restore ${bundle}: ${bundleMetaFile}`) core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
tryDelete(bundleMetaFile)
} }
} }