| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.DbContexts; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 5 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 6 | | using Microsoft.EntityFrameworkCore; |
| | 7 | |
|
| | 8 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 9 | |
|
| | 10 | | public interface ITriggerRetryService |
| | 11 | | { |
| | 12 | | Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumber, int pageSi |
| | 13 | | Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId); |
| | 14 | | Task<bool> DeleteTriggerRetryAsync(int triggerRetryId); |
| | 15 | | Task RetryTriggerRetryAsync(int triggerRetryId); |
| | 16 | | Task RetryAllFailedTriggerAsync(int triggerId); |
| | 17 | | } |
| | 18 | |
|
| 5 | 19 | | public class TriggerRetryService ( |
| 5 | 20 | | ITriggerService triggerService, |
| 5 | 21 | | LgdxContext context |
| 5 | 22 | | ) : ITriggerRetryService |
| | 23 | | { |
| 5 | 24 | | private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer |
| 5 | 25 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 26 | |
|
| | 27 | | public async Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumbe |
| 1 | 28 | | { |
| 1 | 29 | | var triggerRetries = await _context.TriggerRetries.AsNoTracking() |
| 1 | 30 | | .Include(tr => tr.Trigger) |
| 1 | 31 | | .Include(tr => tr.AutoTask) |
| 1 | 32 | | .AsSplitQuery() |
| 1 | 33 | | .Select(tr => new TriggerRetryListBusinessModel { |
| 1 | 34 | | Id = tr.Id, |
| 1 | 35 | | TriggerId = tr.TriggerId, |
| 1 | 36 | | TriggerName = tr.Trigger.Name, |
| 1 | 37 | | AutoTaskId = tr.AutoTaskId, |
| 1 | 38 | | AutoTaskName = tr.AutoTask.Name, |
| 1 | 39 | | CreatedAt = tr.CreatedAt |
| 1 | 40 | | }) |
| 1 | 41 | | .OrderBy(t => t.Id) |
| 1 | 42 | | .Skip(pageSize * (pageNumber - 1)) |
| 1 | 43 | | .Take(pageSize) |
| 1 | 44 | | .ToListAsync(); |
| 1 | 45 | | var itemCount = triggerRetries.Count; |
| 1 | 46 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 1 | 47 | | return (triggerRetries, PaginationHelper); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | public async Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId) |
| 2 | 51 | | { |
| 2 | 52 | | TriggerRetry triggerRetry = await _context.TriggerRetries.AsNoTracking() |
| 2 | 53 | | .Where(tr => tr.Id == triggerRetryId) |
| 2 | 54 | | .Include(tr => tr.Trigger) |
| 2 | 55 | | .Include(tr => tr.AutoTask) |
| 2 | 56 | | .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception(); |
| | 57 | |
|
| 1 | 58 | | int SameTriggerFailed = await _context.TriggerRetries.AsNoTracking() |
| 1 | 59 | | .Where(tr => tr.TriggerId == triggerRetry.TriggerId) |
| 1 | 60 | | .CountAsync(); |
| | 61 | |
|
| 1 | 62 | | return new TriggerRetryBusinessModel { |
| 1 | 63 | | Id = triggerRetry.Id, |
| 1 | 64 | | TriggerId = triggerRetry.TriggerId, |
| 1 | 65 | | TriggerName = triggerRetry.Trigger.Name, |
| 1 | 66 | | TriggerUrl = triggerRetry.Trigger.Url, |
| 1 | 67 | | TriggerHttpMethodId = triggerRetry.Trigger.HttpMethodId, |
| 1 | 68 | | AutoTaskId = triggerRetry.AutoTaskId, |
| 1 | 69 | | AutoTaskName = triggerRetry.AutoTask.Name, |
| 1 | 70 | | Body = triggerRetry.Body, |
| 1 | 71 | | SameTriggerFailed = SameTriggerFailed, |
| 1 | 72 | | CreatedAt = triggerRetry.CreatedAt |
| 1 | 73 | | }; |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | public async Task<bool> DeleteTriggerRetryAsync(int triggerRetryId) |
| 0 | 77 | | { |
| 0 | 78 | | return await _context.TriggerRetries.Where(tr => tr.Id == triggerRetryId) |
| 0 | 79 | | .ExecuteDeleteAsync() >= 1; |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | public async Task RetryTriggerRetryAsync(int triggerRetryId) |
| 2 | 83 | | { |
| 2 | 84 | | var triggerRetry = await _context.TriggerRetries.AsNoTracking() |
| 2 | 85 | | .Where(tr => tr.Id == triggerRetryId) |
| 2 | 86 | | .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception(); |
| | 87 | |
|
| 1 | 88 | | var autoTask = await _context.AutoTasks.AsNoTracking() |
| 1 | 89 | | .Where(at => at.Id == triggerRetry.AutoTaskId) |
| 1 | 90 | | .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.AutoTaskId), "AutoTask ID is in |
| | 91 | |
|
| 1 | 92 | | var trigger = await _context.Triggers.AsNoTracking() |
| 1 | 93 | | .Where(t => t.Id == triggerRetry.TriggerId) |
| 1 | 94 | | .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.TriggerId), "Trigger ID is inva |
| | 95 | |
|
| 1 | 96 | | if (await _triggerService.RetryTriggerAsync(autoTask, trigger, triggerRetry.Body)) |
| 0 | 97 | | { |
| 0 | 98 | | await DeleteTriggerRetryAsync(triggerRetryId); |
| 0 | 99 | | } |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | public async Task RetryAllFailedTriggerAsync(int triggerId) |
| 0 | 103 | | { |
| 0 | 104 | | var triggerRetries = await _context.TriggerRetries |
| 0 | 105 | | .Where(tr => tr.TriggerId == triggerId) |
| 0 | 106 | | .Include(tr => tr.Trigger) |
| 0 | 107 | | .Include(tr => tr.AutoTask) |
| 0 | 108 | | .AsSplitQuery() |
| 0 | 109 | | .ToListAsync(); |
| | 110 | |
|
| 0 | 111 | | foreach (var tr in triggerRetries) |
| 0 | 112 | | { |
| 0 | 113 | | if (await _triggerService.RetryTriggerAsync(tr.AutoTask, tr.Trigger, tr.Body)) |
| 0 | 114 | | { |
| 0 | 115 | | _context.TriggerRetries.Remove(tr); |
| 0 | 116 | | } |
| 0 | 117 | | } |
| 0 | 118 | | _context.SaveChanges(); |
| 0 | 119 | | } |
| | 120 | | } |