| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Hangfire; |
| | | 3 | | using Hangfire.Common; |
| | | 4 | | using Hangfire.States; |
| | | 5 | | |
| | | 6 | | namespace Services.HangfireService; |
| | | 7 | | |
| | | 8 | | public static class HangfireJobClientExtensions |
| | | 9 | | { |
| | | 10 | | public static string EnqueueMain(this IBackgroundJobClient client, Expression<Action> methodCall) => |
| | 0 | 11 | | client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 12 | | |
| | | 13 | | public static string EnqueueMain(this IBackgroundJobClient client, Expression<Func<Task>> methodCall) => |
| | 0 | 14 | | client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 15 | | |
| | | 16 | | public static string EnqueueMain<T>(this IBackgroundJobClient client, Expression<Action<T>> methodCall) => |
| | 0 | 17 | | client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 18 | | |
| | | 19 | | public static string EnqueueMain<T>(this IBackgroundJobClient client, Expression<Func<T, Task>> methodCall) => |
| | 0 | 20 | | client.Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 21 | | |
| | | 22 | | public static string EnqueueMain(Expression<Action> methodCall) => |
| | 0 | 23 | | new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 24 | | |
| | | 25 | | public static string EnqueueMain(Expression<Func<Task>> methodCall) => |
| | 0 | 26 | | new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 27 | | |
| | | 28 | | public static string EnqueueMain<T>(Expression<Action<T>> methodCall) => |
| | 0 | 29 | | new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 30 | | |
| | | 31 | | public static string EnqueueMain<T>(Expression<Func<T, Task>> methodCall) => |
| | 0 | 32 | | new BackgroundJobClient().Create(Job.FromExpression(methodCall), new EnqueuedState(HangfireQueueNames.Main)); |
| | | 33 | | |
| | | 34 | | public static string EnqueueOzonWebhook<T>(Expression<Action<T>> methodCall) => |
| | 0 | 35 | | 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 | | { |
| | 0 | 49 | | for (var attempt = 1; attempt <= maxAttempts; attempt++) |
| | | 50 | | { |
| | | 51 | | try |
| | | 52 | | { |
| | 0 | 53 | | return client.Schedule(methodCall, delay); |
| | | 54 | | } |
| | 0 | 55 | | catch (Exception ex) when (attempt < maxAttempts) |
| | | 56 | | { |
| | 0 | 57 | | if (logAsync != null) |
| | | 58 | | { |
| | 0 | 59 | | await logAsync( |
| | 0 | 60 | | $"Hangfire Schedule: попытка {attempt}/{maxAttempts} не удалась ({ex.GetType().Name}: {ex.Messag |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | await Task.Delay(TimeSpan.FromSeconds(5 * attempt), cancellationToken); |
| | 0 | 64 | | } |
| | | 65 | | catch (Exception ex) |
| | | 66 | | { |
| | 0 | 67 | | if (logAsync != null) |
| | | 68 | | { |
| | 0 | 69 | | await logAsync( |
| | 0 | 70 | | $"Hangfire Schedule: не удалось запланировать job после {maxAttempts} попыток ({ex.GetType().Nam |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return null; |
| | 0 | 78 | | } |
| | | 79 | | } |