Skip to content

Commit

Permalink
Merge pull request #90 from gradle/dd/windows-locks
Browse files Browse the repository at this point in the history
Allow time for processes to delete file locks on windows
  • Loading branch information
bigdaz committed Oct 4, 2021
2 parents c000a0b + 5328161 commit 2989475
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/cache-gradle-user-home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as core from '@actions/core'
import * as glob from '@actions/glob'
import * as exec from '@actions/exec'

import {AbstractCache, hashFileNames} from './cache-utils'
import {AbstractCache, hashFileNames, tryDelete} from './cache-utils'

// Which paths under Gradle User Home should be cached
const CACHE_PATH = ['caches', 'notifications']
Expand Down Expand Up @@ -141,7 +141,7 @@ export class GradleUserHomeCache extends AbstractCache {
if (commonArtifactFiles.length === 0) {
this.debug(`No files found to cache for ${bundle}`)
if (fs.existsSync(cacheMetaFile)) {
fs.unlinkSync(cacheMetaFile)
tryDelete(cacheMetaFile)
}
return
}
Expand All @@ -164,7 +164,7 @@ export class GradleUserHomeCache extends AbstractCache {
}

for (const file of commonArtifactFiles) {
fs.unlinkSync(file)
tryDelete(file)
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as cache from '@actions/cache'
import * as github from '@actions/github'
import * as crypto from 'crypto'
import * as path from 'path'
import * as fs from 'fs'

export function isCacheDisabled(): boolean {
return core.getBooleanInput('cache-disabled')
Expand Down Expand Up @@ -60,6 +61,29 @@ export function hashFileNames(fileNames: string[]): string {
)
}

/**
* Attempt to delete a file, waiting to allow locks to be released
*/
export async function tryDelete(file: string): Promise<void> {
for (let count = 0; count < 3; count++) {
try {
fs.unlinkSync(file)
return
} catch (error) {
if (count === 2) {
throw error
} else {
core.warning(String(error))
await delay(1000)
}
}
}
}

async function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}

class CacheKey {
key: string
restoreKeys: string[]
Expand Down

0 comments on commit 2989475

Please sign in to comment.