mirror of
https://github.com/ppy/osu.git
synced 2025-02-13 00:42:55 +08:00
Merge pull request #16495 from peppy/realm-integration/reinit-on-corrupt
Add support for starting with a fresh realm database if the existing one is not usable
This commit is contained in:
commit
a0336c6fc2
@ -21,6 +21,7 @@ using osu.Game.Stores;
|
|||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Scoring;
|
using osu.Game.Scoring;
|
||||||
using Realms;
|
using Realms;
|
||||||
|
using Realms.Exceptions;
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
|
||||||
@ -105,8 +106,20 @@ namespace osu.Game.Database
|
|||||||
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
||||||
Filename += realm_extension;
|
Filename += realm_extension;
|
||||||
|
|
||||||
// This method triggers the first `CreateContext` call, which will implicitly run realm migrations and bring the schema up-to-date.
|
try
|
||||||
cleanupPendingDeletions();
|
{
|
||||||
|
// This method triggers the first `CreateContext` call, which will implicitly run realm migrations and bring the schema up-to-date.
|
||||||
|
cleanupPendingDeletions();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.Error(e, "Realm startup failed with unrecoverable error; starting with a fresh database. A backup of your database has been made.");
|
||||||
|
|
||||||
|
CreateBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_corrupt{realm_extension}");
|
||||||
|
storage.Delete(Filename);
|
||||||
|
|
||||||
|
cleanupPendingDeletions();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanupPendingDeletions()
|
private void cleanupPendingDeletions()
|
||||||
@ -403,14 +416,23 @@ namespace osu.Game.Database
|
|||||||
const int sleep_length = 200;
|
const int sleep_length = 200;
|
||||||
int timeout = 5000;
|
int timeout = 5000;
|
||||||
|
|
||||||
// see https://github.com/realm/realm-dotnet/discussions/2657
|
try
|
||||||
while (!Compact())
|
|
||||||
{
|
{
|
||||||
Thread.Sleep(sleep_length);
|
// see https://github.com/realm/realm-dotnet/discussions/2657
|
||||||
timeout -= sleep_length;
|
while (!Compact())
|
||||||
|
{
|
||||||
|
Thread.Sleep(sleep_length);
|
||||||
|
timeout -= sleep_length;
|
||||||
|
|
||||||
if (timeout < 0)
|
if (timeout < 0)
|
||||||
throw new TimeoutException(@"Took too long to acquire lock");
|
throw new TimeoutException(@"Took too long to acquire lock");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (RealmException e)
|
||||||
|
{
|
||||||
|
// Compact may fail if the realm is in a bad state.
|
||||||
|
// We still want to continue with the blocking operation, though.
|
||||||
|
Logger.Log($"Realm compact failed with error {e}", LoggingTarget.Database);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
Loading…
Reference in New Issue
Block a user