2020-01-08 04:41:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.BuildServer.Models.JobQueue
|
|
|
|
|
{
|
|
|
|
|
public class Job
|
|
|
|
|
{
|
|
|
|
|
public enum JobPriority : int
|
|
|
|
|
{
|
|
|
|
|
Low,
|
|
|
|
|
Normal,
|
|
|
|
|
High,
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 04:47:06 +00:00
|
|
|
|
[BsonId] public String Id { get; set; } = Guid.NewGuid().ToString();
|
2020-01-08 04:41:50 +00:00
|
|
|
|
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 JobResult Result { get; set; }
|
|
|
|
|
public bool RequiresNexus { get; set; } = true;
|
|
|
|
|
public AJobPayload Payload { get; set; }
|
|
|
|
|
|
2020-01-10 04:47:06 +00:00
|
|
|
|
public static async Task<String> Enqueue(DBContext db, Job job)
|
2020-01-08 04:41:50 +00:00
|
|
|
|
{
|
|
|
|
|
await db.Jobs.InsertOneAsync(job);
|
|
|
|
|
return job.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<Job> GetNext(DBContext db)
|
|
|
|
|
{
|
|
|
|
|
var filter = new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
{"Started", BsonNull.Value}
|
|
|
|
|
};
|
|
|
|
|
var update = new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
{"$set", new BsonDocument {{"Started", DateTime.Now}}}
|
|
|
|
|
};
|
|
|
|
|
var sort = new {Priority=-1, Created=1}.ToBsonDocument();
|
|
|
|
|
var job = await db.Jobs.FindOneAndUpdateAsync<Job>(filter, update, new FindOneAndUpdateOptions<Job>{Sort = sort});
|
|
|
|
|
return job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<Job> Finish(DBContext db, Job job, JobResult jobResult)
|
|
|
|
|
{
|
|
|
|
|
var filter = new BsonDocument
|
|
|
|
|
{
|
2020-01-10 04:47:06 +00:00
|
|
|
|
{"_id", job.Id},
|
2020-01-08 04:41:50 +00:00
|
|
|
|
};
|
|
|
|
|
var update = new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
{"$set", new BsonDocument {{"Ended", DateTime.Now}, {"Result", jobResult.ToBsonDocument()}}}
|
|
|
|
|
};
|
|
|
|
|
var result = await db.Jobs.FindOneAndUpdateAsync<Job>(filter, update);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|