Can enqueue indexing jobs

This commit is contained in:
Timothy Baldridge 2019-12-29 22:49:37 -07:00
parent 82be6f304b
commit a7bf8f42ed
9 changed files with 106 additions and 8 deletions

View File

@ -9,9 +9,23 @@ namespace Wabbajack.CacheServer.DTOs.JobQueue
{
public abstract class AJobPayload
{
public static List<Type> KnowSubtypes = new List<Type>();
public static List<Type> KnownSubTypes = new List<Type> {typeof(IndexJob)};
public static Dictionary<Type, string> TypeToName { get; set; }
public static Dictionary<string, Type> NameToType { get; set; }
[BsonIgnore]
public abstract string Description { get; }
public virtual bool UsesNexus { get; } = false;
public abstract JobResult Execute();
static AJobPayload()
{
NameToType = KnownSubTypes.ToDictionary(t => t.FullName.Substring(t.Namespace.Length + 1), t => t);
TypeToName = NameToType.ToDictionary(k => k.Value, k => k.Key);
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.CacheServer.DTOs.JobQueue
{
public class IndexJob : AJobPayload
{
public AbstractDownloadState State { get; set; }
public override string Description { get; } = "Validate and index an archive";
public override bool UsesNexus { get => State is NexusDownloader.State; }
public override JobResult Execute()
{
throw new NotImplementedException();
}
}
}

View File

@ -19,8 +19,8 @@ namespace Wabbajack.CacheServer.DTOs.JobQueue
[BsonId]
public Guid Id { get; set; }
public DateTime Started { get; set; }
public DateTime Ended { get; set; }
public DateTime? Started { get; set; }
public DateTime? Ended { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
public JobPriority Priority { get; set; } = JobPriority.Normal;
public bool RequiresNexus { get; set; } = true;

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
namespace Wabbajack.CacheServer.DTOs.JobQueue
{
public class JobResult
{
public JobResultType ResultType { get; set; }
[BsonIgnoreIfNull]
public string Message { get; set; }
[BsonIgnoreIfNull]
public string Stacktrace { get; set; }
public static JobResult Success()
{
return new JobResult { ResultType = JobResultType.Success };
}
public static JobResult Error(Exception ex)
{
return new JobResult {ResultType = JobResultType.Error, Stacktrace = ex.ToString()};
}
}
public enum JobResultType
{
Success,
Error
}
}

View File

@ -12,10 +12,6 @@ namespace Wabbajack.CacheServer.DTOs
{
public class ModListStatus
{
static ModListStatus()
{
SerializerSettings.Init();
}
[BsonId]
public string Id { get; set; }

View File

@ -7,6 +7,7 @@ using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using Wabbajack.CacheServer.DTOs.JobQueue;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.CacheServer.DTOs
@ -18,6 +19,11 @@ namespace Wabbajack.CacheServer.DTOs
var dis = new TypeDiscriminator(typeof(AbstractDownloadState), AbstractDownloadState.NameToType,
AbstractDownloadState.TypeToName);
BsonSerializer.RegisterDiscriminatorConvention(typeof(AbstractDownloadState), dis);
BsonClassMap.RegisterClassMap<AbstractDownloadState>(cm => cm.SetIsRootClass(true));
dis = new TypeDiscriminator(typeof(AJobPayload), AJobPayload.NameToType, AJobPayload.TypeToName);
BsonSerializer.RegisterDiscriminatorConvention(typeof(AJobPayload), dis);
BsonClassMap.RegisterClassMap<AJobPayload>(cm => cm.SetIsRootClass(true));
}
}
@ -44,7 +50,7 @@ namespace Wabbajack.CacheServer.DTOs
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
{
Type type = defaultType;
Type type = null;
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.FindElement(ElementName))
@ -55,6 +61,8 @@ namespace Wabbajack.CacheServer.DTOs
}
bsonReader.ReturnToBookmark(bookmark);
if (type == null)
throw new Exception($"Type mis-configuration can't find bson type for ${nominalType}");
return type;
}

View File

@ -15,6 +15,7 @@ namespace Wabbajack.CacheServer
public JobQueueEndpoints() : base ("/jobs")
{
Get("/", HandleListJobs);
Get("/enqueue_curated_for_indexing", HandleEnqueueAllCurated);
}
private readonly Func<object, string> HandleListJobsTemplate = NettleEngine.GetCompiler().Compile(@"
@ -47,5 +48,24 @@ namespace Wabbajack.CacheServer
response.ContentType = "text/html";
return response;
}
private async Task<string> HandleEnqueueAllCurated(object arg)
{
var states = await Server.Config.ListValidation.Connect()
.AsQueryable()
.SelectMany(lst => lst.DetailedStatus.Archives)
.Select(a => a.Archive.State)
.ToListAsync();
var jobs = states.Select(state => new IndexJob {State = state})
.Select(j => new Job {Payload = j, RequiresNexus = j.UsesNexus})
.ToList();
if (jobs.Count > 0)
await Server.Config.JobQueue.Connect().InsertManyAsync(jobs);
return $"Enqueued {states.Count} jobs";
}
}
}

View File

@ -5,6 +5,7 @@ using Nancy.Bootstrapper;
using Nancy.Configuration;
using Nancy.Hosting.Self;
using Nancy.TinyIoc;
using Wabbajack.CacheServer.DTOs;
using Wabbajack.CacheServer.ServerConfig;
using Wabbajack.Common;
@ -18,6 +19,7 @@ namespace Wabbajack.CacheServer
static Server()
{
SerializerSettings.Init();
}

View File

@ -74,7 +74,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DTOs\JobQueue\AJobPayload.cs" />
<Compile Include="DTOs\JobQueue\IndexJob.cs" />
<Compile Include="DTOs\JobQueue\Job.cs" />
<Compile Include="DTOs\JobQueue\JobResult.cs" />
<Compile Include="DTOs\Metric.cs" />
<Compile Include="DTOs\ModListStatus.cs" />
<Compile Include="DTOs\MongoDoc.cs" />