Consolidate error handling for cache restore/save

This commit is contained in:
Daz DeBoer 2021-09-14 13:30:23 -06:00
parent bd08e7b7cd
commit decca791c5
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
2 changed files with 52 additions and 44 deletions

View File

@ -3,7 +3,6 @@ import fs from 'fs'
import os from 'os' import os from 'os'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
import * as cache from '@actions/cache'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import {AbstractCache} from './cache-utils' import {AbstractCache} from './cache-utils'
@ -72,23 +71,14 @@ export class GradleUserHomeCache extends AbstractCache {
const key = path.relative(this.getGradleUserHome(), artifactFile) const key = path.relative(this.getGradleUserHome(), artifactFile)
const cacheKey = `gradle-artifact-${key}` const cacheKey = `gradle-artifact-${key}`
try { const restoreKey = await this.restoreCache([artifactFile], cacheKey)
const restoreKey = await cache.restoreCache(
[artifactFile],
cacheKey
)
if (restoreKey) { if (restoreKey) {
this.debug( this.debug(`Restored ${cacheKey} from cache to ${artifactFile}`)
`Restored ${cacheKey} from cache to ${artifactFile}`
)
} else { } else {
core.warning( this.debug(
`Failed to restore from ${cacheKey} to ${artifactFile}` `Failed to restore from ${cacheKey} to ${artifactFile}`
) )
} }
} catch (error) {
core.warning(`Error restoring ${cacheKey}: ${error}`)
}
} else { } else {
this.debug( this.debug(
`Artifact file already exists, not restoring: ${artifactFile}` `Artifact file already exists, not restoring: ${artifactFile}`
@ -164,21 +154,7 @@ export class GradleUserHomeCache extends AbstractCache {
const cacheKey = `gradle-artifact-${filePath}` const cacheKey = `gradle-artifact-${filePath}`
this.debug(`Caching ${artifactFile} with cache key: ${cacheKey}`) this.debug(`Caching ${artifactFile} with cache key: ${cacheKey}`)
try { await this.saveCache([artifactFile], cacheKey)
await cache.saveCache([artifactFile], cacheKey)
} catch (error) {
// Fail on validation errors or non-errors (the latter to keep Typescript happy)
if (error instanceof cache.ValidationError) {
throw error
} else if (error instanceof cache.ReserveCacheError) {
// These are expected if the artifact is already cached
this.debug(error.message)
} else if (error instanceof Error) {
core.warning(error.message)
} else {
core.warning(`${error}`)
}
}
// Write the marker file that will stand in place of the original // Write the marker file that will stand in place of the original
fs.writeFileSync(markerFile, 'cached') fs.writeFileSync(markerFile, 'cached')

View File

@ -91,7 +91,7 @@ export abstract class AbstractCache {
core.saveState(this.cacheKeyStateKey, cacheKey.key) core.saveState(this.cacheKeyStateKey, cacheKey.key)
const cacheResult = await cache.restoreCache( const cacheResult = await this.restoreCache(
this.getCachePath(), this.getCachePath(),
cacheKey.key, cacheKey.key,
cacheKey.restoreKeys cacheKey.restoreKeys
@ -112,6 +112,28 @@ export abstract class AbstractCache {
return return
} }
protected async restoreCache(
cachePath: string[],
cacheKey: string,
cacheRestoreKeys: string[] = []
): Promise<string | undefined> {
try {
return await cache.restoreCache(
cachePath,
cacheKey,
cacheRestoreKeys
)
} catch (error) {
if (error instanceof cache.ValidationError) {
// Validation errors should fail the build action
throw error
}
// Warn about any other error and continue
core.warning(`Failed to restore ${cacheKey}: ${error}`)
return undefined
}
}
async save(): Promise<void> { async save(): Promise<void> {
if (!this.cacheOutputExists()) { if (!this.cacheOutputExists()) {
this.debug(`No ${this.cacheDescription} to cache.`) this.debug(`No ${this.cacheDescription} to cache.`)
@ -138,22 +160,32 @@ export abstract class AbstractCache {
core.info( core.info(
`Caching ${this.cacheDescription} with cache key: ${cacheKey}` `Caching ${this.cacheDescription} with cache key: ${cacheKey}`
) )
try { const cachePath = this.getCachePath()
await cache.saveCache(this.getCachePath(), cacheKey) await this.saveCache(cachePath, cacheKey)
} catch (error) {
// Fail on validation errors or non-errors (the latter to keep Typescript happy)
if (
error instanceof cache.ValidationError ||
!(error instanceof Error)
) {
throw error
}
core.warning(error.message)
}
return return
} }
protected async saveCache(
cachePath: string[],
cacheKey: string
): Promise<void> {
try {
await cache.saveCache(cachePath, cacheKey)
} catch (error) {
if (error instanceof cache.ValidationError) {
// Validation errors should fail the build action
throw error
} else if (error instanceof cache.ReserveCacheError) {
// Reserve cache errors are expected if the artifact has been previously cached
this.debug(error.message)
} else {
// Warn about any other error and continue
core.warning(String(error))
}
}
}
protected debug(message: string): void { protected debug(message: string): void {
if (this.cacheDebuggingEnabled) { if (this.cacheDebuggingEnabled) {
core.info(message) core.info(message)