Beispiel #1
0
        void DeleteFile(string path, FailureOptions options, RetryTracker retry, CancellationToken cancel)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return;
            }

            retry.Reset();
            while (retry.Try())
            {
                cancel.ThrowIfCancellationRequested();
                try
                {
                    if (File.Exists(path))
                    {
                        if (retry.IsNotFirstAttempt)
                        {
                            File.SetAttributes(path, FileAttributes.Normal);
                        }
                        File.Delete(path);
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (retry.CanRetry())
                    {
                        if (retry.ShouldLogWarning())
                        {
                            Log.VerboseFormat("Retry #{0} on delete file '{1}'. Exception: {2}", retry.CurrentTry, path, ex.Message);
                        }
                        Thread.Sleep(retry.Sleep());
                    }
                    else
                    {
                        if (options == FailureOptions.ThrowOnFailure)
                        {
                            throw;
                        }
                    }
                }
            }
        }
        public void ShouldThrowOnceRetriesExceeded()
        {
            const int retries = 100;
            var subject = new RetryTracker(100, null, new RetryInterval(100, 200, 2));
            Exception caught = null;
            var retried = 0;

            try
            {
                while (subject.Try())
                {
                    try
                    {
                        throw new Exception("Blah");
                    }
                    catch
                    {
                        if (subject.CanRetry())
                        {
                            //swallow exception
                            retried++;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                caught = ex;
            }

            Assert.NotNull(caught);
            Assert.AreEqual(retries, retried);
        }
        void PurgeDirectory(string targetDirectory, Predicate <IFileSystemInfo> exclude, FailureOptions options, CancellationToken cancel, bool includeTarget = false, RetryTracker retry = null)
        {
            exclude = exclude ?? (fi => false);

            if (!DirectoryExists(targetDirectory))
            {
                return;
            }

            retry = retry ?? GetFileOperationRetryTracker();

            foreach (var file in EnumerateFiles(targetDirectory))
            {
                cancel.ThrowIfCancellationRequested();

                var includeInfo = new FileSystemInfoAdapter(new FileInfo(file));
                if (exclude(includeInfo))
                {
                    continue;
                }

                DeleteFile(file, options, retry, cancel);
            }

            foreach (var directory in EnumerateDirectories(targetDirectory))
            {
                cancel.ThrowIfCancellationRequested();

                var info        = new DirectoryInfo(directory);
                var includeInfo = new FileSystemInfoAdapter(info);
                if (exclude(includeInfo))
                {
                    continue;
                }

                if ((info.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                {
                    Directory.Delete(directory, false);
                }
                else
                {
                    PurgeDirectory(directory, exclude, options, cancel, true, retry);
                }
            }

            if (includeTarget && DirectoryIsEmpty(targetDirectory))
            {
                DeleteDirectory(targetDirectory, options);
            }
        }