| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using Newtonsoft.Json.Linq; |
| | | 6 | | |
| | | 7 | | namespace Services.EmailRoutingServices; |
| | | 8 | | |
| | | 9 | | public class EmailRoutingSchedulerSettings |
| | | 10 | | { |
| | 0 | 11 | | public int IntervalMinutes { get; init; } = 5; |
| | 0 | 12 | | public int MaxBatchesPerRun { get; init; } = 20; |
| | 0 | 13 | | public bool AllowMultiMatch { get; init; } |
| | 0 | 14 | | public bool RecurringJobEnabled { get; init; } = true; |
| | 0 | 15 | | public int FetchLimit { get; init; } = 20; |
| | 0 | 16 | | public int FetchScanLimit { get; init; } = ImapFetchWindow.DefaultFetchScanLimit; |
| | 0 | 17 | | public int FetchWindowDays { get; init; } = ImapFetchWindow.DefaultFetchWindowDays; |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public interface IEmailRoutingSchedulerStore |
| | | 21 | | { |
| | | 22 | | EmailRoutingSchedulerSettings GetCurrent(); |
| | | 23 | | Task SaveSettingsAsync(EmailRoutingSchedulerSettings settings, CancellationToken cancellationToken = default); |
| | | 24 | | Task DisableAsync(CancellationToken cancellationToken = default); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public class FileEmailRoutingSchedulerStore : IEmailRoutingSchedulerStore |
| | | 28 | | { |
| | | 29 | | private readonly IHostEnvironment _environment; |
| | | 30 | | private readonly IConfiguration _configuration; |
| | | 31 | | private readonly IOptionsMonitor<EmailRoutingExecutionOptions> _executionOptions; |
| | | 32 | | private readonly IOptionsMonitor<ImapSettings> _imapOptions; |
| | | 33 | | private readonly ILogger<FileEmailRoutingSchedulerStore> _logger; |
| | | 34 | | |
| | | 35 | | public FileEmailRoutingSchedulerStore( |
| | | 36 | | IHostEnvironment environment, |
| | | 37 | | IConfiguration configuration, |
| | | 38 | | IOptionsMonitor<EmailRoutingExecutionOptions> executionOptions, |
| | | 39 | | IOptionsMonitor<ImapSettings> imapOptions, |
| | | 40 | | ILogger<FileEmailRoutingSchedulerStore> logger) |
| | | 41 | | { |
| | | 42 | | _environment = environment; |
| | | 43 | | _configuration = configuration; |
| | | 44 | | _executionOptions = executionOptions; |
| | | 45 | | _imapOptions = imapOptions; |
| | | 46 | | _logger = logger; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public EmailRoutingSchedulerSettings GetCurrent() |
| | | 50 | | { |
| | | 51 | | var execution = _executionOptions.CurrentValue; |
| | | 52 | | var imap = _imapOptions.CurrentValue; |
| | | 53 | | return new EmailRoutingSchedulerSettings |
| | | 54 | | { |
| | | 55 | | IntervalMinutes = execution.IntervalMinutes, |
| | | 56 | | MaxBatchesPerRun = execution.MaxBatchesPerRun, |
| | | 57 | | AllowMultiMatch = execution.AllowMultiMatch, |
| | | 58 | | RecurringJobEnabled = execution.RecurringJobEnabled, |
| | | 59 | | FetchLimit = imap.FetchLimit, |
| | | 60 | | FetchScanLimit = imap.FetchScanLimit, |
| | | 61 | | FetchWindowDays = imap.FetchWindowDays |
| | | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task SaveSettingsAsync(EmailRoutingSchedulerSettings settings, CancellationToken cancellationToken = de |
| | | 66 | | { |
| | | 67 | | var configPath = AppRuntimeSettingsPaths.ResolvePath(_environment.ContentRootPath, _configuration); |
| | | 68 | | await AppRuntimeSettingsWriter.SaveAsync( |
| | | 69 | | _environment.ContentRootPath, |
| | | 70 | | _configuration, |
| | | 71 | | root => |
| | | 72 | | { |
| | | 73 | | var emailRouting = root["EmailRouting"] as JObject ?? new JObject(); |
| | | 74 | | emailRouting["AllowMultiMatch"] = settings.AllowMultiMatch; |
| | | 75 | | emailRouting["RecurringJobEnabled"] = settings.RecurringJobEnabled; |
| | | 76 | | emailRouting["IntervalMinutes"] = settings.IntervalMinutes; |
| | | 77 | | emailRouting["MaxBatchesPerRun"] = settings.MaxBatchesPerRun; |
| | | 78 | | root["EmailRouting"] = emailRouting; |
| | | 79 | | |
| | | 80 | | var imap = root["ImapSettings"] as JObject ?? new JObject(); |
| | | 81 | | imap["FetchLimit"] = settings.FetchLimit; |
| | | 82 | | imap["FetchScanLimit"] = settings.FetchScanLimit; |
| | | 83 | | imap["FetchWindowDays"] = settings.FetchWindowDays; |
| | | 84 | | root["ImapSettings"] = imap; |
| | | 85 | | }, |
| | | 86 | | cancellationToken); |
| | | 87 | | |
| | | 88 | | _logger.LogInformation( |
| | | 89 | | "EmailRouting scheduler settings saved to {ConfigPath}: enabled={Enabled}, interval={IntervalMinutes}m, batc |
| | | 90 | | configPath, |
| | | 91 | | settings.RecurringJobEnabled, |
| | | 92 | | settings.IntervalMinutes, |
| | | 93 | | settings.MaxBatchesPerRun, |
| | | 94 | | settings.FetchLimit); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public async Task DisableAsync(CancellationToken cancellationToken = default) |
| | | 98 | | { |
| | | 99 | | var current = GetCurrent(); |
| | | 100 | | current = new EmailRoutingSchedulerSettings |
| | | 101 | | { |
| | | 102 | | IntervalMinutes = current.IntervalMinutes, |
| | | 103 | | MaxBatchesPerRun = current.MaxBatchesPerRun, |
| | | 104 | | AllowMultiMatch = current.AllowMultiMatch, |
| | | 105 | | RecurringJobEnabled = false, |
| | | 106 | | FetchLimit = current.FetchLimit, |
| | | 107 | | FetchScanLimit = current.FetchScanLimit, |
| | | 108 | | FetchWindowDays = current.FetchWindowDays |
| | | 109 | | }; |
| | | 110 | | await SaveSettingsAsync(current, cancellationToken); |
| | | 111 | | } |
| | | 112 | | } |