use fileExists lambda instead of stat to check file exists in caching
parent
e4c0fbfa6f
commit
76d9019b01
|
|
@ -2513,6 +2513,8 @@ const lfs_hashing_1 = __nccwpck_require__(8915);
|
|||
const remote_client_logger_1 = __nccwpck_require__(59412);
|
||||
const cli_1 = __nccwpck_require__(55651);
|
||||
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 {
|
||||
static cachePush() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
|
|
@ -2543,8 +2545,7 @@ class Caching {
|
|||
cacheArtifactName = cacheArtifactName.replace(' ', '');
|
||||
const startPath = process.cwd();
|
||||
try {
|
||||
const cacheFolderStat = yield fs_1.default.promises.stat(cacheFolder);
|
||||
if (!cacheFolderStat.isDirectory()) {
|
||||
if (!(yield fileExists(cacheFolder))) {
|
||||
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`);
|
||||
}
|
||||
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)}`);
|
||||
const cacheArtifactStatPreMove = yield fs_1.default.promises.stat(`${cacheArtifactName}.zip`);
|
||||
console_1.assert(cacheArtifactStatPreMove.isFile(), 'cache zip exists');
|
||||
const sourceFolderStat = yield fs_1.default.promises.stat(path_1.default.basename(sourceFolder));
|
||||
console_1.assert(sourceFolderStat.isDirectory(), 'source folder exists');
|
||||
console_1.assert(yield fileExists(`${cacheArtifactName}.zip`), 'cache zip exists');
|
||||
console_1.assert(yield fileExists(path_1.default.basename(sourceFolder)), 'source folder exists');
|
||||
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(`mv ${cacheArtifactName}.zip ${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(cacheArtifactStat.isFile(), 'cache zip exists inside cache folder');
|
||||
console_1.assert(yield fileExists(`${path_1.default.join(cacheFolder, cacheArtifactName)}.zip`), 'cache zip exists inside cache folder');
|
||||
}
|
||||
catch (error) {
|
||||
process.chdir(`${startPath}`);
|
||||
|
|
@ -2584,20 +2582,17 @@ class Caching {
|
|||
const startPath = process.cwd();
|
||||
remote_client_logger_1.RemoteClientLogger.log(`Caching for ${path_1.default.basename(destinationFolder)}`);
|
||||
try {
|
||||
const cacheFolderStat = yield fs_1.default.promises.stat(cacheFolder);
|
||||
if (!cacheFolderStat.isDirectory()) {
|
||||
if (!(yield fileExists(cacheFolder))) {
|
||||
yield fs_1.default.promises.mkdir(cacheFolder);
|
||||
}
|
||||
const destinationStat = yield fs_1.default.promises.stat(destinationFolder);
|
||||
if (!destinationStat.isDirectory()) {
|
||||
if (!(yield fileExists(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`))
|
||||
.replace(/\n/g, ``)
|
||||
.replace('.zip', '');
|
||||
process.chdir(cacheFolder);
|
||||
const cacheArtifactStat = yield fs_1.default.promises.stat(`${cacheArtifactName}.zip`);
|
||||
const cacheSelection = cacheArtifactName !== `` && cacheArtifactStat.isFile() ? cacheArtifactName : latestInBranch;
|
||||
const cacheSelection = cacheArtifactName !== `` && (yield fileExists(`${cacheArtifactName}.zip`)) ? cacheArtifactName : latestInBranch;
|
||||
yield cloud_runner_logger_1.default.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`);
|
||||
// eslint-disable-next-line func-style
|
||||
const formatFunction = function (format) {
|
||||
|
|
@ -2609,19 +2604,16 @@ class Caching {
|
|||
if (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 (cacheSelectionExistsStat.isFile()) {
|
||||
if (yield fileExists(`${cacheSelection}.zip`)) {
|
||||
const resultsFolder = `results${cloud_runner_1.default.buildParameters.buildGuid}`;
|
||||
yield cloud_runner_system_1.CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`);
|
||||
remote_client_logger_1.RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`);
|
||||
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)}`);
|
||||
remote_client_logger_1.RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`);
|
||||
const fullResultsFolderStat = yield fs_1.default.promises.stat(fullResultsFolder);
|
||||
console_1.assert(fullResultsFolderStat.isDirectory(), `cache extraction results folder exists`);
|
||||
console_1.assert(yield fileExists(fullResultsFolder), `cache extraction results folder exists`);
|
||||
const destinationParentFolder = path_1.default.resolve(destinationFolder, '..');
|
||||
const destinationFolderStat = yield fs_1.default.promises.stat(destinationFolder);
|
||||
if (destinationFolderStat.isDirectory()) {
|
||||
if (yield fileExists(destinationFolder)) {
|
||||
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}"`);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -9,6 +9,8 @@ import { LfsHashing } from '../services/lfs-hashing';
|
|||
import { RemoteClientLogger } from './remote-client-logger';
|
||||
import { Cli } from '../../cli/cli';
|
||||
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 {
|
||||
@CliFunction(`cache-push`, `push to cache`)
|
||||
|
|
@ -45,8 +47,7 @@ export class Caching {
|
|||
cacheArtifactName = cacheArtifactName.replace(' ', '');
|
||||
const startPath = process.cwd();
|
||||
try {
|
||||
const cacheFolderStat = await fs.promises.stat(cacheFolder);
|
||||
if (!cacheFolderStat.isDirectory()) {
|
||||
if (!(await fileExists(cacheFolder))) {
|
||||
await CloudRunnerSystem.Run(`mkdir -p ${cacheFolder}`);
|
||||
}
|
||||
process.chdir(path.resolve(sourceFolder, '..'));
|
||||
|
|
@ -69,17 +70,17 @@ export class Caching {
|
|||
});
|
||||
};
|
||||
await CloudRunnerSystem.Run(`zip -q -r ${cacheArtifactName}.zip ${path.basename(sourceFolder)}`);
|
||||
const cacheArtifactStatPreMove = await fs.promises.stat(`${cacheArtifactName}.zip`);
|
||||
assert(cacheArtifactStatPreMove.isFile(), 'cache zip exists');
|
||||
const sourceFolderStat = await fs.promises.stat(path.basename(sourceFolder));
|
||||
assert(sourceFolderStat.isDirectory(), 'source folder exists');
|
||||
assert(await fileExists(`${cacheArtifactName}.zip`), 'cache zip exists');
|
||||
assert(await fileExists(path.basename(sourceFolder)), 'source folder exists');
|
||||
if (CloudRunner.buildParameters.cachePushOverrideCommand) {
|
||||
await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePushOverrideCommand));
|
||||
}
|
||||
await CloudRunnerSystem.Run(`mv ${cacheArtifactName}.zip ${cacheFolder}`);
|
||||
RemoteClientLogger.log(`moved ${cacheArtifactName}.zip to ${cacheFolder}`);
|
||||
const cacheArtifactStat = await fs.promises.stat(`${path.join(cacheFolder, cacheArtifactName)}.zip`);
|
||||
assert(cacheArtifactStat.isFile(), 'cache zip exists inside cache folder');
|
||||
assert(
|
||||
await fileExists(`${path.join(cacheFolder, cacheArtifactName)}.zip`),
|
||||
'cache zip exists inside cache folder',
|
||||
);
|
||||
} catch (error) {
|
||||
process.chdir(`${startPath}`);
|
||||
throw error;
|
||||
|
|
@ -91,14 +92,11 @@ export class Caching {
|
|||
const startPath = process.cwd();
|
||||
RemoteClientLogger.log(`Caching for ${path.basename(destinationFolder)}`);
|
||||
try {
|
||||
const cacheFolderStat = await fs.promises.stat(cacheFolder);
|
||||
if (!cacheFolderStat.isDirectory()) {
|
||||
if (!(await fileExists(cacheFolder))) {
|
||||
await fs.promises.mkdir(cacheFolder);
|
||||
}
|
||||
|
||||
const destinationStat = await fs.promises.stat(destinationFolder);
|
||||
|
||||
if (!destinationStat.isDirectory()) {
|
||||
if (!(await fileExists(destinationFolder))) {
|
||||
await fs.promises.mkdir(destinationFolder);
|
||||
}
|
||||
|
||||
|
|
@ -107,10 +105,9 @@ export class Caching {
|
|||
.replace('.zip', '');
|
||||
|
||||
process.chdir(cacheFolder);
|
||||
const cacheArtifactStat = await fs.promises.stat(`${cacheArtifactName}.zip`);
|
||||
|
||||
const cacheSelection =
|
||||
cacheArtifactName !== `` && cacheArtifactStat.isFile() ? cacheArtifactName : latestInBranch;
|
||||
cacheArtifactName !== `` && (await fileExists(`${cacheArtifactName}.zip`)) ? cacheArtifactName : latestInBranch;
|
||||
await CloudRunnerLogger.log(`cache key ${cacheArtifactName} selection ${cacheSelection}`);
|
||||
|
||||
// eslint-disable-next-line func-style
|
||||
|
|
@ -128,20 +125,17 @@ export class Caching {
|
|||
await CloudRunnerSystem.Run(formatFunction(CloudRunner.buildParameters.cachePullOverrideCommand));
|
||||
}
|
||||
|
||||
const cacheSelectionExistsStat = await fs.promises.stat(`${cacheSelection}.zip`);
|
||||
if (cacheSelectionExistsStat.isFile()) {
|
||||
if (await fileExists(`${cacheSelection}.zip`)) {
|
||||
const resultsFolder = `results${CloudRunner.buildParameters.buildGuid}`;
|
||||
await CloudRunnerSystem.Run(`mkdir -p ${resultsFolder}`);
|
||||
RemoteClientLogger.log(`cache item exists ${cacheFolder}/${cacheSelection}.zip`);
|
||||
const fullResultsFolder = path.join(cacheFolder, resultsFolder);
|
||||
await CloudRunnerSystem.Run(`unzip -q ${cacheSelection}.zip -d ${path.basename(resultsFolder)}`);
|
||||
RemoteClientLogger.log(`cache item extracted to ${fullResultsFolder}`);
|
||||
const fullResultsFolderStat = await fs.promises.stat(fullResultsFolder);
|
||||
assert(fullResultsFolderStat.isDirectory(), `cache extraction results folder exists`);
|
||||
assert(await fileExists(fullResultsFolder), `cache extraction results folder exists`);
|
||||
const destinationParentFolder = path.resolve(destinationFolder, '..');
|
||||
|
||||
const destinationFolderStat = await fs.promises.stat(destinationFolder);
|
||||
if (destinationFolderStat.isDirectory()) {
|
||||
if (await fileExists(destinationFolder)) {
|
||||
await fs.promises.rmdir(destinationFolder, { recursive: true });
|
||||
}
|
||||
await CloudRunnerSystem.Run(
|
||||
|
|
|
|||
Loading…
Reference in New Issue