< Summary

Information
Class: Services.OrderControlServices.OrderControlEvaluationJob
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/OrderControlServices/OrderControlEvaluationJob.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 10
Coverable lines: 10
Total lines: 33
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
GetCron(...)100%210%
.ctor(...)100%210%
ExecuteAsync()0%110100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/OrderControlServices/OrderControlEvaluationJob.cs

#LineLine coverage
 1using Hangfire;
 2using Microsoft.EntityFrameworkCore;
 3using OzonDomains.Models;
 4using OzonRepositories.Context;
 5using Services.HangfireService;
 6
 7namespace Services.OrderControlServices;
 8
 9public sealed class OrderControlEvaluationJob
 10{
 11    public const string JobId = "order-control-evaluation";
 12    public const string DefaultCron = "*/5 * * * *";
 013    public static string GetCron(int intervalMinutes) => $"*/{Math.Clamp(intervalMinutes, 5, 59)} * * * *";
 14    private readonly IOrderControlService _service;
 15    private readonly ApplicationDbContext _db;
 16
 017    public OrderControlEvaluationJob(IOrderControlService service, ApplicationDbContext db) { _service = service; _db = 
 18
 19    // Hangfire-хранилище общее на двух прод-инстансах — без этого атрибута ручной RunNow
 20    // и очередной cron-тик могут выполниться одновременно на разных серверах.
 21    [Queue(HangfireQueueNames.OrderStatusSync)]
 22    [DisableConcurrentExecution(timeoutInSeconds: 0)]
 23    public async Task<int> ExecuteAsync()
 24    {
 025        var settings = await _db.OrderControlSettings.FindAsync(1) ?? new OrderControlSettings { Id = 1 };
 026        if (_db.Entry(settings).State == EntityState.Detached) { _db.OrderControlSettings.Add(settings); await _db.SaveC
 027        var last = await _db.OrderControlExecutions.Where(x => x.Status == "Success").OrderByDescending(x => x.Completed
 028        if (last?.CompletedAtUtc >= DateTime.UtcNow.AddMinutes(-Math.Clamp(settings.IntervalMinutes, 5, 1440))) return 0
 029        var execution = new OrderControlExecution(); _db.OrderControlExecutions.Add(execution); await _db.SaveChangesAsy
 030        try { var count = await _service.EvaluateAsync(); execution.CreatedTasksCount = count; execution.Status = "Succe
 031        catch (Exception ex) { execution.Status = "Failed"; execution.ErrorMessage = ex.Message[..Math.Min(1000, ex.Mess
 032    }
 33}