1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-14 19:53:08 +08:00

Move database backup creation to async thread where possible

This commit is contained in:
Dean Herbert 2022-06-16 16:50:00 +09:00
parent 17dbb599d1
commit 4526f8c07d
3 changed files with 14 additions and 16 deletions

View File

@ -123,8 +123,18 @@ namespace osu.Game.Database
private void beginMigration()
{
const string backup_folder = "backups";
string backupSuffix = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
// required for initial backup.
var realmBlockOperations = realm.BlockAllOperations();
Task.Factory.StartNew(() =>
{
realm.CreateBackup(Path.Combine(backup_folder, $"client.{backupSuffix}.realm"), realmBlockOperations);
efContextFactory.CreateBackup(Path.Combine(backup_folder, $"client.{backupSuffix}.db"));
using (var ef = efContextFactory.Get())
{
realm.Write(r =>
@ -182,7 +192,6 @@ namespace osu.Game.Database
true);
const string attachment_filename = "attach_me.zip";
const string backup_folder = "backups";
var backupStorage = storage.GetStorageForDirectory(backup_folder);
@ -209,6 +218,8 @@ namespace osu.Game.Database
// If we were to not do this, the migration would run another time the next time the user starts the game.
deletePreRealmData();
realmBlockOperations.Dispose();
migrationCompleted.SetResult(true);
efContextFactory.SetMigrationCompletion();
});

View File

@ -750,9 +750,9 @@ namespace osu.Game.Database
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
efContextFactory?.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
public void CreateBackup(string backupFilename)
public void CreateBackup(string backupFilename, IDisposable? blockAllOperations = null)
{
using (BlockAllOperations())
using (blockAllOperations ?? BlockAllOperations())
{
Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database);

View File

@ -233,19 +233,6 @@ namespace osu.Game
Decoder.RegisterDependencies(RulesetStore);
// Backup is taken here rather than in EFToRealmMigrator to avoid recycling realm contexts
// after initial usages below. It can be moved once a direction is established for handling re-subscription.
// See https://github.com/ppy/osu/pull/16547 for more discussion.
if (EFContextFactory != null)
{
const string backup_folder = "backups";
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
EFContextFactory.CreateBackup(Path.Combine(backup_folder, $"client.{migration}.db"));
realm.CreateBackup(Path.Combine(backup_folder, $"client.{migration}.realm"));
}
dependencies.CacheAs(Storage);
var largeStore = new LargeTextureStore(Host.CreateTextureLoaderStore(new NamespacedResourceStore<byte[]>(Resources, @"Textures")));