using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Wabbajack.Common; namespace Wabbajack.Server.Services; public class QuickSync { private readonly AsyncLock _lock = new(); private readonly ILogger _logger; private readonly Dictionary _services = new(); private readonly Dictionary _syncs = new(); public QuickSync(ILogger logger) { _logger = logger; } public async Task> Report() { using var _ = await _lock.WaitAsync(); return _services.ToDictionary(s => s.Key, s => (s.Value.Delay, DateTime.UtcNow - s.Value.LastEnd, s.Value.ActiveWorkStatus)); } public async Task Register(T service) where T : IReportingService { using var _ = await _lock.WaitAsync(); _services[service.GetType()] = service; } public async Task GetToken() { 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() { using var _ = await _lock.WaitAsync(); if (_syncs.TryGetValue(typeof(T), out var ct)) ct.Cancel(); _syncs[typeof(T)] = new CancellationTokenSource(); } public async Task Notify() { _logger.LogInformation($"Quicksync {typeof(T).Name}"); // Needs debugging using var _ = await _lock.WaitAsync(); if (_syncs.TryGetValue(typeof(T), out var ct)) ct.Cancel(); } 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(); } }