Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#58 - centralize and make directory deleting more robust #59

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/SIL.Machine.AspNetCore/Services/LocalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,7 @@ public LocalStorage(string basePath)

public override void Dispose()
{
DeleteRecursive();
Directory.Delete(_basePath + "/");
}

private void DeleteRecursive(string? path = null)
{
path ??= _basePath + "/";
foreach (var subDir in Directory.GetDirectories(path))
{
DeleteRecursive(subDir);
Directory.Delete(subDir);
}
foreach (var subPath in Directory.GetFiles(path))
{
File.Delete(subPath);
}
DirectoryHelper.DeleteDirectoryRobust(_basePath + "/");
}

public override Task<bool> Exists(string path, CancellationToken cancellationToken = default)
Expand Down
8 changes: 2 additions & 6 deletions src/SIL.Machine.AspNetCore/Services/ThotSmtModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ public void Cleanup(string engineId)
string engineDir = Path.Combine(_engineOptions.CurrentValue.EnginesDir, engineId);
if (!Directory.Exists(engineDir))
return;
string lmDir = Path.Combine(engineDir, "lm");
if (Directory.Exists(lmDir))
Directory.Delete(lmDir, true);
string tmDir = Path.Combine(engineDir, "tm");
if (Directory.Exists(tmDir))
Directory.Delete(tmDir, true);
DirectoryHelper.DeleteDirectoryRobust(Path.Combine(engineDir, "lm"));
DirectoryHelper.DeleteDirectoryRobust(Path.Combine(engineDir, "tm"));
string smtConfigFileName = Path.Combine(engineDir, "smt.cfg");
if (File.Exists(smtConfigFileName))
File.Delete(smtConfigFileName);
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Machine.Translation.Thot/ThotSmtModelTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ private static bool IsSegmentValid(ParallelTextRow segment)

protected override void DisposeManagedResources()
{
Directory.Delete(_tempDir, true);
DirectoryHelper.DeleteDirectoryRobust(_tempDir);
}

private static ParallelTextRow EscapeTokens(ParallelTextRow row)
Expand Down
47 changes: 47 additions & 0 deletions src/SIL.Machine/Utils/DirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Threading;

namespace SIL.Machine.Utils
{
public static class DirectoryHelper
{
public static void DeleteDirectoryRobust(string path)
{
// Catch and retry logic based off of this post:
// https://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true

if (Directory.Exists(path))
{
foreach (string directory in Directory.GetDirectories(path))
{
DeleteDirectoryRobust(directory);
}

try
{
Thread.Sleep(0);
Directory.Delete(path, true);
}
catch (IOException)
{
try
{
Thread.Sleep(1000);
Directory.Delete(path, true);
}
catch (Exception) { }
}
catch (UnauthorizedAccessException)
{
try
{
Thread.Sleep(1000);
Directory.Delete(path, true);
}
catch (Exception) { }
}
}
}
}
}
31 changes: 2 additions & 29 deletions src/SIL.Machine/Utils/TempDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,15 @@ public class TempDirectory : DisposableBase
public TempDirectory(string name)
{
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), name);
DeleteFolderThatMayBeInUse();
DirectoryHelper.DeleteDirectoryRobust(Path);
Directory.CreateDirectory(Path);
}

public string Path { get; }

protected override void DisposeManagedResources()
{
DeleteFolderThatMayBeInUse();
}

private void DeleteFolderThatMayBeInUse()
{
if (Directory.Exists(Path))
{
try
{
Directory.Delete(Path, true);
}
catch (Exception)
{
try
{
//maybe we can at least clear it out a bit
string[] files = Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories);
foreach (string s in files)
{
File.Delete(s);
}
//sleep and try again (seems to work)
Task.Delay(1000).Wait();
Directory.Delete(Path, true);
}
catch (Exception) { }
}
}
DirectoryHelper.DeleteDirectoryRobust(Path);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using SIL.Machine.Utils;

namespace SIL.Machine.Tokenization.SentencePiece
{
Expand All @@ -16,8 +17,7 @@ public class SentencePieceTokenizerTests
public void CreateModel()
{
_tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
if (Directory.Exists(_tempDir))
Directory.Delete(_tempDir, recursive: true);
DirectoryHelper.DeleteDirectoryRobust(_tempDir);
Directory.CreateDirectory(_tempDir);
var trainer = new SentencePieceTrainer { VocabSize = 100 };
trainer.Train(TestFilename, Path.Combine(_tempDir, "sp"));
Expand All @@ -26,8 +26,7 @@ public void CreateModel()
[OneTimeTearDown]
public void DeleteModel()
{
if (Directory.Exists(_tempDir))
Directory.Delete(_tempDir, recursive: true);
DirectoryHelper.DeleteDirectoryRobust(_tempDir);
}

[Test]
Expand Down
Loading