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

72 lines
2.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using Wabbajack.BuildServer.Model.Models;
using Wabbajack.BuildServer.Models.JobQueue;
using Wabbajack.Common;
using Wabbajack.VirtualFileSystem;
namespace Wabbajack.BuildServer.Models.Jobs
{
public class ReindexArchives : AJobPayload
{
public override string Description => "Reindex all files in the mod archive folder";
public override async Task<JobResult> Execute(DBContext db, SqlService sql, AppSettings settings)
{
using (var queue = new WorkQueue())
{
2020-03-28 20:42:45 +00:00
var files = settings.ArchiveDir.EnumerateFiles()
.Where(f => f.Extension != Consts.HashFileExtension)
.ToList();
var total_count = files.Count;
int completed = 0;
await files.PMap(queue, async file =>
{
try
{
Interlocked.Increment(ref completed);
if (await sql.HaveIndexdFile(await file.FileHashCachedAsync()))
{
2020-03-28 20:42:45 +00:00
Utils.Log($"({completed}/{total_count}) Skipping {file.FileName}, it's already indexed");
return;
}
var sub_folder = Guid.NewGuid().ToString();
2020-03-28 20:42:45 +00:00
var folder = settings.DownloadDir.Combine(sub_folder);
Utils.Log($"({completed}/{total_count}) Copying {file}");
2020-03-28 20:42:45 +00:00
folder.CreateDirectory();
Utils.Log($"({completed}/{total_count}) Copying {file}");
2020-03-28 20:42:45 +00:00
file.CopyTo(folder.Combine(file.FileName));
Utils.Log($"({completed}/{total_count}) Analyzing {file}");
var vfs = new Context(queue, true);
await vfs.AddRoot(folder);
var root = vfs.Index.ByRootPath.First().Value;
Utils.Log($"({completed}/{total_count}) Ingesting {root.ThisAndAllChildren.Count()} files");
await sql.MergeVirtualFile(root);
Utils.Log($"({completed}/{total_count}) Cleaning up {file}");
2020-03-28 20:42:45 +00:00
await Utils.DeleteDirectory(folder);
}
catch (Exception ex)
{
Utils.Log(ex.ToString());
}
});
}
return JobResult.Success();
}
}
}