2020-01-09 04:42:25 +00:00
|
|
|
|
using System;
|
2020-01-09 12:14:48 +00:00
|
|
|
|
using System.Linq;
|
2020-01-09 04:42:25 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using MongoDB.Driver.Linq;
|
|
|
|
|
using Wabbajack.BuildServer.Models;
|
|
|
|
|
using Wabbajack.BuildServer.Models.JobQueue;
|
|
|
|
|
using Wabbajack.BuildServer.Models.Jobs;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.BuildServer
|
|
|
|
|
{
|
|
|
|
|
public class JobManager
|
|
|
|
|
{
|
|
|
|
|
protected readonly ILogger<JobManager> Logger;
|
|
|
|
|
protected readonly DBContext Db;
|
|
|
|
|
protected readonly AppSettings Settings;
|
|
|
|
|
|
|
|
|
|
public JobManager(ILogger<JobManager> logger, DBContext db, AppSettings settings)
|
|
|
|
|
{
|
|
|
|
|
Db = db;
|
|
|
|
|
Logger = logger;
|
|
|
|
|
Settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartJobRunners()
|
|
|
|
|
{
|
|
|
|
|
for (var idx = 0; idx < 2; idx++)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var job = await Job.GetNext(Db);
|
|
|
|
|
if (job == null)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Log(LogLevel.Information, $"Starting Job: {job.Payload.Description}");
|
|
|
|
|
JobResult result;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = await job.Payload.Execute(Db, Settings);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Log(LogLevel.Error, ex, $"Error while running job: {job.Payload.Description}");
|
|
|
|
|
result = JobResult.Error(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Job.Finish(Db, job, result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Log(LogLevel.Error, ex, $"Error getting or updating Job");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task JobScheduler()
|
|
|
|
|
{
|
|
|
|
|
Utils.LogMessages.Subscribe(msg => Logger.Log(LogLevel.Information, msg.ToString()));
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
await KillOrphanedJobs();
|
2020-01-09 12:14:48 +00:00
|
|
|
|
await ScheduledJob<GetNexusUpdatesJob>(TimeSpan.FromHours(2));
|
|
|
|
|
await ScheduledJob<UpdateModLists>(TimeSpan.FromMinutes(30));
|
2020-01-09 04:42:25 +00:00
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task KillOrphanedJobs()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var started = await Db.Jobs.AsQueryable()
|
|
|
|
|
.Where(j => j.Started != null && j.Ended == null)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
foreach (var job in started)
|
|
|
|
|
{
|
|
|
|
|
var runtime = DateTime.Now - job.Started;
|
|
|
|
|
if (runtime > TimeSpan.FromMinutes(30))
|
|
|
|
|
{
|
|
|
|
|
await Job.Finish(Db, job, JobResult.Error(new Exception($"Timeout after {runtime.Value.TotalMinutes}")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-01-09 12:14:48 +00:00
|
|
|
|
Logger.Log(LogLevel.Error, ex, "Error in JobScheduler when scheduling KillOrphanedJobs");
|
2020-01-09 04:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 12:14:48 +00:00
|
|
|
|
|
|
|
|
|
private async Task ScheduledJob<T>(TimeSpan span) where T : AJobPayload, new()
|
2020-01-09 04:42:25 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-01-09 12:14:48 +00:00
|
|
|
|
var jobs = await Db.Jobs.AsQueryable()
|
|
|
|
|
.Where(j => j.Payload is T)
|
|
|
|
|
.OrderByDescending(j => j.Created)
|
|
|
|
|
.Take(10)
|
2020-01-09 04:42:25 +00:00
|
|
|
|
.ToListAsync();
|
2020-01-09 12:14:48 +00:00
|
|
|
|
|
|
|
|
|
foreach (var job in jobs)
|
2020-01-09 04:42:25 +00:00
|
|
|
|
{
|
2020-01-09 12:14:48 +00:00
|
|
|
|
if (job.Started == null || job.Ended == null) return;
|
|
|
|
|
if (DateTime.Now - job.Ended < span) return;
|
2020-01-09 04:42:25 +00:00
|
|
|
|
}
|
2020-01-09 12:14:48 +00:00
|
|
|
|
await Db.Jobs.InsertOneAsync(new Job
|
|
|
|
|
{
|
|
|
|
|
Payload = new T()
|
|
|
|
|
});
|
2020-01-09 04:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-01-09 12:14:48 +00:00
|
|
|
|
|
|
|
|
|
Logger.Log(LogLevel.Error, ex, $"Error in JobScheduler when scheduling {typeof(T).Name}");
|
2020-01-09 04:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 12:14:48 +00:00
|
|
|
|
|
2020-01-09 04:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|