Move scheduler from `OnlineLookupQueue` to `BeatmapUpdater`

This commit is contained in:
Dean Herbert 2022-07-28 16:08:27 +09:00
parent fe6b487d75
commit 17a3fd30fb
2 changed files with 21 additions and 28 deletions

View File

@ -8,6 +8,7 @@
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Rulesets.Objects;
@ -20,15 +21,21 @@ namespace osu.Game.Beatmaps
public class BeatmapUpdater : IDisposable
{
private readonly IWorkingBeatmapCache workingBeatmapCache;
private readonly BeatmapOnlineLookupQueue onlineLookupQueue;
private readonly BeatmapDifficultyCache difficultyCache;
private readonly BeatmapUpdaterMetadataLookup metadataLookup;
private const int update_queue_request_concurrency = 4;
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(update_queue_request_concurrency, nameof(BeatmapUpdaterMetadataLookup));
public BeatmapUpdater(IWorkingBeatmapCache workingBeatmapCache, BeatmapDifficultyCache difficultyCache, IAPIProvider api, Storage storage)
{
this.workingBeatmapCache = workingBeatmapCache;
this.difficultyCache = difficultyCache;
onlineLookupQueue = new BeatmapOnlineLookupQueue(api, storage);
metadataLookup = new BeatmapUpdaterMetadataLookup(api, storage);
}
/// <summary>
@ -37,7 +44,7 @@ public BeatmapUpdater(IWorkingBeatmapCache workingBeatmapCache, BeatmapDifficult
public void Queue(Live<BeatmapSetInfo> beatmap)
{
Logger.Log($"Queueing change for local beatmap {beatmap}");
Task.Factory.StartNew(() => beatmap.PerformRead(Process));
Task.Factory.StartNew(() => beatmap.PerformRead(b => Process(b)), default, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler);
}
/// <summary>
@ -50,7 +57,7 @@ public void Process(BeatmapSetInfo beatmapSet) => beatmapSet.Realm.Write(r =>
// TODO: this call currently uses the local `online.db` lookup.
// We probably don't want this to happen after initial import (as the data may be stale).
onlineLookupQueue.Update(beatmapSet);
metadataLookup.Update(beatmapSet);
foreach (var beatmap in beatmapSet.Beatmaps)
{
@ -90,8 +97,11 @@ private double calculateLength(IBeatmap b)
public void Dispose()
{
if (onlineLookupQueue.IsNotNull())
onlineLookupQueue.Dispose();
if (metadataLookup.IsNotNull())
metadataLookup.Dispose();
if (updateScheduler.IsNotNull())
updateScheduler.Dispose();
}
#endregion

View File

@ -6,8 +6,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using osu.Framework.Development;
@ -15,7 +13,6 @@
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Framework.Threading;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
@ -32,20 +29,16 @@ namespace osu.Game.Beatmaps
/// This will always be checked before doing a second online query to get required metadata.
/// </remarks>
[ExcludeFromDynamicCompile]
public class BeatmapOnlineLookupQueue : IDisposable
public class BeatmapUpdaterMetadataLookup : IDisposable
{
private readonly IAPIProvider api;
private readonly Storage storage;
private const int update_queue_request_concurrency = 4;
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(update_queue_request_concurrency, nameof(BeatmapOnlineLookupQueue));
private FileWebRequest cacheDownloadRequest;
private const string cache_database_name = "online.db";
public BeatmapOnlineLookupQueue(IAPIProvider api, Storage storage)
public BeatmapUpdaterMetadataLookup(IAPIProvider api, Storage storage)
{
this.api = api;
this.storage = storage;
@ -61,15 +54,6 @@ public void Update(BeatmapSetInfo beatmapSet)
lookup(beatmapSet, b);
}
public Task UpdateAsync(BeatmapSetInfo beatmapSet, CancellationToken cancellationToken)
{
return Task.WhenAll(beatmapSet.Beatmaps.Select(b => UpdateAsync(beatmapSet, b, cancellationToken)).ToArray());
}
// todo: expose this when we need to do individual difficulty lookups.
protected Task UpdateAsync(BeatmapSetInfo beatmapSet, BeatmapInfo beatmapInfo, CancellationToken cancellationToken)
=> Task.Factory.StartNew(() => lookup(beatmapSet, beatmapInfo), cancellationToken, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler);
private void lookup(BeatmapSetInfo set, BeatmapInfo beatmapInfo)
{
if (checkLocalCache(set, beatmapInfo))
@ -134,7 +118,7 @@ private void prepareLocalCache()
File.Delete(compressedCacheFilePath);
File.Delete(cacheFilePath);
Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache download failed: {ex}", LoggingTarget.Database);
Logger.Log($"{nameof(BeatmapUpdaterMetadataLookup)}'s online cache download failed: {ex}", LoggingTarget.Database);
};
cacheDownloadRequest.Finished += () =>
@ -151,7 +135,7 @@ private void prepareLocalCache()
}
catch (Exception ex)
{
Logger.Log($"{nameof(BeatmapOnlineLookupQueue)}'s online cache extraction failed: {ex}", LoggingTarget.Database);
Logger.Log($"{nameof(BeatmapUpdaterMetadataLookup)}'s online cache extraction failed: {ex}", LoggingTarget.Database);
File.Delete(cacheFilePath);
}
finally
@ -238,12 +222,11 @@ private bool checkLocalCache(BeatmapSetInfo set, BeatmapInfo beatmapInfo)
}
private void logForModel(BeatmapSetInfo set, string message) =>
RealmArchiveModelImporter<BeatmapSetInfo>.LogForModel(set, $"[{nameof(BeatmapOnlineLookupQueue)}] {message}");
RealmArchiveModelImporter<BeatmapSetInfo>.LogForModel(set, $"[{nameof(BeatmapUpdaterMetadataLookup)}] {message}");
public void Dispose()
{
cacheDownloadRequest?.Dispose();
updateScheduler?.Dispose();
}
}
}