Skip to content

Commit

Permalink
Better error reporting when file deletion fails
Browse files Browse the repository at this point in the history
- Include file name in all logging
- Log inital attempts at debug to avoid noise
- Include output of 'jps -lm' when final attempt fails
  • Loading branch information
bigdaz committed Jun 6, 2022
1 parent 9cd70b5 commit 8096e65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<void> {
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})
Expand All @@ -205,10 +208,13 @@ export async function tryDelete(file: string): Promise<void> {
}
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)
}
}
Expand All @@ -218,3 +224,8 @@ export async function tryDelete(file: string): Promise<void> {
async function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function getJavaProcesses(): Promise<string> {
const jpsOutput = await exec.getExecOutput('jps', ['-lm'])
return jpsOutput.stdout
}
4 changes: 2 additions & 2 deletions src/setup-gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -56,7 +56,7 @@ export async function complete(): Promise<void> {
const gradleUserHome = core.getState(GRADLE_USER_HOME)
await caches.save(gradleUserHome, cacheListener)

if (generateJobSummary()) {
if (shouldGenerateJobSummary()) {
writeJobSummary(buildResults, cacheListener)
}
}
Expand Down

0 comments on commit 8096e65

Please sign in to comment.