< Summary

Information
Class: Services.HangfireService.EmailAttachmentRoutingJob
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/HangfireService/EmailAttachmentRoutingJob.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 115
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ExecuteAsync()100%210%
ExecuteManualAsync()100%210%
ExecuteInternalAsync()0%4260%
BuildHangfireSummary(...)0%2040%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/HangfireService/EmailAttachmentRoutingJob.cs

#LineLine coverage
 1using Hangfire;
 2using Microsoft.Extensions.Logging;
 3using Microsoft.Extensions.Options;
 4using OzonDomains.Models;
 5using Services.EmailRoutingServices;
 6
 7namespace Services.HangfireService;
 8
 9public 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
 020    public EmailAttachmentRoutingJob(
 021        IEmailAttachmentRoutingService routingService,
 022        IEmailRoutingJobRunService jobRunService,
 023        IOptionsMonitor<EmailRoutingExecutionOptions> options,
 024        ILogger<EmailAttachmentRoutingJob> logger)
 25    {
 026        _routingService = routingService;
 027        _jobRunService = jobRunService;
 028        _options = options;
 029        _logger = logger;
 030    }
 31
 32    [DisableConcurrentExecution(600)]
 33    [Queue(QueueName)]
 34    [AutomaticRetry(Attempts = 0)]
 35    public Task<string> ExecuteAsync() =>
 036        ExecuteInternalAsync(EmailRoutingJobTriggerType.Scheduled);
 37
 38    [DisableConcurrentExecution(600)]
 39    [Queue(QueueName)]
 40    [AutomaticRetry(Attempts = 0)]
 41    public Task<string> ExecuteManualAsync() =>
 042        ExecuteInternalAsync(EmailRoutingJobTriggerType.Manual);
 43
 44    private async Task<string> ExecuteInternalAsync(string triggerType)
 45    {
 046        var execution = await _jobRunService.StartAsync(triggerType);
 047        var total = new EmailInboxProcessResult();
 048        var maxBatches = Math.Max(1, _options.CurrentValue.MaxBatchesPerRun);
 49
 50        try
 51        {
 052            for (var batchIndex = 0; batchIndex < maxBatches; batchIndex++)
 53            {
 54                EmailInboxProcessResult batchResult;
 55                try
 56                {
 057                    batchResult = await _routingService.ProcessInboxAsync(false);
 058                }
 059                catch (Exception ex)
 60                {
 061                    if (ImapEmailInboxClient.IsTransientConnectionException(ex))
 62                    {
 063                        _logger.LogInformation(
 064                            "EmailAttachmentRoutingJob batch {Batch} пропущен из-за временной недоступности IMAP-сервера
 065                            batchIndex + 1,
 066                            ex.Message);
 67                    }
 68                    else
 69                    {
 070                        _logger.LogWarning(ex, "EmailAttachmentRoutingJob batch {Batch} failed", batchIndex + 1);
 71                    }
 72
 073                    total.SkippedDueToError = true;
 074                    total.ErrorMessage = ex.Message;
 075                    await _jobRunService.CompleteAsync(execution.Id, total, ex.Message);
 076                    return $"Пропуск текущего запуска EmailAttachmentRoutingJob: {ex.Message}";
 77                }
 78
 079                total.Add(batchResult);
 080                if (batchResult.MessagesProcessed == 0)
 81                {
 82                    break;
 83                }
 084            }
 85
 086            var summary = BuildHangfireSummary(total);
 087            await _jobRunService.CompleteAsync(execution.Id, total);
 088            _logger.LogInformation("EmailAttachmentRoutingJob completed. {Result}", summary);
 089            return summary;
 90        }
 091        catch (Exception ex)
 92        {
 093            total.SkippedDueToError = true;
 094            total.ErrorMessage = ex.Message;
 095            await _jobRunService.CompleteAsync(execution.Id, total, ex.Message);
 096            throw;
 97        }
 098    }
 99
 100    internal static string BuildHangfireSummary(EmailInboxProcessResult total)
 101    {
 0102        var summary = total.ToSummaryString();
 0103        if (total.BatchesProcessed > 1)
 104        {
 0105            summary += $" Пакетов IMAP: {total.BatchesProcessed}.";
 106        }
 107
 0108        if (total.AttachmentsFailed > 0)
 109        {
 0110            summary += "<br>Есть ошибки загрузки вложений.";
 111        }
 112
 0113        return summary;
 114    }
 115}