wabbajack/Wabbajack.BuildServer/Models/Jobs/GetNexusUpdatesJob.cs

82 lines
3.1 KiB
C#
Raw Normal View History

2020-01-09 04:42:25 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wabbajack.BuildServer.Models.JobQueue;
using Wabbajack.Common;
using Wabbajack.Lib.NexusApi;
using MongoDB.Driver;
2020-01-13 04:04:46 +00:00
using Newtonsoft.Json;
2020-01-09 04:42:25 +00:00
namespace Wabbajack.BuildServer.Models.Jobs
{
public class GetNexusUpdatesJob : AJobPayload
{
2020-01-13 21:11:07 +00:00
public override string Description => "Poll the Nexus for updated mods, and clean any references to those mods";
2020-01-09 04:42:25 +00:00
public override async Task<JobResult> Execute(DBContext db, AppSettings settings)
{
var api = await NexusApiClient.Get();
var gameTasks = GameRegistry.Games.Values
.Where(game => game.NexusName != null)
.Select(async game =>
{
2020-01-13 04:04:46 +00:00
var mods = await api.Get<List<NexusUpdateEntry>>(
$"https://api.nexusmods.com/v1/games/{game.NexusName}/mods/updated.json?period=1m");
var entry = new NexusCacheData<List<NexusUpdateEntry>>();
entry.Game = game.NexusName;
entry.Path = $"/v1/games/{game.NexusName}/mods/updated.json?period=1m";
entry.Data = mods;
await entry.Upsert(db.NexusUpdates);
return (game, mods);
2020-01-09 04:42:25 +00:00
})
.Select(async rTask =>
{
var (game, mods) = await rTask;
return mods.Select(mod => new { game = game, mod = mod });
}).ToList();
Utils.Log($"Getting update list for {gameTasks.Count} games");
var purge = (await Task.WhenAll(gameTasks))
.SelectMany(i => i)
.ToList();
Utils.Log($"Found {purge.Count} updated mods in the last month");
using (var queue = new WorkQueue())
{
2020-01-13 04:04:46 +00:00
var collected = purge.Select(d =>
2020-01-09 04:42:25 +00:00
{
2020-01-13 04:04:46 +00:00
var a = d.mod.LatestFileUpdate.AsUnixTime();
2020-01-09 04:42:25 +00:00
// Mod activity could hide files
2020-01-13 04:04:46 +00:00
var b = d.mod.LastestModActivity.AsUnixTime();
2020-01-09 04:42:25 +00:00
2020-01-13 04:04:46 +00:00
return new {Game = d.game.NexusName, Date = (a > b ? a : b), ModId = d.mod.ModId.ToString()};
});
var purged = await collected.PMap(queue, async t =>
2020-01-09 04:42:25 +00:00
{
var resultA = await db.NexusModInfos.DeleteManyAsync(f =>
f.Game == t.Game && f.ModId == t.ModId && f.LastCheckedUTC <= t.Date);
var resultB = await db.NexusModFiles.DeleteManyAsync(f =>
f.Game == t.Game && f.ModId == t.ModId && f.LastCheckedUTC <= t.Date);
var resultC = await db.NexusFileInfos.DeleteManyAsync(f =>
f.Game == t.Game && f.ModId == t.ModId && f.LastCheckedUTC <= t.Date);
return resultA.DeletedCount + resultB.DeletedCount + resultC.DeletedCount;
});
2020-01-13 04:04:46 +00:00
Utils.Log($"Purged {purged.Sum()} cache entries");
2020-01-09 04:42:25 +00:00
}
return JobResult.Success();
}
2020-01-13 04:04:46 +00:00
2020-01-09 04:42:25 +00:00
}
}