use fileExists lambda instead of stat to check file exists in caching

pull/353/head
Frostebite 2022-04-10 23:11:36 +01:00
parent e4c0fbfa6f
commit 76d9019b01
3 changed files with 28 additions and 42 deletions

32
dist/index.js vendored
View File

@ -2513,6 +2513,8 @@ const lfs_hashing_1 = __nccwpck_require__(8915);
const remote_client_logger_1 = __nccwpck_require__(59412); const remote_client_logger_1 = __nccwpck_require__(59412);
const cli_1 = __nccwpck_require__(55651); const cli_1 = __nccwpck_require__(55651);
const cli_functions_repository_1 = __nccwpck_require__(85301); const cli_functions_repository_1 = __nccwpck_require__(85301);
// eslint-disable-next-line github/no-then
const fileExists = (fpath) => __awaiter(void 0, void 0, void 0, function* () { return !!(yield fs_1.default.promises.stat(fpath).catch(() => false)); });
class Caching { class Caching {
static cachePush() { static cachePush() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -2543,8 +2545,7 @@ class Caching {
cacheArtifactName = cacheArtifactName.replace(' ', ''); cacheArtifactName = cacheArtifactName.replace(' ', '');
const startPath = process.cwd(); const startPath = process.cwd();
try { try {
const cacheFolderStat = yield fs_1.default.promises.stat(cacheFolder); if (!(yield fileExists(cacheFolder))) {
if (!cacheFolderStat.isDirectory()) {
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`);
} }
process.chdir(path_1.default.resolve(sourceFolder, '..')); process.chdir(path_1.default.resolve(sourceFolder, '..'));
@ -2559,17 +2560,14 @@ class Caching {
}); });
}; };
yield cloud_runner_system_1.CloudRunnerSystem.Run(`zip -q -r ${cacheArtifactName}.zip ${path_1.default.basename(sourceFolder)}`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`zip -q -r ${cacheArtifactName}.zip ${path_1.default.basename(sourceFolder)}`);
const cacheArtifactStatPreMove = yield fs_1.default.promises.stat(`${cacheArtifactName}.zip`); console_1.assert(yield fileExists(`${cacheArtifactName}.zip`), 'cache zip exists');
console_1.assert(cacheArtifactStatPreMove.isFile(), 'cache zip exists'); console_1.assert(yield fileExists(path_1.default.basename(sourceFolder)), 'source folder exists');
const sourceFolderStat = yield fs_1.default.promises.stat(path_1.default.basename(sourceFolder));
console_1.assert(sourceFolderStat.isDirectory(), 'source folder exists');
if (cloud_runner_1.default.buildParameters.cachePushOverrideCommand) { if (cloud_runner_1.default.buildParameters.cachePushOverrideCommand) {
yield cloud_runner_system_1.CloudRunnerSystem.Run(formatFunction(cloud_runner_1.default.buildParameters.cachePushOverrideCommand)); yield cloud_runner_system_1.CloudRunnerSystem.Run(formatFunction(cloud_runner_1.default.buildParameters.cachePushOverrideCommand));
} }
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mv ${cacheArtifactName}.zip ${cacheFolder}`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`mv ${cacheArtifactName}.zip ${cacheFolder}`);
remote_client_logger_1.RemoteClientLogger.log(`moved ${cacheArtifactName}.zip to ${cacheFolder}`); remote_client_logger_1.RemoteClientLogger.log(`moved ${cacheArtifactName}.zip to ${cacheFolder}`);
const cacheArtifactStat = yield fs_1.default.promises.stat(`${path_1.default.join(cacheFolder, cacheArtifactName)}.zip`); console_1.assert(yield fileExists(`${path_1.default.join(cacheFolder, cacheArtifactName)}.zip`), 'cache zip exists inside cache folder');
console_1.assert(cacheArtifactStat.isFile(), 'cache zip exists inside cache folder');
} }
catch (error) { catch (error) {
process.chdir(`${startPath}`); process.chdir(`${startPath}`);
@ -2584,20 +2582,17 @@ class Caching {
const startPath = process.cwd(); const startPath = process.cwd();
remote_client_logger_1.RemoteClientLogger.log(`Caching for ${path_1.default.basename(destinationFolder)}`); remote_client_logger_1.RemoteClientLogger.log(`Caching for ${path_1.default.basename(destinationFolder)}`);
try { try {
const cacheFolderStat = yield fs_1.default.promises.stat(cacheFolder); if (!(yield fileExists(cacheFolder))) {
if (!cacheFolderStat.isDirectory()) {
yield fs_1.default.promises.mkdir(cacheFolder); yield fs_1.default.promises.mkdir(cacheFolder);
} }
const destinationStat = yield fs_1.default.promises.stat(destinationFolder); if (!(yield fileExists(destinationFolder))) {
if (!destinationStat.isDirectory()) {
yield fs_1.default.promises.mkdir(destinationFolder); yield fs_1.default.promises.mkdir(destinationFolder);
} }
const latestInBranch = yield (yield cloud_runner_system_1.CloudRunnerSystem.Run(`ls -t "${cacheFolder}" | grep .zip$ | head -1`)) const latestInBranch = yield (yield cloud_runner_system_1.CloudRunnerSystem.Run(`ls -t "${cacheFolder}" | grep .zip$ | head -1`))
.replace(/\n/g, ``) .replace(/\n/g, ``)
.replace('.zip', ''); .replace('.zip', '');
process.chdir(cacheFolder); process.chdir(cacheFolder);
const cacheArtifactStat = yield fs_1.default.promises.stat(`${cacheArtifactName}.zip`); const cacheSelection = cacheArtifactName !== `` && (yield fileExists(`${cacheArtifactName}.zip`)) ? cacheArtifactName : latestInBranch;
const cacheSelection = cacheArtifactName !== `` && cacheArtifactStat.isFile() ? cacheArtifactName : latestInBranch;
yield cloud_runner_logger_1.default.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`); yield cloud_runner_logger_1.default.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`);
// eslint-disable-next-line func-style // eslint-disable-next-line func-style
const formatFunction = function (format) { const formatFunction = function (format) {
@ -2609,19 +2604,16 @@ class Caching {
if (cloud_runner_1.default.buildParameters.cachePullOverrideCommand) { if (cloud_runner_1.default.buildParameters.cachePullOverrideCommand) {
yield cloud_runner_system_1.CloudRunnerSystem.Run(formatFunction(cloud_runner_1.default.buildParameters.cachePullOverrideCommand)); yield cloud_runner_system_1.CloudRunnerSystem.Run(formatFunction(cloud_runner_1.default.buildParameters.cachePullOverrideCommand));
} }
const cacheSelectionExistsStat = yield fs_1.default.promises.stat(`${cacheSelection}.zip`); if (yield fileExists(`${cacheSelection}.zip`)) {
if (cacheSelectionExistsStat.isFile()) {
const resultsFolder = `results${cloud_runner_1.default.buildParameters.buildGuid}`; const resultsFolder = `results${cloud_runner_1.default.buildParameters.buildGuid}`;
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`);
remote_client_logger_1.RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`); remote_client_logger_1.RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`);
const fullResultsFolder = path_1.default.join(cacheFolder, resultsFolder); const fullResultsFolder = path_1.default.join(cacheFolder, resultsFolder);
yield cloud_runner_system_1.CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path_1.default.basename(resultsFolder)}`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path_1.default.basename(resultsFolder)}`);
remote_client_logger_1.RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`); remote_client_logger_1.RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`);
const fullResultsFolderStat = yield fs_1.default.promises.stat(fullResultsFolder); console_1.assert(yield fileExists(fullResultsFolder), `cache extraction results folder exists`);
console_1.assert(fullResultsFolderStat.isDirectory(), `cache extraction results folder exists`);
const destinationParentFolder = path_1.default.resolve(destinationFolder, '..'); const destinationParentFolder = path_1.default.resolve(destinationFolder, '..');
const destinationFolderStat = yield fs_1.default.promises.stat(destinationFolder); if (yield fileExists(destinationFolder)) {
if (destinationFolderStat.isDirectory()) {
yield fs_1.default.promises.rmdir(destinationFolder, { recursive: true }); yield fs_1.default.promises.rmdir(destinationFolder, { recursive: true });
} }
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mv "${path_1.default.join(fullResultsFolder, path_1.default.basename(destinationFolder))}" "${destinationParentFolder}"`); yield cloud_runner_system_1.CloudRunnerSystem.Run(`mv "${path_1.default.join(fullResultsFolder, path_1.default.basename(destinationFolder))}" "${destinationParentFolder}"`);

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,8 @@ import { LfsHashing } from '../services/lfs-hashing';
import { RemoteClientLogger } from './remote-client-logger'; import { RemoteClientLogger } from './remote-client-logger';
import { Cli } from '../../cli/cli'; import { Cli } from '../../cli/cli';
import { CliFunction } from '../../cli/cli-functions-repository'; import { CliFunction } from '../../cli/cli-functions-repository';
// eslint-disable-next-line github/no-then
const fileExists = async (fpath) => !!(await fs.promises.stat(fpath).catch(() => false));
export class Caching { export class Caching {
@CliFunction(`cache-push`, `push to cache`) @CliFunction(`cache-push`, `push to cache`)
@ -45,8 +47,7 @@ export class Caching {
cacheArtifactName = cacheArtifactName.replace(' ', ''); cacheArtifactName = cacheArtifactName.replace(' ', '');
const startPath = process.cwd(); const startPath = process.cwd();
try { try {
const cacheFolderStat = await fs.promises.stat(cacheFolder); if (!(await fileExists(cacheFolder))) {
if (!cacheFolderStat.isDirectory()) {
await CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`); await CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`);
} }
process.chdir(path.resolve(sourceFolder, '..')); process.chdir(path.resolve(sourceFolder, '..'));
@ -69,17 +70,17 @@ export class Caching {
}); });
}; };
await CloudRunnerSystem.Run(`zip -q -r ${cacheArtifactName}.zip ${path.basename(sourceFolder)}`); await CloudRunnerSystem.Run(`zip -q -r ${cacheArtifactName}.zip ${path.basename(sourceFolder)}`);
const cacheArtifactStatPreMove = await fs.promises.stat(`${cacheArtifactName}.zip`); assert(await fileExists(`${cacheArtifactName}.zip`), 'cache zip exists');
assert(cacheArtifactStatPreMove.isFile(), 'cache zip exists'); assert(await fileExists(path.basename(sourceFolder)), 'source folder exists');
const sourceFolderStat = await fs.promises.stat(path.basename(sourceFolder));
assert(sourceFolderStat.isDirectory(), 'source folder exists');
if (CloudRunner.buildParameters.cachePushOverrideCommand) { if (CloudRunner.buildParameters.cachePushOverrideCommand) {
await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePushOverrideCommand)); await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePushOverrideCommand));
} }
await CloudRunnerSystem.Run(`mv ${cacheArtifactName}.zip ${cacheFolder}`); await CloudRunnerSystem.Run(`mv ${cacheArtifactName}.zip ${cacheFolder}`);
RemoteClientLogger.log(`moved ${cacheArtifactName}.zip to ${cacheFolder}`); RemoteClientLogger.log(`moved ${cacheArtifactName}.zip to ${cacheFolder}`);
const cacheArtifactStat = await fs.promises.stat(`${path.join(cacheFolder, cacheArtifactName)}.zip`); assert(
assert(cacheArtifactStat.isFile(), 'cache zip exists inside cache folder'); await fileExists(`${path.join(cacheFolder, cacheArtifactName)}.zip`),
'cache zip exists inside cache folder',
);
} catch (error) { } catch (error) {
process.chdir(`${startPath}`); process.chdir(`${startPath}`);
throw error; throw error;
@ -91,14 +92,11 @@ export class Caching {
const startPath = process.cwd(); const startPath = process.cwd();
RemoteClientLogger.log(`Caching for ${path.basename(destinationFolder)}`); RemoteClientLogger.log(`Caching for ${path.basename(destinationFolder)}`);
try { try {
const cacheFolderStat = await fs.promises.stat(cacheFolder); if (!(await fileExists(cacheFolder))) {
if (!cacheFolderStat.isDirectory()) {
await fs.promises.mkdir(cacheFolder); await fs.promises.mkdir(cacheFolder);
} }
const destinationStat = await fs.promises.stat(destinationFolder); if (!(await fileExists(destinationFolder))) {
if (!destinationStat.isDirectory()) {
await fs.promises.mkdir(destinationFolder); await fs.promises.mkdir(destinationFolder);
} }
@ -107,10 +105,9 @@ export class Caching {
.replace('.zip', ''); .replace('.zip', '');
process.chdir(cacheFolder); process.chdir(cacheFolder);
const cacheArtifactStat = await fs.promises.stat(`${cacheArtifactName}.zip`);
const cacheSelection = const cacheSelection =
cacheArtifactName !== `` && cacheArtifactStat.isFile() ? cacheArtifactName : latestInBranch; cacheArtifactName !== `` && (await fileExists(`${cacheArtifactName}.zip`)) ? cacheArtifactName : latestInBranch;
await CloudRunnerLogger.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`); await CloudRunnerLogger.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`);
// eslint-disable-next-line func-style // eslint-disable-next-line func-style
@ -128,20 +125,17 @@ export class Caching {
await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePullOverrideCommand)); await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePullOverrideCommand));
} }
const cacheSelectionExistsStat = await fs.promises.stat(`${cacheSelection}.zip`); if (await fileExists(`${cacheSelection}.zip`)) {
if (cacheSelectionExistsStat.isFile()) {
const resultsFolder = `results${CloudRunner.buildParameters.buildGuid}`; const resultsFolder = `results${CloudRunner.buildParameters.buildGuid}`;
await CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`); await CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`);
RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`); RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`);
const fullResultsFolder = path.join(cacheFolder, resultsFolder); const fullResultsFolder = path.join(cacheFolder, resultsFolder);
await CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path.basename(resultsFolder)}`); await CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path.basename(resultsFolder)}`);
RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`); RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`);
const fullResultsFolderStat = await fs.promises.stat(fullResultsFolder); assert(await fileExists(fullResultsFolder), `cache extraction results folder exists`);
assert(fullResultsFolderStat.isDirectory(), `cache extraction results folder exists`);
const destinationParentFolder = path.resolve(destinationFolder, '..'); const destinationParentFolder = path.resolve(destinationFolder, '..');
const destinationFolderStat = await fs.promises.stat(destinationFolder); if (await fileExists(destinationFolder)) {
if (destinationFolderStat.isDirectory()) {
await fs.promises.rmdir(destinationFolder, { recursive: true }); await fs.promises.rmdir(destinationFolder, { recursive: true });
} }
await CloudRunnerSystem.Run( await CloudRunnerSystem.Run(