Skip to content

Commit

Permalink
Merge pull request #41 from khkh-ms/khkh/ingore-notfound-error-during…
Browse files Browse the repository at this point in the history
…-delete

Ignore file/dir not found error during the delete operation
  • Loading branch information
JennyLawrance authored Oct 11, 2022
2 parents d0b9dec + 09c553e commit ecb0ac1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
16 changes: 10 additions & 6 deletions bin/kudusync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions lib/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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();
});
Expand Down
7 changes: 5 additions & 2 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ 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;

var retryAction = () => {
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 {
Expand Down
10 changes: 5 additions & 5 deletions test/attemptTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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);
Expand Down

0 comments on commit ecb0ac1

Please sign in to comment.