using ConnectionsAPI.Database; using ConnectionsAPI.Utility; using System.Diagnostics; namespace ConnectionsAPI.Events; public class ConnectionsSyncEvent : IEvent { } public class ConnectionsSyncHandler(ILogger logger, IServiceScopeFactory scopeFactory) : IEventHandler { private readonly ILogger _logger = logger; private readonly IServiceScopeFactory _scopeFactory = scopeFactory; public async Task HandleAsync(ConnectionsSyncEvent eventModel, CancellationToken ct) { Stopwatch stopwatch = Stopwatch.StartNew(); _logger.LogInformation("Received Connections Sync Event"); try { // construct scope using var scope = _scopeFactory.CreateScope(); // get dependencies ConnectionsContext db = scope.ServiceProvider.GetRequiredService(); HttpClient http = scope.ServiceProvider.GetRequiredService().CreateClient(); ILogger syncLogger = scope.ServiceProvider.GetRequiredService>(); // do the work await new SyncUtility(db, syncLogger, http).SyncPuzzlesAsync(ct); } catch (Exception ex) { _logger.LogError(ex, "Error while executing Connections sync event"); } finally { stopwatch.Stop(); } _logger.LogInformation("Connections Sync Event finished in {ts}", stopwatch.Elapsed); } }