Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors thrown by retrieve file stream #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions lib/create-remote-cache-retrieve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdir, writeFile } from "fs/promises";
import { mkdir, writeFile, rm } from "fs/promises";
import type { RemoteCache } from "nx/src/tasks-runner/default-tasks-runner";
import { join } from "path";
import { Readable } from "stream";
Expand All @@ -7,6 +7,7 @@ import { extract } from "tar";
import { createFilterSourceFile } from "./create-filter-source-file";
import { getFileNameFromHash } from "./get-file-name-from-hash";
import { SafeRemoteCacheImplementation } from "./types/safe-remote-cache-implementation";
import * as log from "./log";

const COMMIT_FILE_EXTENSION = ".commit";
const COMMIT_FILE_CONTENT = "true";
Expand All @@ -32,6 +33,11 @@ const writeCommitFile = (destination: string) => {
return writeFile(commitFilePath, COMMIT_FILE_CONTENT);
};

const deleteCommitFile = (destination: string) => {
const commitFilePath = destination + COMMIT_FILE_EXTENSION;
return rm(commitFilePath, { force: true, maxRetries: 10 });
};

export const createRemoteCacheRetrieve =
(
safeImplementation: Promise<SafeRemoteCacheImplementation | null>
Expand All @@ -44,22 +50,34 @@ export const createRemoteCacheRetrieve =
}

const file = getFileNameFromHash(hash);
const { fileExists, retrieveFile } = implementation;
const isFileCached = await fileExists(file);
const destination = join(cacheDirectory, hash);

if (!isFileCached) {
return false;
}
try {
const {fileExists, retrieveFile} = implementation;
const isFileCached = await fileExists(file);

const stream = await retrieveFile(file);
const destination = join(cacheDirectory, hash);
if (!isFileCached) {
return false;
}

if (!stream) {
return false;
}
const stream = await retrieveFile(file);

if (!stream) {
return false;
}

await extractFolder(stream, destination, hash);
await writeCommitFile(destination);

return true;
} catch (error) {

await extractFolder(stream, destination, hash);
await writeCommitFile(destination);
log.retrieveFailure(implementation, file, error);

return true;
await rm(destination, { force: true, recursive: true, maxRetries: 10 });
await deleteCommitFile(destination);


return false;
}
};
3 changes: 2 additions & 1 deletion lib/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { green, grey, red, yellow } from "chalk";
import { RemoteCacheImplementation } from "./types/remote-cache-implementation";
import { SafeRemoteCacheImplementation } from './types/safe-remote-cache-implementation';

const DELIMITER_LENGTH = 78;
const DELIMITER = Array.from({ length: DELIMITER_LENGTH }, () => "-").join("");
Expand All @@ -15,7 +16,7 @@ export const retrieveSuccess = (
) => log(formatSection(`Remote cache hit: ${green(name)}`, `File: ${file}`));

export const retrieveFailure = (
{ name }: RemoteCacheImplementation,
{ name }: RemoteCacheImplementation | SafeRemoteCacheImplementation,
file: string,
error: any
) =>
Expand Down