wabbajack/Wabbajack.Server/Services/NexusPoll.cs

98 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Linq;
2021-09-27 12:42:46 +00:00
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Wabbajack.BuildServer;
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs;
using Wabbajack.Networking.NexusApi;
using Wabbajack.Server.DataLayer;
namespace Wabbajack.Server.Services
{
public class NexusPoll
{
private SqlService _sql;
private AppSettings _settings;
private GlobalInformation _globalInformation;
private ILogger<NexusPoll> _logger;
2021-09-27 12:42:46 +00:00
private readonly ParallelOptions _parallelOptions;
private readonly NexusApi _api;
2021-09-27 12:42:46 +00:00
public NexusPoll(ILogger<NexusPoll> logger, AppSettings settings, SqlService service, GlobalInformation globalInformation, ParallelOptions parallelOptions, NexusApi api)
{
_sql = service;
_settings = settings;
_globalInformation = globalInformation;
_logger = logger;
2021-09-27 12:42:46 +00:00
_parallelOptions = parallelOptions;
_api = api;
}
2021-09-27 12:42:46 +00:00
public async Task UpdateNexusCacheAPI(CancellationToken token)
{
using var _ = _logger.BeginScope("Nexus Update via API");
2020-05-20 03:25:41 +00:00
_logger.Log(LogLevel.Information, "Starting Nexus Update via API");
2021-09-27 12:42:46 +00:00
var purged = await GameRegistry.Games.Values
.Where(game => game.NexusName != null)
2021-09-27 12:42:46 +00:00
.SelectMany(async game =>
{
2021-09-27 12:42:46 +00:00
var (mods, _) = await _api.GetUpdates(game.Game, token);
2021-09-27 12:42:46 +00:00
return mods.Select(mod => new { Game = game, Mod = mod });
})
2021-09-27 12:42:46 +00:00
.Select(async row =>
{
2021-09-27 12:42:46 +00:00
var a = row.Mod.LatestFileUpdate.AsUnixTime();
// Mod activity could hide files
var b = row.Mod.LastestModActivity.AsUnixTime();
2021-09-27 12:42:46 +00:00
var t = (a > b) ? a : b;
2021-09-27 12:42:46 +00:00
long purgeCount = 0;
purgeCount += await _sql.DeleteNexusModInfosUpdatedBeforeDate(row.Game.Game, row.Mod.ModId, t.Date);
purgeCount += await _sql.DeleteNexusModFilesUpdatedBeforeDate(row.Game.Game, row.Mod.ModId, t.Date);
return purgeCount;
})
.SumAsync<long>(x => x);
_logger.Log(LogLevel.Information, "Purged {count} cache entries", purged);
2020-05-09 22:16:16 +00:00
_globalInformation.LastNexusSyncUTC = DateTime.UtcNow;
}
public void Start()
{
2020-05-09 13:04:38 +00:00
if (!_settings.RunBackEndJobs) return;
2021-09-27 12:42:46 +00:00
Task.Run(async () =>
{
while (true)
{
try
{
2021-09-27 12:42:46 +00:00
await UpdateNexusCacheAPI(CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting API feed from Nexus");
}
await Task.Delay(_globalInformation.NexusAPIPollRate);
}
});
}
}
public static class NexusPollExtensions
{
public static void UseNexusPoll(this IApplicationBuilder b)
{
var poll = (NexusPoll)b.ApplicationServices.GetService(typeof(NexusPoll));
poll.Start();
}
}
}