From 4968d2280b506786dea973c32bac7c65615927f9 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Mon, 4 Oct 2021 23:59:08 +0200 Subject: [PATCH] Allow time for processes to release file locks on windows --- src/cache-gradle-user-home.ts | 6 +++--- src/cache-utils.ts | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index b4ac360..a07e562 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -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'] @@ -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 } @@ -164,7 +164,7 @@ export class GradleUserHomeCache extends AbstractCache { } for (const file of commonArtifactFiles) { - fs.unlinkSync(file) + tryDelete(file) } } diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 0c9c57e..b618b94 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -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') @@ -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 { + 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 { + return new Promise(resolve => setTimeout(resolve, ms)) +} + class CacheKey { key: string restoreKeys: string[]