1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-07 15:32:58 +08:00

Add retry loop to avoid log export failing occasionally on windows

Closes https://github.com/ppy/osu/issues/26693.
This commit is contained in:
Dean Herbert 2024-01-25 13:53:31 +09:00
parent b272d34960
commit d2990170d0
No known key found for this signature in database
3 changed files with 80 additions and 36 deletions

View File

@ -6,8 +6,8 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using osu.Framework.Platform;
using osu.Game.Utils;
namespace osu.Game.IO
{
@ -81,7 +81,7 @@ namespace osu.Game.IO
if (IgnoreSuffixes.Any(suffix => fi.Name.EndsWith(suffix, StringComparison.Ordinal)))
continue;
allFilesDeleted &= AttemptOperation(() => fi.Delete(), throwOnFailure: false);
allFilesDeleted &= FileUtils.AttemptOperation(() => fi.Delete(), throwOnFailure: false);
}
foreach (DirectoryInfo dir in target.GetDirectories())
@ -92,11 +92,11 @@ namespace osu.Game.IO
if (IgnoreSuffixes.Any(suffix => dir.Name.EndsWith(suffix, StringComparison.Ordinal)))
continue;
allFilesDeleted &= AttemptOperation(() => dir.Delete(true), throwOnFailure: false);
allFilesDeleted &= FileUtils.AttemptOperation(() => dir.Delete(true), throwOnFailure: false);
}
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
allFilesDeleted &= AttemptOperation(target.Delete, throwOnFailure: false);
allFilesDeleted &= FileUtils.AttemptOperation(target.Delete, throwOnFailure: false);
return allFilesDeleted;
}
@ -115,7 +115,7 @@ namespace osu.Game.IO
if (IgnoreSuffixes.Any(suffix => fileInfo.Name.EndsWith(suffix, StringComparison.Ordinal)))
continue;
AttemptOperation(() =>
FileUtils.AttemptOperation(() =>
{
fileInfo.Refresh();
@ -139,35 +139,5 @@ namespace osu.Game.IO
CopyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
}
}
/// <summary>
/// Attempt an IO operation multiple times and only throw if none of the attempts succeed.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="attempts">The number of attempts (250ms wait between each).</param>
/// <param name="throwOnFailure">Whether to throw an exception on failure. If <c>false</c>, will silently fail.</param>
protected static bool AttemptOperation(Action action, int attempts = 10, bool throwOnFailure = true)
{
while (true)
{
try
{
action();
return true;
}
catch (Exception)
{
if (attempts-- == 0)
{
if (throwOnFailure)
throw;
return false;
}
}
Thread.Sleep(250);
}
}
}
}

View File

@ -15,6 +15,7 @@ using osu.Game.Localisation;
using osu.Game.Overlays.Notifications;
using osu.Game.Overlays.Settings.Sections.Maintenance;
using osu.Game.Updater;
using osu.Game.Utils;
using SharpCompress.Archives.Zip;
namespace osu.Game.Overlays.Settings.Sections.General
@ -111,7 +112,8 @@ namespace osu.Game.Overlays.Settings.Sections.General
using (var outStream = storage.CreateFileSafely(archive_filename))
using (var zip = ZipArchive.Create())
{
foreach (string? f in logStorage.GetFiles(string.Empty, "*.log")) zip.AddEntry(f, logStorage.GetStream(f), true);
foreach (string? f in logStorage.GetFiles(string.Empty, "*.log"))
FileUtils.AttemptOperation(z => z.AddEntry(f, logStorage.GetStream(f), true), zip);
zip.SaveTo(outStream);
}

View File

@ -0,0 +1,72 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Threading;
namespace osu.Game.Utils
{
public static class FileUtils
{
/// <summary>
/// Attempt an IO operation multiple times and only throw if none of the attempts succeed.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="state">The provided state.</param>
/// <param name="attempts">The number of attempts (250ms wait between each).</param>
/// <param name="throwOnFailure">Whether to throw an exception on failure. If <c>false</c>, will silently fail.</param>
public static bool AttemptOperation<T>(Action<T> action, T state, int attempts = 10, bool throwOnFailure = true)
{
while (true)
{
try
{
action(state);
return true;
}
catch (Exception)
{
if (attempts-- == 0)
{
if (throwOnFailure)
throw;
return false;
}
}
Thread.Sleep(250);
}
}
/// <summary>
/// Attempt an IO operation multiple times and only throw if none of the attempts succeed.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="attempts">The number of attempts (250ms wait between each).</param>
/// <param name="throwOnFailure">Whether to throw an exception on failure. If <c>false</c>, will silently fail.</param>
public static bool AttemptOperation(Action action, int attempts = 10, bool throwOnFailure = true)
{
while (true)
{
try
{
action();
return true;
}
catch (Exception)
{
if (attempts-- == 0)
{
if (throwOnFailure)
throw;
return false;
}
}
Thread.Sleep(250);
}
}
}
}