2021-08-24 12:57:17 -06:00
|
|
|
import * as core from '@actions/core'
|
2021-11-05 06:54:31 -06:00
|
|
|
import * as cache from '@actions/cache'
|
2022-06-06 09:26:49 -06:00
|
|
|
import * as exec from '@actions/exec'
|
|
|
|
|
|
2021-09-05 21:35:17 -06:00
|
|
|
import * as crypto from 'crypto'
|
2021-09-27 21:05:17 -06:00
|
|
|
import * as path from 'path'
|
2021-10-04 23:59:08 +02:00
|
|
|
import * as fs from 'fs'
|
2021-08-24 12:57:17 -06:00
|
|
|
|
2022-01-19 12:11:51 -07:00
|
|
|
import {CacheEntryListener} from './cache-reporting'
|
2021-10-27 16:05:07 -06:00
|
|
|
|
2022-08-22 13:28:43 -06:00
|
|
|
const SEGMENT_DOWNLOAD_TIMEOUT_VAR = 'SEGMENT_DOWNLOAD_TIMEOUT_MINS'
|
2022-08-22 13:07:12 -06:00
|
|
|
const SEGMENT_DOWNLOAD_TIMEOUT_DEFAULT = 10 * 60 * 1000 // 10 minutes
|
|
|
|
|
|
2024-04-06 20:37:46 -06:00
|
|
|
export function isCacheDebuggingEnabled(): boolean {
|
|
|
|
|
if (core.isDebug()) {
|
2022-06-20 17:39:25 -06:00
|
|
|
return true
|
|
|
|
|
}
|
2024-04-06 20:37:46 -06:00
|
|
|
return process.env['GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'] ? true : false
|
2022-06-12 09:53:04 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 16:52:53 -07:00
|
|
|
export function hashFileNames(fileNames: string[]): string {
|
|
|
|
|
return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')))
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 21:35:17 -06:00
|
|
|
export function hashStrings(values: string[]): string {
|
|
|
|
|
const hash = crypto.createHash('md5')
|
|
|
|
|
for (const value of values) {
|
|
|
|
|
hash.update(value)
|
|
|
|
|
}
|
|
|
|
|
return hash.digest('hex')
|
2021-09-05 17:10:47 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-29 16:07:33 -07:00
|
|
|
export async function restoreCache(
|
|
|
|
|
cachePath: string[],
|
|
|
|
|
cacheKey: string,
|
2022-01-19 12:11:51 -07:00
|
|
|
cacheRestoreKeys: string[],
|
|
|
|
|
listener: CacheEntryListener
|
2021-12-29 16:07:33 -07:00
|
|
|
): Promise<cache.CacheEntry | undefined> {
|
2022-01-19 12:11:51 -07:00
|
|
|
listener.markRequested(cacheKey, cacheRestoreKeys)
|
2021-12-29 16:07:33 -07:00
|
|
|
try {
|
2024-09-13 10:45:46 -06:00
|
|
|
const startTime = Date.now()
|
2022-08-22 13:07:12 -06:00
|
|
|
// Only override the read timeout if the SEGMENT_DOWNLOAD_TIMEOUT_MINS env var has NOT been set
|
2022-08-22 13:28:43 -06:00
|
|
|
const cacheRestoreOptions = process.env[SEGMENT_DOWNLOAD_TIMEOUT_VAR]
|
2022-08-22 13:07:12 -06:00
|
|
|
? {}
|
|
|
|
|
: {segmentTimeoutInMs: SEGMENT_DOWNLOAD_TIMEOUT_DEFAULT}
|
|
|
|
|
const restoredEntry = await cache.restoreCache(cachePath, cacheKey, cacheRestoreKeys, cacheRestoreOptions)
|
2022-01-19 12:11:51 -07:00
|
|
|
if (restoredEntry !== undefined) {
|
2024-09-13 11:05:35 -06:00
|
|
|
const restoreTime = Date.now() - startTime
|
|
|
|
|
listener.markRestored(restoredEntry.key, restoredEntry.size, restoreTime)
|
|
|
|
|
core.info(`Restored cache entry with key ${cacheKey} to ${cachePath.join()} in ${restoreTime}ms`)
|
2022-01-19 12:11:51 -07:00
|
|
|
}
|
|
|
|
|
return restoredEntry
|
2021-12-29 16:07:33 -07:00
|
|
|
} catch (error) {
|
2022-08-16 15:29:23 -06:00
|
|
|
listener.markNotRestored((error as Error).message)
|
2021-12-29 16:07:33 -07:00
|
|
|
handleCacheFailure(error, `Failed to restore ${cacheKey}`)
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 12:11:51 -07:00
|
|
|
export async function saveCache(cachePath: string[], cacheKey: string, listener: CacheEntryListener): Promise<void> {
|
2021-12-29 16:07:33 -07:00
|
|
|
try {
|
2024-09-13 10:45:46 -06:00
|
|
|
const startTime = Date.now()
|
2022-01-19 12:11:51 -07:00
|
|
|
const savedEntry = await cache.saveCache(cachePath, cacheKey)
|
2024-09-13 11:05:35 -06:00
|
|
|
const saveTime = Date.now() - startTime
|
|
|
|
|
listener.markSaved(savedEntry.key, savedEntry.size, saveTime)
|
|
|
|
|
core.info(`Saved cache entry with key ${cacheKey} from ${cachePath.join()} in ${saveTime}ms`)
|
2021-12-29 16:07:33 -07:00
|
|
|
} catch (error) {
|
2022-01-19 12:11:51 -07:00
|
|
|
if (error instanceof cache.ReserveCacheError) {
|
|
|
|
|
listener.markAlreadyExists(cacheKey)
|
2022-08-16 15:29:23 -06:00
|
|
|
} else {
|
|
|
|
|
listener.markNotSaved((error as Error).message)
|
2022-01-19 12:11:51 -07:00
|
|
|
}
|
2022-06-06 08:48:03 -06:00
|
|
|
handleCacheFailure(error, `Failed to save cache entry with path '${cachePath}' and key: ${cacheKey}`)
|
2021-12-29 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function cacheDebug(message: string): void {
|
|
|
|
|
if (isCacheDebuggingEnabled()) {
|
|
|
|
|
core.info(message)
|
|
|
|
|
} else {
|
|
|
|
|
core.debug(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 06:54:31 -06:00
|
|
|
export function handleCacheFailure(error: unknown, message: string): void {
|
|
|
|
|
if (error instanceof cache.ValidationError) {
|
|
|
|
|
// Fail on cache validation errors
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
if (error instanceof cache.ReserveCacheError) {
|
|
|
|
|
// Reserve cache errors are expected if the artifact has been previously cached
|
2022-01-19 13:31:55 -07:00
|
|
|
core.info(`${message}: ${error}`)
|
2021-11-05 06:54:31 -06:00
|
|
|
} else {
|
|
|
|
|
// Warn on all other errors
|
|
|
|
|
core.warning(`${message}: ${error}`)
|
2022-03-18 13:53:28 -06:00
|
|
|
if (error instanceof Error && error.stack) {
|
|
|
|
|
cacheDebug(error.stack)
|
|
|
|
|
}
|
2021-11-05 06:54:31 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 23:59:08 +02:00
|
|
|
/**
|
2021-10-15 14:54:29 -06:00
|
|
|
* Attempt to delete a file or directory, waiting to allow locks to be released
|
2021-10-04 23:59:08 +02:00
|
|
|
*/
|
|
|
|
|
export async function tryDelete(file: string): Promise<void> {
|
2022-06-06 09:26:49 -06:00
|
|
|
const maxAttempts = 5
|
|
|
|
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
2022-06-06 15:30:51 -06:00
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-04 23:59:08 +02:00
|
|
|
try {
|
2022-06-06 15:30:51 -06:00
|
|
|
const stat = fs.lstatSync(file)
|
2021-10-15 14:54:29 -06:00
|
|
|
if (stat.isDirectory()) {
|
2023-02-06 12:39:23 -07:00
|
|
|
fs.rmSync(file, {recursive: true})
|
2021-10-15 14:54:29 -06:00
|
|
|
} else {
|
|
|
|
|
fs.unlinkSync(file)
|
|
|
|
|
}
|
2021-10-04 23:59:08 +02:00
|
|
|
return
|
|
|
|
|
} catch (error) {
|
2022-06-06 09:26:49 -06:00
|
|
|
if (attempt === maxAttempts) {
|
|
|
|
|
core.warning(`Failed to delete ${file}, which will impact caching.
|
|
|
|
|
It is likely locked by another process. Output of 'jps -ml':
|
|
|
|
|
${await getJavaProcesses()}`)
|
2021-10-04 23:59:08 +02:00
|
|
|
throw error
|
|
|
|
|
} else {
|
2022-06-06 09:26:49 -06:00
|
|
|
cacheDebug(`Attempt to delete ${file} failed. Will try again.`)
|
2021-10-04 23:59:08 +02:00
|
|
|
await delay(1000)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function delay(ms: number): Promise<void> {
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
|
}
|
2022-06-06 09:26:49 -06:00
|
|
|
|
|
|
|
|
async function getJavaProcesses(): Promise<string> {
|
|
|
|
|
const jpsOutput = await exec.getExecOutput('jps', ['-lm'])
|
|
|
|
|
return jpsOutput.stdout
|
|
|
|
|
}
|