diff --git a/src/cache-utils.ts b/src/cache-utils.ts index d57c054..111b85a 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -1,6 +1,8 @@ import * as core from '@actions/core' import * as cache from '@actions/cache' import * as github from '@actions/github' +import * as exec from '@actions/exec' + import * as crypto from 'crypto' import * as path from 'path' import * as fs from 'fs' @@ -195,8 +197,9 @@ export function handleCacheFailure(error: unknown, message: string): void { * Attempt to delete a file or directory, waiting to allow locks to be released */ export async function tryDelete(file: string): Promise { + const maxAttempts = 5 const stat = fs.lstatSync(file) - for (let count = 0; count < 3; count++) { + for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { if (stat.isDirectory()) { fs.rmdirSync(file, {recursive: true}) @@ -205,10 +208,13 @@ export async function tryDelete(file: string): Promise { } return } catch (error) { - if (count === 2) { + 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()}`) throw error } else { - core.warning(String(error)) + cacheDebug(`Attempt to delete ${file} failed. Will try again.`) await delay(1000) } } @@ -218,3 +224,8 @@ export async function tryDelete(file: string): Promise { async function delay(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)) } + +async function getJavaProcesses(): Promise { + const jpsOutput = await exec.getExecOutput('jps', ['-lm']) + return jpsOutput.stdout +} diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index d13713f..d85bd59 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -13,7 +13,7 @@ const GRADLE_USER_HOME = 'GRADLE_USER_HOME' const CACHE_LISTENER = 'CACHE_LISTENER' const JOB_SUMMARY_ENABLED_PARAMETER = 'generate-job-summary' -function generateJobSummary(): boolean { +function shouldGenerateJobSummary(): boolean { return core.getBooleanInput(JOB_SUMMARY_ENABLED_PARAMETER) } @@ -56,7 +56,7 @@ export async function complete(): Promise { const gradleUserHome = core.getState(GRADLE_USER_HOME) await caches.save(gradleUserHome, cacheListener) - if (generateJobSummary()) { + if (shouldGenerateJobSummary()) { writeJobSummary(buildResults, cacheListener) } }