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

89 lines
3.3 KiB
C#
Raw Normal View History

2020-01-10 13:16:41 +00:00
using System;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using System.Linq;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Wabbajack.BuildServer.Model.Models;
2020-01-10 13:16:41 +00:00
using Wabbajack.BuildServer.Models.JobQueue;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.ModListRegistry;
namespace Wabbajack.BuildServer.Models.Jobs
{
public class EnqueueAllArchives : AJobPayload, IBackEndJob
2020-01-10 13:16:41 +00:00
{
public override string Description => "Add missing modlist archives to indexer";
public override async Task<JobResult> Execute(DBContext db, SqlService sql, AppSettings settings)
2020-01-10 13:16:41 +00:00
{
2020-01-13 21:11:07 +00:00
Utils.Log("Starting ModList indexing");
2020-01-10 13:16:41 +00:00
var modlists = await ModlistMetadata.LoadFromGithub();
using (var queue = new WorkQueue())
{
foreach (var list in modlists)
{
try
{
2020-01-11 04:34:01 +00:00
await EnqueueFromList(db, list, queue);
2020-01-10 13:16:41 +00:00
}
catch (Exception ex)
{
Utils.Log(ex.ToString());
}
}
}
return JobResult.Success();
}
2020-01-11 04:34:01 +00:00
private static async Task EnqueueFromList(DBContext db, ModlistMetadata list, WorkQueue queue)
2020-01-10 13:16:41 +00:00
{
var existing = await db.ModListStatus.FindOneAsync(l => l.Id == list.Links.MachineURL);
var modlist_path = Path.Combine(Consts.ModListDownloadFolder,
2020-01-22 00:41:32 +00:00
list.Links.MachineURL + Consts.ModListExtension);
2020-01-10 13:16:41 +00:00
if (list.NeedsDownload(modlist_path))
{
if (File.Exists(modlist_path))
File.Delete(modlist_path);
var state = DownloadDispatcher.ResolveArchive(list.Links.Download);
Utils.Log($"Downloading {list.Links.MachineURL} - {list.Title}");
await state.Download(modlist_path);
}
else
{
2020-01-13 21:11:07 +00:00
Utils.Log($"No changes detected from downloaded ModList");
2020-01-10 13:16:41 +00:00
}
Utils.Log($"Loading {modlist_path}");
var installer = AInstaller.LoadFromFile(modlist_path);
var archives = installer.Archives;
Utils.Log($"Found {archives.Count} archives in {installer.Name} to index");
var searching = archives.Select(a => a.Hash).Distinct().ToArray();
Utils.Log($"Looking for missing archives");
var knownArchives = (await db.IndexedFiles.AsQueryable().Where(a => searching.Contains(a.Hash))
.Select(d => d.Hash).ToListAsync()).ToDictionary(a => a);
Utils.Log($"Found {knownArchives.Count} pre-existing archives");
var missing = archives.Where(a => !knownArchives.ContainsKey(a.Hash)).ToList();
Utils.Log($"Found {missing.Count} missing archives, enqueing indexing jobs");
2020-01-11 04:34:01 +00:00
var jobs = missing.Select(a => new Job {Payload = new IndexJob {Archive = a}, Priority = Job.JobPriority.Low});
2020-01-10 13:16:41 +00:00
2020-01-13 21:11:07 +00:00
Utils.Log($"Writing jobs to the database");
2020-01-10 13:16:41 +00:00
await db.Jobs.InsertManyAsync(jobs, new InsertManyOptions {IsOrdered = false});
Utils.Log($"Done adding archives for {installer.Name}");
}
}
}