wabbajack/Wabbajack.Server/Services/NonNexusDownloadValidator.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2020-05-13 21:52:34 +00:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Logging;
using Splat;
using Wabbajack.BuildServer;
using Wabbajack.Common;
2020-05-13 22:48:33 +00:00
using Wabbajack.Lib.Downloaders;
2020-05-13 21:52:34 +00:00
using Wabbajack.Server.DataLayer;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace Wabbajack.Server.Services
{
public class NonNexusDownloadValidator : AbstractService<NonNexusDownloadValidator, int>
{
private SqlService _sql;
public NonNexusDownloadValidator(ILogger<NonNexusDownloadValidator> logger, AppSettings settings, SqlService sql)
: base(logger, settings, TimeSpan.FromHours(2))
{
_sql = sql;
}
public override async Task<int> Execute()
{
var archives = await _sql.GetNonNexusModlistArchives();
2020-05-13 22:48:33 +00:00
_logger.Log(LogLevel.Information, $"Validating {archives.Count} non-Nexus archives");
2020-05-13 21:52:34 +00:00
using var queue = new WorkQueue();
2020-05-13 22:48:33 +00:00
await DownloadDispatcher.PrepareAll(archives.Select(a => a.State));
2020-05-13 21:52:34 +00:00
var results = await archives.PMap(queue, async archive =>
{
try
{
2020-05-13 22:48:33 +00:00
bool isValid = false;
switch (archive.State)
{
case WabbajackCDNDownloader.State _:
case GoogleDriveDownloader.State _:
case ManualDownloader.State _:
case ModDBDownloader.State _:
2020-05-13 22:48:33 +00:00
case HTTPDownloader.State h when h.Url.StartsWith("https://wabbajack"):
isValid = true;
break;
default:
isValid = await archive.State.Verify(archive);
break;
}
2020-05-13 21:52:34 +00:00
return (Archive: archive, IsValid: isValid);
}
2020-05-13 22:48:33 +00:00
catch (Exception ex)
2020-05-13 21:52:34 +00:00
{
2020-05-13 22:48:33 +00:00
_logger.Log(LogLevel.Warning, $"Error for {archive.Name} {archive.State.PrimaryKeyString} {ex}");
2020-05-13 21:52:34 +00:00
return (Archive: archive, IsValid: false);
}
});
await _sql.UpdateNonNexusModlistArchivesStatus(results);
var failed = results.Count(r => !r.IsValid);
var passed = results.Count() - failed;
2020-05-13 22:48:33 +00:00
foreach(var (archive, _) in results.Where(f => !f.IsValid))
2020-05-13 21:52:34 +00:00
_logger.Log(LogLevel.Warning, $"Validation failed for {archive.Name} from {archive.State.PrimaryKeyString}");
_logger.Log(LogLevel.Information, $"Non-nexus validation completed {failed} out of {passed} failed");
return failed;
}
}
}