< Summary

Information
Class: Services.HangfireService.HangfireJobClientExtensions
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/HangfireService/HangfireJobClientExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 79
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueMain(...)100%210%
EnqueueOzonWebhook(...)100%210%
TryScheduleMainAsync()0%4260%

File(s)

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

#LineLine coverage
 1using System.Linq.Expressions;
 2using Hangfire;
 3using Hangfire.Common;
 4using Hangfire.States;
 5
 6namespace Services.HangfireService;
 7
 8public static class HangfireJobClientExtensions
 9{
 10    public static string EnqueueMain(this IBackgroundJobClient client, Expression<Action> methodCall) =>
 011        client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 12
 13    public static string EnqueueMain(this IBackgroundJobClient client, Expression<Func<Task>> methodCall) =>
 014        client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 15
 16    public static string EnqueueMain<T>(this IBackgroundJobClient client, Expression<Action<T>> methodCall) =>
 017        client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 18
 19    public static string EnqueueMain<T>(this IBackgroundJobClient client, Expression<Func<T, Task>> methodCall) =>
 020        client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 21
 22    public static string EnqueueMain(Expression<Action> methodCall) =>
 023        new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 24
 25    public static string EnqueueMain(Expression<Func<Task>> methodCall) =>
 026        new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 27
 28    public static string EnqueueMain<T>(Expression<Action<T>> methodCall) =>
 029        new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 30
 31    public static string EnqueueMain<T>(Expression<Func<T, Task>> methodCall) =>
 032        new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main));
 33
 34    public static string EnqueueOzonWebhook<T>(Expression<Action<T>> methodCall) =>
 035        new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.OzonWebhoo
 36
 37    /// <summary>
 38    /// Отложенный job в очередь main с повторами при таймауте/блокировке Hangfire MySQL storage.
 39    /// Возвращает Hangfire job id или null, если все попытки исчерпаны.
 40    /// </summary>
 41    public static async Task<string?> TryScheduleMainAsync<T>(
 42        this IBackgroundJobClient client,
 43        Expression<Func<T, Task>> methodCall,
 44        TimeSpan delay,
 45        Func<string, Task>? logAsync = null,
 46        int maxAttempts = 3,
 47        CancellationToken cancellationToken = default)
 48    {
 049        for (var attempt = 1; attempt <= maxAttempts; attempt++)
 50        {
 51            try
 52            {
 053                return client.Schedule(methodCall, delay);
 54            }
 055            catch (Exception ex) when (attempt < maxAttempts)
 56            {
 057                if (logAsync != null)
 58                {
 059                    await logAsync(
 060                        $"Hangfire Schedule: попытка {attempt}/{maxAttempts} не удалась ({ex.GetType().Name}: {ex.Messag
 61                }
 62
 063                await Task.Delay(TimeSpan.FromSeconds(5 * attempt), cancellationToken);
 064            }
 65            catch (Exception ex)
 66            {
 067                if (logAsync != null)
 68                {
 069                    await logAsync(
 070                        $"Hangfire Schedule: не удалось запланировать job после {maxAttempts} попыток ({ex.GetType().Nam
 71                }
 72
 073                return null;
 74            }
 75        }
 76
 077        return null;
 078    }
 79}