From 2fd99694b687d29fdc4b12d4e4d545f8f46dad44 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 13 Jul 2026 11:59:44 +0200 Subject: [PATCH] refactor: Remove monolithic ProfileManager Replaced by modular ProfileWorker namespace with better separation of concerns. --- ECMJobRunner.WebCron/ProfileManager.cs | 71 -------------------------- 1 file changed, 71 deletions(-) delete mode 100644 ECMJobRunner.WebCron/ProfileManager.cs diff --git a/ECMJobRunner.WebCron/ProfileManager.cs b/ECMJobRunner.WebCron/ProfileManager.cs deleted file mode 100644 index 2524de5..0000000 --- a/ECMJobRunner.WebCron/ProfileManager.cs +++ /dev/null @@ -1,71 +0,0 @@ -using ECMJobRunner.Application.Common.Dtos; -using ECMJobRunner.Application.DEXJob.Commands; -using ECMJobRunner.Application.DEXJob.Queries; -using ECMJobRunner.WebCron.Extensions; -using Hangfire; -using MediatR; -using System.Collections.Concurrent; - -namespace ECMJobRunner.WebCron -{ - public class ProfileManager(ILogger Logger, IServiceScopeFactory ScopeFactory, IRecurringJobManager JobManager) : BackgroundService - { - private readonly ConcurrentDictionary Profiles = new(); - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - try - { - - while (!stoppingToken.IsCancellationRequested) - { - if (Logger.IsEnabled(LogLevel.Information)) - { - Logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); - } - - // Create a scope to resolve scoped services (ISQLExecutor used by MediatR pipeline) - using var scope = ScopeFactory.CreateScope(); - var mediator = scope.ServiceProvider.GetRequiredService(); - - var profiles = await mediator.Send(new GetProfileQuery() - { - Active = true, - IncludeSqlJobs = true - }, stoppingToken); - - foreach (var profile in profiles) - { - if (Profiles.TryGetValue(profile.JobId(), out var currentProfile) - && currentProfile.Schedule == profile.Schedule) - continue; - - // Add or update recurring job using MediatR command - JobManager.AddOrUpdate( - profile.JobId(), - mediator => mediator.Send(profile.ToJob(), CancellationToken.None), - profile.Schedule, - new RecurringJobOptions - { - TimeZone = TimeZoneInfo.Local - } - ); - - // Store/update in local cache - Profiles[profile.JobId()] = profile; - - Logger.LogInformation("Job {JobId} registered with schedule: {Schedule}", - profile.JobId(), profile.Schedule); - } - - await Task.Delay(1000, stoppingToken); - } - - } - catch (Exception ex) - { - Logger.LogError(ex, "An error occurred in ProfileManager."); - } - } - } -}