2020-06-06 21:44:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-19 22:56:30 +00:00
|
|
|
|
using System.Linq;
|
2020-06-06 21:44:30 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-08-12 04:25:12 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-06-06 21:44:30 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Services
|
|
|
|
|
{
|
|
|
|
|
public class QuickSync
|
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
private Dictionary<Type, CancellationTokenSource> _syncs = new();
|
|
|
|
|
private Dictionary<Type, IReportingService> _services = new();
|
|
|
|
|
private AsyncLock _lock = new();
|
2020-08-12 04:25:12 +00:00
|
|
|
|
private ILogger<QuickSync> _logger;
|
|
|
|
|
|
|
|
|
|
public QuickSync(ILogger<QuickSync> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
2020-06-06 21:44:30 +00:00
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public async Task<Dictionary<Type, (TimeSpan Delay, TimeSpan LastRunTime, (String, DateTime)[] ActiveWork)>> Report()
|
2020-11-19 22:56:30 +00:00
|
|
|
|
{
|
|
|
|
|
using var _ = await _lock.WaitAsync();
|
|
|
|
|
return _services.ToDictionary(s => s.Key,
|
2020-12-31 06:44:42 +00:00
|
|
|
|
s => (s.Value.Delay, DateTime.UtcNow - s.Value.LastEnd, s.Value.ActiveWorkStatus));
|
2020-11-19 22:56:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Register<T>(T service)
|
|
|
|
|
where T : IReportingService
|
|
|
|
|
{
|
|
|
|
|
using var _ = await _lock.WaitAsync();
|
2020-11-20 05:00:07 +00:00
|
|
|
|
_services[service.GetType()] = service;
|
2020-11-19 22:56:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 21:44:30 +00:00
|
|
|
|
public async Task<CancellationToken> GetToken<T>()
|
|
|
|
|
{
|
|
|
|
|
using var _ = await _lock.WaitAsync();
|
|
|
|
|
if (_syncs.TryGetValue(typeof(T), out var result))
|
|
|
|
|
{
|
|
|
|
|
return result.Token;
|
|
|
|
|
}
|
|
|
|
|
var token = new CancellationTokenSource();
|
|
|
|
|
_syncs[typeof(T)] = token;
|
|
|
|
|
return token.Token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task ResetToken<T>()
|
|
|
|
|
{
|
|
|
|
|
using var _ = await _lock.WaitAsync();
|
|
|
|
|
if (_syncs.TryGetValue(typeof(T), out var ct))
|
|
|
|
|
{
|
|
|
|
|
ct.Cancel();
|
|
|
|
|
}
|
|
|
|
|
_syncs[typeof(T)] = new CancellationTokenSource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Notify<T>()
|
|
|
|
|
{
|
2020-08-12 04:25:12 +00:00
|
|
|
|
_logger.LogInformation($"Quicksync {typeof(T).Name}");
|
2020-08-08 18:26:55 +00:00
|
|
|
|
// Needs debugging
|
2020-06-06 21:44:30 +00:00
|
|
|
|
using var _ = await _lock.WaitAsync();
|
|
|
|
|
if (_syncs.TryGetValue(typeof(T), out var ct))
|
|
|
|
|
{
|
|
|
|
|
ct.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-04 03:48:30 +00:00
|
|
|
|
|
|
|
|
|
public async Task Notify(Type t)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Quicksync {t.Name}");
|
|
|
|
|
// Needs debugging
|
|
|
|
|
using var _ = await _lock.WaitAsync();
|
|
|
|
|
if (_syncs.TryGetValue(t, out var ct))
|
|
|
|
|
{
|
|
|
|
|
ct.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-06 21:44:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|