OPDS Cleanup (#534)

* Fixed opds url display

* Rewrote how stat collection works, now we check in multiple places and always run stat collection in a background thread, to not block main thread.

* Cleaned up the ParseInfoTest to be more verbose

* Added benchmarking
This commit is contained in:
Joseph Milazzo 2021-08-28 15:32:24 -07:00 committed by GitHub
parent d36c3d62ce
commit 51b9d1a45a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 133 additions and 52 deletions

View file

@ -1,4 +1,6 @@
namespace API.Interfaces
using System.Threading.Tasks;
namespace API.Interfaces
{
public interface ITaskScheduler
{
@ -6,7 +8,7 @@
/// For use on Server startup
/// </summary>
void ScheduleTasks();
void ScheduleStatsTasks();
Task ScheduleStatsTasks();
void ScheduleUpdaterTasks();
void ScanLibrary(int libraryId, bool forceUpdate = false);
void CleanupChapters(int[] chapterIds);
@ -15,5 +17,6 @@
void RefreshSeriesMetadata(int libraryId, int seriesId);
void ScanSeries(int libraryId, int seriesId, bool forceUpdate = false);
void CancelStatsTasks();
void RunStatCollection();
}
}

View file

@ -6,8 +6,6 @@ namespace API.Interfaces.Services
public interface IStatsService
{
Task PathData(ClientInfoDto clientInfoDto);
Task FinalizeStats();
Task CollectRelevantData();
Task CollectAndSendStatsData();
}
}

View file

@ -2,7 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using API.Interfaces;
using API.Interfaces.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -27,7 +26,10 @@ namespace API.Services.HostedServices
try
{
await ManageStartupStatsTasks(scope, taskScheduler);
// These methods will automatically check if stat collection is disabled to prevent sending any data regardless
// of when setting was changed
await taskScheduler.ScheduleStatsTasks();
taskScheduler.RunStatCollection();
}
catch (Exception)
{
@ -35,21 +37,6 @@ namespace API.Services.HostedServices
}
}
private async Task ManageStartupStatsTasks(IServiceScope serviceScope, ITaskScheduler taskScheduler)
{
var unitOfWork = serviceScope.ServiceProvider.GetRequiredService<IUnitOfWork>();
var settingsDto = await unitOfWork.SettingsRepository.GetSettingsDtoAsync();
if (!settingsDto.AllowStatCollection) return;
taskScheduler.ScheduleStatsTasks();
var statsService = serviceScope.ServiceProvider.GetRequiredService<IStatsService>();
await statsService.CollectAndSendStatsData();
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}

View file

@ -22,6 +22,7 @@ namespace API.Services
private readonly IStatsService _statsService;
private readonly IVersionUpdaterService _versionUpdaterService;
private const string SendDataTask = "finalize-stats";
public static BackgroundJobServer Client => new BackgroundJobServer();
@ -76,19 +77,17 @@ namespace API.Services
#region StatsTasks
private const string SendDataTask = "finalize-stats";
public void ScheduleStatsTasks()
public async Task ScheduleStatsTasks()
{
var allowStatCollection = bool.Parse(Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.AllowStatCollection)).GetAwaiter().GetResult().Value);
var allowStatCollection = (await _unitOfWork.SettingsRepository.GetSettingsDtoAsync()).AllowStatCollection;
if (!allowStatCollection)
{
_logger.LogDebug("User has opted out of stat collection, not registering tasks");
return;
}
_logger.LogDebug("Adding StatsTasks");
_logger.LogDebug("Scheduling Send data to the Stats server {Setting}", nameof(Cron.Daily));
_logger.LogDebug("Scheduling stat collection daily");
RecurringJob.AddOrUpdate(SendDataTask, () => _statsService.CollectAndSendStatsData(), Cron.Daily);
}
@ -99,6 +98,12 @@ namespace API.Services
RecurringJob.RemoveIfExists(SendDataTask);
}
public void RunStatCollection()
{
_logger.LogInformation("Enqueuing stat collection");
BackgroundJob.Enqueue(() => _statsService.CollectAndSendStatsData());
}
#endregion
#region UpdateTasks

View file

@ -50,7 +50,7 @@ namespace API.Services.Tasks
await SaveFile(statisticsDto);
}
public async Task CollectRelevantData()
private async Task CollectRelevantData()
{
_logger.LogDebug("Collecting data from the server and database");
@ -63,7 +63,7 @@ namespace API.Services.Tasks
await PathData(serverInfo, usageInfo);
}
public async Task FinalizeStats()
private async Task FinalizeStats()
{
try
{
@ -86,6 +86,12 @@ namespace API.Services.Tasks
public async Task CollectAndSendStatsData()
{
var allowStatCollection = (await _unitOfWork.SettingsRepository.GetSettingsDtoAsync()).AllowStatCollection;
if (!allowStatCollection)
{
_logger.LogDebug("User has opted out of stat collection, not registering tasks");
return;
}
await CollectRelevantData();
await FinalizeStats();
}