Skip to content

Commit 4c22a9d

Browse files
committed
Only release handles on retry
1 parent 5963dc4 commit 4c22a9d

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/Ensconce.Cli/FileInteraction.cs

+23-20
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,42 @@ internal static void DeleteDirectory(string directory)
2020
// Try and kill processes we know about in the dir
2121
ApplicationInteraction.StopProcessesInDirectory(directory);
2222

23-
// Try and kill handles in the dir
24-
ApplicationInteraction.ReleaseHandlesInDirectory(directory);
25-
2623
Logging.Log("Deleting from {0}", directory);
27-
PerformDelete(new DirectoryInfo(directory));
28-
29-
Logging.Log("Ensure Directory {0} has been deleted", directory);
30-
Retry.Do(() =>
31-
{
32-
if (Directory.Exists(directory))
33-
{
34-
throw new Exception("Directory still exists");
35-
}
36-
}, TimeSpan.FromMilliseconds(1000));
37-
}
24+
var directoryInfo = new DirectoryInfo(directory);
3825

39-
private static void PerformDelete(DirectoryInfo directory)
40-
{
4126
// Disable any read-only flags
42-
directory.Attributes = FileAttributes.Normal;
27+
directoryInfo.Attributes = FileAttributes.Normal;
4328

44-
foreach (var dir in directory.EnumerateDirectories("*", SearchOption.AllDirectories))
29+
foreach (var dir in directoryInfo.EnumerateDirectories("*", SearchOption.AllDirectories))
4530
{
4631
dir.Attributes = FileAttributes.Normal;
4732
}
4833

49-
foreach (var file in directory.EnumerateFiles("*", SearchOption.AllDirectories))
34+
foreach (var file in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories))
5035
{
5136
file.Attributes = FileAttributes.Normal;
5237
}
5338

5439
// Delete directory tree
55-
Retry.Do(() => directory.Delete(true), TimeSpan.FromMilliseconds(1000));
40+
Retry.Do(retry =>
41+
{
42+
if (retry > 0)
43+
{
44+
// Try and kill handles in the dir
45+
ApplicationInteraction.ReleaseHandlesInDirectory(directory);
46+
}
47+
48+
directoryInfo.Delete(true);
49+
}, TimeSpan.FromMilliseconds(1000));
50+
51+
Logging.Log("Ensure Directory {0} has been deleted", directory);
52+
Retry.Do(() =>
53+
{
54+
if (directoryInfo.Exists)
55+
{
56+
throw new Exception("Directory still exists");
57+
}
58+
}, TimeSpan.FromMilliseconds(1000));
5659
}
5760

5861
internal static void CopyDirectory(string from, string to)

src/Ensconce.Helpers/Retry.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,34 @@ public static class Retry
1515

1616
public static void Do(Action action, TimeSpan retryInterval, Type[] doNotRetryTheseExceptions = null)
1717
{
18-
Do<object>(() =>
18+
Do(_ => action(), retryInterval, doNotRetryTheseExceptions);
19+
}
20+
21+
public static void Do(Action<int> action, TimeSpan retryInterval, Type[] doNotRetryTheseExceptions = null)
22+
{
23+
Do<object>(retry =>
1924
{
20-
action();
25+
action(retry);
2126
return null;
2227
},
2328
retryInterval,
2429
doNotRetryTheseExceptions);
2530
}
2631

2732
public static T Do<T>(Func<T> action, TimeSpan retryInterval, Type[] doNotRetryTheseExceptions = null)
33+
{
34+
return Do(_ => action(), retryInterval, doNotRetryTheseExceptions);
35+
}
36+
37+
public static T Do<T>(Func<int, T> action, TimeSpan retryInterval, Type[] doNotRetryTheseExceptions = null)
2838
{
2939
var exceptions = new List<Exception>();
3040

3141
for (var retry = 0; retry < retryCount; retry++)
3242
{
3343
try
3444
{
35-
return action();
45+
return action(retry);
3646
}
3747
catch (Exception ex) when (doNotRetryTheseExceptions?.Any(t => ex.GetType() == t) == true)
3848
{

0 commit comments

Comments
 (0)