Skip to content

Commit

Permalink
Added retry logic to the cp() method (#707)
Browse files Browse the repository at this point in the history
Co-authored-by: sdobrodeev <[email protected]>
  • Loading branch information
egor-bryzgalov and sdobrodeev authored Feb 12, 2021
1 parent f844a24 commit 0d334a3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
3 changes: 2 additions & 1 deletion node/Strings/resources.resjson/en-US/resources.resjson
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"loc.messages.LIB_OperationFailed": "Failed %s: %s",
"loc.messages.LIB_UseFirstGlobMatch": "Multiple workspace matches. using first.",
"loc.messages.LIB_MergeTestResultNotSupported": "Merging test results from multiple files to one test run is not supported on this version of build agent for OSX/Linux, each test result file will be published as a separate test run in VSO/TFS.",
"loc.messages.LIB_PlatformNotSupported": "Platform not supported: %s"
"loc.messages.LIB_PlatformNotSupported": "Platform not supported: %s",
"loc.messages.LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s"
}
1 change: 1 addition & 0 deletions node/docs/azure-pipelines-task-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ source | string | source path
dest | string | destination path
options | string | string \-r, \-f or \-rf for recursive and force
continueOnError | boolean | optional. whether to continue on error
retryCount | number | optional. Retry count to copy the file. It might help to resolve intermittent issues e.g. with UNC target paths on a remote host.

<br/>
<div id="taskmv">
Expand Down
3 changes: 2 additions & 1 deletion node/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"LIB_OperationFailed": "Failed %s: %s",
"LIB_UseFirstGlobMatch": "Multiple workspace matches. using first.",
"LIB_MergeTestResultNotSupported": "Merging test results from multiple files to one test run is not supported on this version of build agent for OSX/Linux, each test result file will be published as a separate test run in VSO/TFS.",
"LIB_PlatformNotSupported": "Platform not supported: %s"
"LIB_PlatformNotSupported": "Platform not supported: %s",
"LIB_CopyFileFailed": "Error while copying the file. Attempts left: %s"
}
}
2 changes: 1 addition & 1 deletion node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "2.12.1",
"version": "2.12.2",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down
37 changes: 27 additions & 10 deletions node/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ export interface FsOptions {
}

export function writeFile(file: string, data: string | Buffer, options?: string | FsOptions) {
if(typeof(options) === 'string'){
fs.writeFileSync(file, data, {encoding: options});
if (typeof (options) === 'string') {
fs.writeFileSync(file, data, { encoding: options });
}
else {
fs.writeFileSync(file, data, options);
Expand Down Expand Up @@ -744,16 +744,33 @@ export function ls(options: string, paths: string[]): string[] {
* @param dest destination path
* @param options string -r, -f or -rf for recursive and force
* @param continueOnError optional. whether to continue on error
* @param retryCount optional. Retry count to copy the file. It might help to resolve intermittent issues e.g. with UNC target paths on a remote host.
*/
export function cp(source: string, dest: string, options?: string, continueOnError?: boolean): void {
if (options) {
shell.cp(options, source, dest);
}
else {
shell.cp(source, dest);
export function cp(source: string, dest: string, options?: string, continueOnError?: boolean, retryCount: number = 0): void {
while (retryCount >= 0) {
if (options) {
shell.cp(options, source, dest);
}
else {
shell.cp(source, dest);
}
try {
_checkShell('cp', false);
break;
} catch (e) {
if (retryCount <= 0) {
if (continueOnError) {
warning(e);
break;
} else {
throw e;
}
} else {
console.log(loc('LIB_CopyFileFailed', retryCount));
retryCount--;
}
}
}

_checkShell('cp', continueOnError);
}

/**
Expand Down

0 comments on commit 0d334a3

Please sign in to comment.