| | | 1 | | using System.Diagnostics; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | |
| | | 7 | | namespace Services.BackgroundJobsServices; |
| | | 8 | | |
| | | 9 | | public interface IBackgroundJobLoggingService |
| | | 10 | | { |
| | | 11 | | Task<long> StartExecutionAsync(int? scheduleId, string jobType, string triggerType, CancellationToken cancellationTo |
| | | 12 | | Task CompleteExecutionAsync(long executionId, CancellationToken cancellationToken = default); |
| | | 13 | | Task FailExecutionAsync(long executionId, string errorMessage, string? stackTrace = null, CancellationToken cancella |
| | | 14 | | Task UpdateExecutionStatsAsync(long executionId, int totalOrders = 0, int matchedOrders = 0, int processedOrders = 0 |
| | | 15 | | |
| | | 16 | | Task<long> StartStepAsync(long executionId, int stepNumber, string stepName, object? inputData = null, CancellationT |
| | | 17 | | Task CompleteStepAsync(long stepId, object? outputData = null, CancellationToken cancellationToken = default); |
| | | 18 | | Task FailStepAsync(long stepId, string errorMessage, CancellationToken cancellationToken = default); |
| | | 19 | | |
| | | 20 | | Task<long> LogReportLoadAsync(long executionId, long? stepId, int ozonClientId, DateTime periodFrom, DateTime period |
| | | 21 | | Task UpdateReportLoadAsync(long reportLoadId, string? fileName = null, string? fileUrl = null, long? fileSizeBytes = |
| | | 22 | | |
| | | 23 | | Task<long> LogOrderMatchAsync(long executionId, long? stepId, long? reportLoadId, string orderNumber, string article |
| | | 24 | | Task BulkLogOrderMatchesAsync(IEnumerable<BackgroundOrderMatch> matches, CancellationToken cancellationToken = defau |
| | | 25 | | |
| | | 26 | | Task<long> LogTransactionResultAsync(long executionId, long? stepId, long? orderMatchId, int orderId, string? wareho |
| | | 27 | | Task UpdateTransactionResultAsync(long resultId, int? transactionId = null, string? oneCDocumentId = null, string? o |
| | | 28 | | Task BulkUpdateTransactionResultsAsync(IEnumerable<(long resultId, int? transactionId, string? oneCDocumentId, strin |
| | | 29 | | |
| | | 30 | | Task<BackgroundJobExecution?> GetExecutionAsync(long executionId, CancellationToken cancellationToken = default); |
| | | 31 | | Task<List<BackgroundJobStep>> GetExecutionStepsAsync(long executionId, CancellationToken cancellationToken = default |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public class BackgroundJobLoggingService : IBackgroundJobLoggingService |
| | | 35 | | { |
| | | 36 | | private readonly IDbContextFactory<ApplicationDbContext> _contextFactory; |
| | | 37 | | |
| | 0 | 38 | | public BackgroundJobLoggingService(IDbContextFactory<ApplicationDbContext> contextFactory) |
| | | 39 | | { |
| | 0 | 40 | | _contextFactory = contextFactory; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task<long> StartExecutionAsync(int? scheduleId, string jobType, string triggerType, CancellationToken c |
| | | 44 | | { |
| | 0 | 45 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 46 | | |
| | 0 | 47 | | var execution = new BackgroundJobExecution |
| | 0 | 48 | | { |
| | 0 | 49 | | ScheduleId = scheduleId, |
| | 0 | 50 | | JobType = jobType, |
| | 0 | 51 | | Status = "Running", |
| | 0 | 52 | | TriggerType = triggerType, |
| | 0 | 53 | | StartedAt = DateTime.UtcNow |
| | 0 | 54 | | }; |
| | | 55 | | |
| | 0 | 56 | | context.BackgroundJobExecutions.Add(execution); |
| | 0 | 57 | | await context.SaveChangesAsync(cancellationToken); |
| | | 58 | | |
| | 0 | 59 | | return execution.Id; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task CompleteExecutionAsync(long executionId, CancellationToken cancellationToken = default) |
| | | 63 | | { |
| | 0 | 64 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 65 | | |
| | 0 | 66 | | var execution = await context.BackgroundJobExecutions.FindAsync(new object[] { executionId }, cancellationToken) |
| | 0 | 67 | | if (execution == null) return; |
| | | 68 | | |
| | 0 | 69 | | execution.CompletedAt = DateTime.UtcNow; |
| | 0 | 70 | | execution.DurationMs = (long)(execution.CompletedAt.Value - execution.StartedAt).TotalMilliseconds; |
| | | 71 | | |
| | 0 | 72 | | if (execution.FailedOrders == 0) |
| | 0 | 73 | | execution.Status = "Completed"; |
| | 0 | 74 | | else if (execution.SuccessfulOrders > 0) |
| | 0 | 75 | | execution.Status = "PartialSuccess"; |
| | | 76 | | else |
| | 0 | 77 | | execution.Status = "Failed"; |
| | | 78 | | |
| | 0 | 79 | | execution.CompletedSteps = await context.BackgroundJobSteps |
| | 0 | 80 | | .Where(s => s.ExecutionId == executionId && s.Status == "Completed") |
| | 0 | 81 | | .CountAsync(cancellationToken); |
| | | 82 | | |
| | 0 | 83 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public async Task FailExecutionAsync(long executionId, string errorMessage, string? stackTrace = null, CancellationT |
| | | 87 | | { |
| | 0 | 88 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 89 | | |
| | 0 | 90 | | var execution = await context.BackgroundJobExecutions.FindAsync(new object[] { executionId }, cancellationToken) |
| | 0 | 91 | | if (execution == null) return; |
| | | 92 | | |
| | 0 | 93 | | execution.CompletedAt = DateTime.UtcNow; |
| | 0 | 94 | | execution.DurationMs = (long)(execution.CompletedAt.Value - execution.StartedAt).TotalMilliseconds; |
| | 0 | 95 | | execution.Status = "Failed"; |
| | 0 | 96 | | execution.ErrorMessage = errorMessage; |
| | 0 | 97 | | execution.ErrorStackTrace = stackTrace; |
| | | 98 | | |
| | 0 | 99 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public async Task UpdateExecutionStatsAsync(long executionId, int totalOrders = 0, int matchedOrders = 0, int proces |
| | | 103 | | { |
| | 0 | 104 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 105 | | |
| | 0 | 106 | | var execution = await context.BackgroundJobExecutions.FindAsync(new object[] { executionId }, cancellationToken) |
| | 0 | 107 | | if (execution == null) return; |
| | | 108 | | |
| | 0 | 109 | | if (totalOrders > 0) execution.TotalOrders = totalOrders; |
| | 0 | 110 | | if (matchedOrders > 0) execution.MatchedOrders = matchedOrders; |
| | 0 | 111 | | if (processedOrders > 0) execution.ProcessedOrders = processedOrders; |
| | 0 | 112 | | if (successfulOrders > 0) execution.SuccessfulOrders = successfulOrders; |
| | 0 | 113 | | if (failedOrders > 0) execution.FailedOrders = failedOrders; |
| | | 114 | | |
| | 0 | 115 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | public async Task<long> StartStepAsync(long executionId, int stepNumber, string stepName, object? inputData = null, |
| | | 119 | | { |
| | 0 | 120 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 121 | | |
| | 0 | 122 | | var step = new BackgroundJobStep |
| | 0 | 123 | | { |
| | 0 | 124 | | ExecutionId = executionId, |
| | 0 | 125 | | StepNumber = stepNumber, |
| | 0 | 126 | | StepName = stepName, |
| | 0 | 127 | | Status = "Running", |
| | 0 | 128 | | StartedAt = DateTime.UtcNow, |
| | 0 | 129 | | InputData = inputData != null ? JsonConvert.SerializeObject(inputData) : null |
| | 0 | 130 | | }; |
| | | 131 | | |
| | 0 | 132 | | context.BackgroundJobSteps.Add(step); |
| | 0 | 133 | | await context.SaveChangesAsync(cancellationToken); |
| | | 134 | | |
| | 0 | 135 | | return step.Id; |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | public async Task CompleteStepAsync(long stepId, object? outputData = null, CancellationToken cancellationToken = de |
| | | 139 | | { |
| | 0 | 140 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 141 | | |
| | 0 | 142 | | var step = await context.BackgroundJobSteps.FindAsync(new object[] { stepId }, cancellationToken); |
| | 0 | 143 | | if (step == null) return; |
| | | 144 | | |
| | 0 | 145 | | step.CompletedAt = DateTime.UtcNow; |
| | 0 | 146 | | step.DurationMs = (long)(step.CompletedAt.Value - step.StartedAt).TotalMilliseconds; |
| | 0 | 147 | | step.Status = "Completed"; |
| | 0 | 148 | | step.OutputData = outputData != null ? JsonConvert.SerializeObject(outputData) : null; |
| | | 149 | | |
| | 0 | 150 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public async Task FailStepAsync(long stepId, string errorMessage, CancellationToken cancellationToken = default) |
| | | 154 | | { |
| | 0 | 155 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 156 | | |
| | 0 | 157 | | var step = await context.BackgroundJobSteps.FindAsync(new object[] { stepId }, cancellationToken); |
| | 0 | 158 | | if (step == null) return; |
| | | 159 | | |
| | 0 | 160 | | step.CompletedAt = DateTime.UtcNow; |
| | 0 | 161 | | step.DurationMs = (long)(step.CompletedAt.Value - step.StartedAt).TotalMilliseconds; |
| | 0 | 162 | | step.Status = "Failed"; |
| | 0 | 163 | | step.ErrorMessage = errorMessage; |
| | | 164 | | |
| | 0 | 165 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | public async Task<long> LogReportLoadAsync(long executionId, long? stepId, int ozonClientId, DateTime periodFrom, Da |
| | | 169 | | { |
| | 0 | 170 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 171 | | |
| | 0 | 172 | | var reportLoad = new BackgroundReportLoad |
| | 0 | 173 | | { |
| | 0 | 174 | | ExecutionId = executionId, |
| | 0 | 175 | | StepId = stepId, |
| | 0 | 176 | | OzonClientId = ozonClientId, |
| | 0 | 177 | | ReportCode = reportCode, |
| | 0 | 178 | | ReportPeriodFrom = periodFrom, |
| | 0 | 179 | | ReportPeriodTo = periodTo, |
| | 0 | 180 | | Status = "Requested", |
| | 0 | 181 | | CreatedAt = DateTime.UtcNow |
| | 0 | 182 | | }; |
| | | 183 | | |
| | 0 | 184 | | context.BackgroundReportLoads.Add(reportLoad); |
| | 0 | 185 | | await context.SaveChangesAsync(cancellationToken); |
| | | 186 | | |
| | 0 | 187 | | return reportLoad.Id; |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | public async Task UpdateReportLoadAsync(long reportLoadId, string? fileName = null, string? fileUrl = null, long? fi |
| | | 191 | | { |
| | 0 | 192 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 193 | | |
| | 0 | 194 | | var reportLoad = await context.BackgroundReportLoads.FindAsync(new object[] { reportLoadId }, cancellationToken) |
| | 0 | 195 | | if (reportLoad == null) return; |
| | | 196 | | |
| | 0 | 197 | | if (fileName != null) reportLoad.FileName = fileName; |
| | 0 | 198 | | if (fileUrl != null) reportLoad.FileUrl = fileUrl; |
| | 0 | 199 | | if (fileSizeBytes.HasValue) reportLoad.FileSizeBytes = fileSizeBytes.Value; |
| | 0 | 200 | | if (totalRows.HasValue) reportLoad.TotalRows = totalRows.Value; |
| | 0 | 201 | | if (rowsWithRequiredData.HasValue) reportLoad.RowsWithRequiredData = rowsWithRequiredData.Value; |
| | 0 | 202 | | if (uniqueOrderNumbers.HasValue) reportLoad.UniqueOrderNumbers = uniqueOrderNumbers.Value; |
| | 0 | 203 | | if (status != null) reportLoad.Status = status; |
| | 0 | 204 | | if (errorMessage != null) reportLoad.ErrorMessage = errorMessage; |
| | | 205 | | |
| | 0 | 206 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public async Task<long> LogOrderMatchAsync(long executionId, long? stepId, long? reportLoadId, string orderNumber, s |
| | | 210 | | { |
| | 0 | 211 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 212 | | |
| | 0 | 213 | | var match = new BackgroundOrderMatch |
| | 0 | 214 | | { |
| | 0 | 215 | | ExecutionId = executionId, |
| | 0 | 216 | | StepId = stepId, |
| | 0 | 217 | | ReportLoadId = reportLoadId, |
| | 0 | 218 | | OrderNumber = orderNumber, |
| | 0 | 219 | | Article = article, |
| | 0 | 220 | | OrderId = orderId, |
| | 0 | 221 | | OrderStatus = orderStatus, |
| | 0 | 222 | | IsMatchedInDb = isMatchedInDb, |
| | 0 | 223 | | IsEligibleForProcessing = isEligibleForProcessing, |
| | 0 | 224 | | RejectionReason = rejectionReason, |
| | 0 | 225 | | CreatedAt = DateTime.UtcNow |
| | 0 | 226 | | }; |
| | | 227 | | |
| | 0 | 228 | | context.BackgroundOrderMatches.Add(match); |
| | 0 | 229 | | await context.SaveChangesAsync(cancellationToken); |
| | | 230 | | |
| | 0 | 231 | | return match.Id; |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | public async Task BulkLogOrderMatchesAsync(IEnumerable<BackgroundOrderMatch> matches, CancellationToken cancellation |
| | | 235 | | { |
| | 0 | 236 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 237 | | |
| | 0 | 238 | | context.BackgroundOrderMatches.AddRange(matches); |
| | 0 | 239 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | public async Task<long> LogTransactionResultAsync(long executionId, long? stepId, long? orderMatchId, int orderId, s |
| | | 243 | | { |
| | 0 | 244 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 245 | | |
| | 0 | 246 | | var result = new BackgroundTransactionResult |
| | 0 | 247 | | { |
| | 0 | 248 | | ExecutionId = executionId, |
| | 0 | 249 | | StepId = stepId, |
| | 0 | 250 | | OrderMatchId = orderMatchId, |
| | 0 | 251 | | OrderId = orderId, |
| | 0 | 252 | | WarehouseName = warehouseName, |
| | 0 | 253 | | TransactionId = transactionId, |
| | 0 | 254 | | Status = status, |
| | 0 | 255 | | Comment = comment, |
| | 0 | 256 | | IsAutomated = true, |
| | 0 | 257 | | CreatedAt = DateTime.UtcNow |
| | 0 | 258 | | }; |
| | | 259 | | |
| | 0 | 260 | | context.BackgroundTransactionResults.Add(result); |
| | 0 | 261 | | await context.SaveChangesAsync(cancellationToken); |
| | | 262 | | |
| | 0 | 263 | | return result.Id; |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | public async Task UpdateTransactionResultAsync(long resultId, int? transactionId = null, string? oneCDocumentId = nu |
| | | 267 | | { |
| | 0 | 268 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 269 | | |
| | 0 | 270 | | var result = await context.BackgroundTransactionResults.FindAsync(new object[] { resultId }, cancellationToken); |
| | 0 | 271 | | if (result == null) return; |
| | | 272 | | |
| | 0 | 273 | | if (transactionId.HasValue) result.TransactionId = transactionId.Value; |
| | 0 | 274 | | if (oneCDocumentId != null) result.OneCDocumentId = oneCDocumentId; |
| | 0 | 275 | | if (oneCDocumentNumber != null) result.OneCDocumentNumber = oneCDocumentNumber; |
| | 0 | 276 | | if (status != null) result.Status = status; |
| | 0 | 277 | | if (errorMessage != null) result.ErrorMessage = errorMessage; |
| | 0 | 278 | | if (comment != null) result.Comment = comment; |
| | 0 | 279 | | if (isPosted.HasValue) result.IsPosted = isPosted.Value; |
| | | 280 | | |
| | 0 | 281 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | public async Task BulkUpdateTransactionResultsAsync(IEnumerable<(long resultId, int? transactionId, string? oneCDocu |
| | | 285 | | { |
| | 0 | 286 | | await using var context = _contextFactory.CreateDbContext(); |
| | | 287 | | |
| | 0 | 288 | | foreach (var update in updates) |
| | | 289 | | { |
| | 0 | 290 | | var result = await context.BackgroundTransactionResults.FindAsync(new object[] { update.resultId }, cancella |
| | 0 | 291 | | if (result == null) continue; |
| | | 292 | | |
| | 0 | 293 | | if (update.transactionId.HasValue) result.TransactionId = update.transactionId.Value; |
| | 0 | 294 | | if (update.oneCDocumentId != null) result.OneCDocumentId = update.oneCDocumentId; |
| | 0 | 295 | | if (update.oneCDocumentNumber != null) result.OneCDocumentNumber = update.oneCDocumentNumber; |
| | 0 | 296 | | if (update.status != null) result.Status = update.status; |
| | 0 | 297 | | if (update.errorMessage != null) result.ErrorMessage = update.errorMessage; |
| | 0 | 298 | | if (update.comment != null) result.Comment = update.comment; |
| | 0 | 299 | | if (update.isPosted.HasValue) result.IsPosted = update.isPosted.Value; |
| | 0 | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | await context.SaveChangesAsync(cancellationToken); |
| | 0 | 303 | | } |
| | | 304 | | |
| | | 305 | | public async Task<BackgroundJobExecution?> GetExecutionAsync(long executionId, CancellationToken cancellationToken = |
| | | 306 | | { |
| | 0 | 307 | | await using var context = _contextFactory.CreateDbContext(); |
| | 0 | 308 | | return await context.BackgroundJobExecutions |
| | 0 | 309 | | .AsNoTracking() |
| | 0 | 310 | | .FirstOrDefaultAsync(e => e.Id == executionId, cancellationToken); |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | public async Task<List<BackgroundJobStep>> GetExecutionStepsAsync(long executionId, CancellationToken cancellationTo |
| | | 314 | | { |
| | 0 | 315 | | await using var context = _contextFactory.CreateDbContext(); |
| | 0 | 316 | | return await context.BackgroundJobSteps |
| | 0 | 317 | | .AsNoTracking() |
| | 0 | 318 | | .Where(s => s.ExecutionId == executionId) |
| | 0 | 319 | | .OrderBy(s => s.StepNumber) |
| | 0 | 320 | | .ToListAsync(cancellationToken); |
| | 0 | 321 | | } |
| | | 322 | | } |