| | | 1 | | using Hangfire; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using Services.EmailRoutingServices; |
| | | 6 | | |
| | | 7 | | namespace Services.HangfireService; |
| | | 8 | | |
| | | 9 | | public class EmailAttachmentRoutingJob |
| | | 10 | | { |
| | | 11 | | public const string JobId = "EmailAttachmentRoutingJob"; |
| | | 12 | | public const string DefaultCron = "*/5 * * * *"; |
| | | 13 | | public const string QueueName = "main"; |
| | | 14 | | |
| | | 15 | | private readonly IEmailAttachmentRoutingService _routingService; |
| | | 16 | | private readonly IEmailRoutingJobRunService _jobRunService; |
| | | 17 | | private readonly IOptionsMonitor<EmailRoutingExecutionOptions> _options; |
| | | 18 | | private readonly ILogger<EmailAttachmentRoutingJob> _logger; |
| | | 19 | | |
| | 0 | 20 | | public EmailAttachmentRoutingJob( |
| | 0 | 21 | | IEmailAttachmentRoutingService routingService, |
| | 0 | 22 | | IEmailRoutingJobRunService jobRunService, |
| | 0 | 23 | | IOptionsMonitor<EmailRoutingExecutionOptions> options, |
| | 0 | 24 | | ILogger<EmailAttachmentRoutingJob> logger) |
| | | 25 | | { |
| | 0 | 26 | | _routingService = routingService; |
| | 0 | 27 | | _jobRunService = jobRunService; |
| | 0 | 28 | | _options = options; |
| | 0 | 29 | | _logger = logger; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | [DisableConcurrentExecution(600)] |
| | | 33 | | [Queue(QueueName)] |
| | | 34 | | [AutomaticRetry(Attempts = 0)] |
| | | 35 | | public Task<string> ExecuteAsync() => |
| | 0 | 36 | | ExecuteInternalAsync(EmailRoutingJobTriggerType.Scheduled); |
| | | 37 | | |
| | | 38 | | [DisableConcurrentExecution(600)] |
| | | 39 | | [Queue(QueueName)] |
| | | 40 | | [AutomaticRetry(Attempts = 0)] |
| | | 41 | | public Task<string> ExecuteManualAsync() => |
| | 0 | 42 | | ExecuteInternalAsync(EmailRoutingJobTriggerType.Manual); |
| | | 43 | | |
| | | 44 | | private async Task<string> ExecuteInternalAsync(string triggerType) |
| | | 45 | | { |
| | 0 | 46 | | var execution = await _jobRunService.StartAsync(triggerType); |
| | 0 | 47 | | var total = new EmailInboxProcessResult(); |
| | 0 | 48 | | var maxBatches = Math.Max(1, _options.CurrentValue.MaxBatchesPerRun); |
| | | 49 | | |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | for (var batchIndex = 0; batchIndex < maxBatches; batchIndex++) |
| | | 53 | | { |
| | | 54 | | EmailInboxProcessResult batchResult; |
| | | 55 | | try |
| | | 56 | | { |
| | 0 | 57 | | batchResult = await _routingService.ProcessInboxAsync(false); |
| | 0 | 58 | | } |
| | 0 | 59 | | catch (Exception ex) |
| | | 60 | | { |
| | 0 | 61 | | if (ImapEmailInboxClient.IsTransientConnectionException(ex)) |
| | | 62 | | { |
| | 0 | 63 | | _logger.LogInformation( |
| | 0 | 64 | | "EmailAttachmentRoutingJob batch {Batch} пропущен из-за временной недоступности IMAP-сервера |
| | 0 | 65 | | batchIndex + 1, |
| | 0 | 66 | | ex.Message); |
| | | 67 | | } |
| | | 68 | | else |
| | | 69 | | { |
| | 0 | 70 | | _logger.LogWarning(ex, "EmailAttachmentRoutingJob batch {Batch} failed", batchIndex + 1); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | total.SkippedDueToError = true; |
| | 0 | 74 | | total.ErrorMessage = ex.Message; |
| | 0 | 75 | | await _jobRunService.CompleteAsync(execution.Id, total, ex.Message); |
| | 0 | 76 | | return $"Пропуск текущего запуска EmailAttachmentRoutingJob: {ex.Message}"; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | total.Add(batchResult); |
| | 0 | 80 | | if (batchResult.MessagesProcessed == 0) |
| | | 81 | | { |
| | | 82 | | break; |
| | | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | var summary = BuildHangfireSummary(total); |
| | 0 | 87 | | await _jobRunService.CompleteAsync(execution.Id, total); |
| | 0 | 88 | | _logger.LogInformation("EmailAttachmentRoutingJob completed. {Result}", summary); |
| | 0 | 89 | | return summary; |
| | | 90 | | } |
| | 0 | 91 | | catch (Exception ex) |
| | | 92 | | { |
| | 0 | 93 | | total.SkippedDueToError = true; |
| | 0 | 94 | | total.ErrorMessage = ex.Message; |
| | 0 | 95 | | await _jobRunService.CompleteAsync(execution.Id, total, ex.Message); |
| | 0 | 96 | | throw; |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | internal static string BuildHangfireSummary(EmailInboxProcessResult total) |
| | | 101 | | { |
| | 0 | 102 | | var summary = total.ToSummaryString(); |
| | 0 | 103 | | if (total.BatchesProcessed > 1) |
| | | 104 | | { |
| | 0 | 105 | | summary += $" Пакетов IMAP: {total.BatchesProcessed}."; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | if (total.AttachmentsFailed > 0) |
| | | 109 | | { |
| | 0 | 110 | | summary += "<br>Есть ошибки загрузки вложений."; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | return summary; |
| | | 114 | | } |
| | | 115 | | } |