wabbajack/Wabbajack.Server/Services/AbstractService.cs

106 lines
2.6 KiB
C#
Raw Normal View History

2020-05-13 21:52:34 +00:00
using System;
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;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Server.Services;
public interface IStartable
2020-05-13 21:52:34 +00:00
{
2021-10-23 16:51:17 +00:00
public Task Start();
}
2021-10-23 16:51:17 +00:00
public interface IReportingService
{
public TimeSpan Delay { get; }
public DateTime LastStart { get; }
public DateTime LastEnd { get; }
2021-10-23 16:51:17 +00:00
public (string, DateTime)[] ActiveWorkStatus { get; }
}
2020-05-13 21:52:34 +00:00
2021-10-23 16:51:17 +00:00
public abstract class AbstractService<TP, TR> : IStartable, IReportingService
{
protected ILogger<TP> _logger;
protected QuickSync _quickSync;
protected AppSettings _settings;
2021-10-23 16:51:17 +00:00
public AbstractService(ILogger<TP> logger, AppSettings settings, QuickSync quickSync, TimeSpan delay)
{
_settings = settings;
Delay = delay;
_logger = logger;
_quickSync = quickSync;
}
2021-10-23 16:51:17 +00:00
public TimeSpan Delay { get; }
2021-10-23 16:51:17 +00:00
public DateTime LastStart { get; private set; }
public DateTime LastEnd { get; private set; }
public (string, DateTime)[] ActiveWorkStatus { get; private set; } = { };
public async Task Start()
{
await Setup();
await _quickSync.Register(this);
2020-05-13 21:52:34 +00:00
2021-10-23 16:51:17 +00:00
while (true)
2020-05-13 21:52:34 +00:00
{
2021-10-23 16:51:17 +00:00
await _quickSync.ResetToken<TP>();
try
2020-05-13 21:52:34 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogInformation($"Running: {GetType().Name}");
ActiveWorkStatus = Array.Empty<(string, DateTime)>();
LastStart = DateTime.UtcNow;
await Execute();
LastEnd = DateTime.UtcNow;
}
catch (Exception ex)
{
_logger.LogError(ex, "Running Service Loop");
2020-05-13 21:52:34 +00:00
}
2021-10-23 16:51:17 +00:00
var token = await _quickSync.GetToken<TP>();
try
{
await Task.Delay(Delay, token);
}
catch (TaskCanceledException)
{
}
}
2021-10-23 16:51:17 +00:00
}
public virtual async Task Setup()
{
}
2021-10-23 16:51:17 +00:00
public abstract Task<TR> Execute();
protected void ReportStarting(string value)
{
lock (this)
{
2021-10-23 16:51:17 +00:00
ActiveWorkStatus = ActiveWorkStatus.Append((value, DateTime.UtcNow)).ToArray();
}
2020-05-13 21:52:34 +00:00
}
2021-10-23 16:51:17 +00:00
protected void ReportEnding(string value)
2020-05-13 21:52:34 +00:00
{
2021-10-23 16:51:17 +00:00
lock (this)
2020-05-13 21:52:34 +00:00
{
2021-10-23 16:51:17 +00:00
ActiveWorkStatus = ActiveWorkStatus.Where(x => x.Item1 != value).ToArray();
2020-05-13 21:52:34 +00:00
}
}
}
2021-10-23 16:51:17 +00:00
public static class AbstractServiceExtensions
{
public static void UseService<T>(this IApplicationBuilder b)
{
var poll = (IStartable) b.ApplicationServices.GetService(typeof(T));
poll.Start();
}
}