2020-05-13 21:52:34 +00:00
|
|
|
|
using System;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using System.Linq;
|
2020-05-13 21:52:34 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Wabbajack.BuildServer;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Services
|
|
|
|
|
{
|
|
|
|
|
public interface IStartable
|
|
|
|
|
{
|
|
|
|
|
public void Start();
|
|
|
|
|
}
|
2020-11-19 22:56:30 +00:00
|
|
|
|
|
|
|
|
|
public interface IReportingService
|
|
|
|
|
{
|
|
|
|
|
public TimeSpan Delay { get; }
|
|
|
|
|
public DateTime LastStart { get; }
|
|
|
|
|
public DateTime LastEnd { get; }
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
public (String, DateTime)[] ActiveWorkStatus { get; }
|
2020-11-19 22:56:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-05-13 21:52:34 +00:00
|
|
|
|
|
2020-11-19 22:56:30 +00:00
|
|
|
|
public abstract class AbstractService<TP, TR> : IStartable, IReportingService
|
2020-05-13 21:52:34 +00:00
|
|
|
|
{
|
|
|
|
|
protected AppSettings _settings;
|
|
|
|
|
private TimeSpan _delay;
|
|
|
|
|
protected ILogger<TP> _logger;
|
2020-06-06 21:44:30 +00:00
|
|
|
|
protected QuickSync _quickSync;
|
2020-05-13 21:52:34 +00:00
|
|
|
|
|
2020-11-19 22:56:30 +00:00
|
|
|
|
public TimeSpan Delay => _delay;
|
|
|
|
|
public DateTime LastStart { get; private set; }
|
|
|
|
|
public DateTime LastEnd { get; private set; }
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public (String, DateTime)[] ActiveWorkStatus { get; private set; }= { };
|
2020-11-19 22:56:30 +00:00
|
|
|
|
|
2020-06-06 21:44:30 +00:00
|
|
|
|
public AbstractService(ILogger<TP> logger, AppSettings settings, QuickSync quickSync, TimeSpan delay)
|
2020-05-13 21:52:34 +00:00
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_delay = delay;
|
|
|
|
|
_logger = logger;
|
2020-06-06 21:44:30 +00:00
|
|
|
|
_quickSync = quickSync;
|
2020-08-05 00:34:09 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual async Task Setup()
|
|
|
|
|
{
|
|
|
|
|
|
2020-05-13 21:52:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2020-08-05 00:34:09 +00:00
|
|
|
|
|
2020-05-13 21:52:34 +00:00
|
|
|
|
if (_settings.RunBackEndJobs)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2020-08-05 00:34:09 +00:00
|
|
|
|
await Setup();
|
2020-11-19 22:56:30 +00:00
|
|
|
|
await _quickSync.Register(this);
|
2020-08-05 00:34:09 +00:00
|
|
|
|
|
2020-05-13 21:52:34 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2020-06-06 21:44:30 +00:00
|
|
|
|
await _quickSync.ResetToken<TP>();
|
2020-05-13 21:52:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-08-05 20:53:19 +00:00
|
|
|
|
_logger.LogInformation($"Running: {GetType().Name}");
|
2020-11-19 22:56:30 +00:00
|
|
|
|
LastStart = DateTime.UtcNow;
|
2020-05-13 21:52:34 +00:00
|
|
|
|
await Execute();
|
2020-11-19 22:56:30 +00:00
|
|
|
|
LastEnd = DateTime.UtcNow;
|
2020-05-13 21:52:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Running Service Loop");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 21:44:30 +00:00
|
|
|
|
var token = await _quickSync.GetToken<TP>();
|
2020-06-08 20:42:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(_delay, token);
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2020-05-13 21:52:34 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract Task<TR> Execute();
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
protected void ReportStarting(string value)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
ActiveWorkStatus = ActiveWorkStatus.Cons((value, DateTime.UtcNow)).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void ReportEnding(string value)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
ActiveWorkStatus = ActiveWorkStatus.Where(x => x.Item1 != value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-13 21:52:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class AbstractServiceExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void UseService<T>(this IApplicationBuilder b)
|
|
|
|
|
{
|
|
|
|
|
var poll = (IStartable)b.ApplicationServices.GetService(typeof(T));
|
|
|
|
|
poll.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-12-31 06:44:42 +00:00
|
|
|
|
|
|
|
|
|
|
2020-05-13 21:52:34 +00:00
|
|
|
|
}
|