| | | 1 | | using Hangfire; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | using Services.HangfireService; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.WebServices.SystemAlerts; |
| | | 8 | | |
| | | 9 | | public interface ISystemMonitoringSignalService |
| | | 10 | | { |
| | | 11 | | Task RecordAsync(string sourceKey, string outcome, string? scopeKey = null, string? message = null, CancellationToke |
| | | 12 | | } |
| | | 13 | | |
| | 0 | 14 | | public sealed class SystemMonitoringSignalService(ApplicationDbContext context) : ISystemMonitoringSignalService |
| | | 15 | | { |
| | | 16 | | public async Task RecordAsync(string sourceKey, string outcome, string? scopeKey = null, string? message = null, Can |
| | | 17 | | { |
| | 0 | 18 | | context.SystemMonitoringSignals.Add(new SystemMonitoringSignal |
| | 0 | 19 | | { |
| | 0 | 20 | | SourceKey = sourceKey[..Math.Min(100, sourceKey.Length)], Outcome = outcome[..Math.Min(16, outcome.Length)], |
| | 0 | 21 | | ScopeKey = scopeKey?[..Math.Min(120, scopeKey.Length)], Message = message?[..Math.Min(500, message.Length)] |
| | 0 | 22 | | }); |
| | 0 | 23 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 24 | | } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public sealed class SystemMonitoringSignalCleanupJob(ApplicationDbContext context) |
| | | 28 | | { |
| | | 29 | | public const string JobId = "system-monitoring-signal-cleanup"; |
| | | 30 | | public const string DefaultCron = "30 3 * * *"; |
| | | 31 | | [Queue(HangfireQueueNames.Main)] |
| | | 32 | | public async Task ExecuteAsync() |
| | | 33 | | { |
| | | 34 | | var threshold = DateTime.UtcNow.AddDays(-RetentionDays); |
| | | 35 | | await context.SystemMonitoringSignals.Where(x => x.OccurredAtUtc < threshold).ExecuteDeleteAsync(); |
| | | 36 | | // Закрытые инциденты тоже не копим бесконечно; активные не трогаем. |
| | | 37 | | await context.SystemAlertIncidents.Where(x => !x.IsActive && x.LastObservedAtUtc < threshold).ExecuteDeleteAsync |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private const int RetentionDays = 90; |
| | | 41 | | } |