From 09c553e35a8607e6009e936184801f294fe5f8d4 Mon Sep 17 00:00:00 2001 From: Khuram Khan <64149947+khkh-ms@users.noreply.github.com> Date: Wed, 10 Aug 2022 17:44:54 +0000 Subject: [PATCH] Ignore file/dir not found error during the delete operation --- bin/kudusync.js | 16 ++++++++++------ lib/FileUtils.ts | 4 ++-- lib/Utils.ts | 7 +++++-- test/attemptTests.js | 10 +++++----- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/bin/kudusync.js b/bin/kudusync.js index da8be40..cf54095 100644 --- a/bin/kudusync.js +++ b/bin/kudusync.js @@ -20,17 +20,21 @@ var Utils; (function (Utils) { Utils.DefaultRetries = 3; Utils.DefaultDelayBeforeRetry = 250; - function attempt(action, retries, delayBeforeRetry) { + function attempt(action, ignoreError, retries, delayBeforeRetry) { if (typeof retries === "undefined") { retries = Utils.DefaultRetries; } if (typeof delayBeforeRetry === "undefined") { delayBeforeRetry = Utils.DefaultDelayBeforeRetry; } Ensure.argNotNull(action, "action"); var currentTry = 1; var retryAction = function () { return action().then(Q.resolve, function (err) { - if(retries >= currentTry++) { - return Q.delay(Q.fcall(retryAction), delayBeforeRetry); + if(ignoreError && err && err.code && err.code == ignoreError) { + Q.resolve; } else { - return Q.reject(err); + if(retries >= currentTry++) { + return Q.delay(Q.fcall(retryAction), delayBeforeRetry); + } else { + return Q.reject(err); + } } }); }; @@ -445,7 +449,7 @@ function deleteFile(file, manifest, rootPath, targetSubFolder, ignoreManifest, w if(!whatIf) { return Utils.attempt(function () { return Q.nfcall(fs.unlink, path); - }); + }, "ENOENT"); } } return Q.resolve(); @@ -478,7 +482,7 @@ function deleteDirectoryRecursive(directory, manifest, rootPath, targetSubFolder if(!whatIf) { return Utils.attempt(function () { return Q.nfcall(fs.rmdir, path); - }); + }, "ENOENT"); } return Q.resolve(); }); diff --git a/lib/FileUtils.ts b/lib/FileUtils.ts index 6287968..e05ae75 100644 --- a/lib/FileUtils.ts +++ b/lib/FileUtils.ts @@ -111,7 +111,7 @@ function deleteFile(file: FileInfo, manifest: Manifest, rootPath: string, target log("Deleting file: '" + file.relativePath() + "'"); if (!whatIf) { - return Utils.attempt(() => Q.nfcall(fs.unlink, path)); + return Utils.attempt(() => Q.nfcall(fs.unlink, path), "ENOENT"); } } @@ -155,7 +155,7 @@ function deleteDirectoryRecursive(directory: DirectoryInfo, manifest: Manifest, // Delete current directory log("Deleting directory: '" + relativePath + "'"); if (!whatIf) { - return Utils.attempt(() => Q.nfcall(fs.rmdir, path)); + return Utils.attempt(() => Q.nfcall(fs.rmdir, path), "ENOENT"); } return Q.resolve(); }); diff --git a/lib/Utils.ts b/lib/Utils.ts index 84a0723..3dab5a0 100644 --- a/lib/Utils.ts +++ b/lib/Utils.ts @@ -4,7 +4,7 @@ module Utils { private DefaultRetries: number = 3; private DefaultDelayBeforeRetry: number = 250; // 250 ms - export function attempt(action: () => Promise, retries: number = DefaultRetries, delayBeforeRetry: number = DefaultDelayBeforeRetry) : Promise { + export function attempt(action: () => Promise, ignoreError: string = "", retries: number = DefaultRetries, delayBeforeRetry: number = DefaultDelayBeforeRetry) : Promise { Ensure.argNotNull(action, "action"); var currentTry = 1; @@ -12,7 +12,10 @@ module Utils { return action().then( Q.resolve, function(err?) { - if (retries >= currentTry++) { + if (ignoreError && err && err.code && err.code == ignoreError) { + Q.resolve; + } + else if (retries >= currentTry++) { return Q.delay(Q.fcall(retryAction), delayBeforeRetry); } else { diff --git a/test/attemptTests.js b/test/attemptTests.js index c770d2d..a47e64b 100644 --- a/test/attemptTests.js +++ b/test/attemptTests.js @@ -15,7 +15,7 @@ suite('Attempt Function Tests', function () { attempts.should.equal(0); attempts++; return Q.resolve(); - }, 3, 10).then(function () { + }, null, 3, 10).then(function () { attempts.should.equal(1); done(); }); @@ -31,7 +31,7 @@ suite('Attempt Function Tests', function () { } return Q.resolve(); - }, 3, 10).then(function () { + }, null, 3, 10).then(function () { attempts.should.equal(2); done(); }); @@ -47,7 +47,7 @@ suite('Attempt Function Tests', function () { } return Q.resolve(); - }, 3, 10).then(function () { + }, null, 3, 10).then(function () { attempts.should.equal(3); done(); }); @@ -63,7 +63,7 @@ suite('Attempt Function Tests', function () { } return Q.resolve(); - }, 3, 10).then(function () { + }, null, 3, 10).then(function () { attempts.should.equal(4); done(); }); @@ -79,7 +79,7 @@ suite('Attempt Function Tests', function () { } return Q.resolve(); - }, 3, 10).fail(function (err) { + }, null, 3, 10).fail(function (err) { err.should.be.ok; err.message.should.equal("error"); attempts.should.equal(4);