| | | 1 | | using Hangfire; |
| | | 2 | | using Microsoft.AspNetCore.SignalR; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Services.HangfireService; |
| | | 5 | | using Services.SignalRServices; |
| | | 6 | | |
| | | 7 | | namespace Services.BitrixStockServices; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Единый серверный оркестратор цепочки этапов BitrixStock. См. <see cref="IBitrixStockSyncPipelineOrchestrator"/>. |
| | | 11 | | /// Модель — как у <c>RunAllPresetsJob</c>: последовательные await в одном задании. UI получает прогресс через |
| | | 12 | | /// единую SignalR-подписку на группу прогона (события PipelineStarted / PipelineStage / PipelineFinished). |
| | | 13 | | /// </summary> |
| | | 14 | | public class BitrixStockSyncPipelineOrchestrator : IBitrixStockSyncPipelineOrchestrator |
| | | 15 | | { |
| | | 16 | | private readonly IBitrixStockSyncReportService _reportService; |
| | | 17 | | private readonly IBitrixStockSyncReportApplyService _applyService; |
| | | 18 | | private readonly IBitrixStockSyncReportCleanupService _cleanupService; |
| | | 19 | | private readonly IBitrixStockSyncReportPricePushService _pricePushService; |
| | | 20 | | private readonly IBitrixStockOzonOrphanCleanupService _orphanCleanupService; |
| | | 21 | | private readonly IHubContext<JobProgressHub> _jobProgressHub; |
| | | 22 | | private readonly BitrixStockActiveStageStore _stageStore; |
| | | 23 | | private readonly BitrixStockRolloutTelegramReporter _telegramReporter; |
| | | 24 | | private readonly ILogger<BitrixStockSyncPipelineOrchestrator> _logger; |
| | | 25 | | private readonly IBitrixStockRunFileLogger? _runFileLogger; |
| | | 26 | | |
| | 19 | 27 | | public BitrixStockSyncPipelineOrchestrator( |
| | 19 | 28 | | IBitrixStockSyncReportService reportService, |
| | 19 | 29 | | IBitrixStockSyncReportApplyService applyService, |
| | 19 | 30 | | IBitrixStockSyncReportCleanupService cleanupService, |
| | 19 | 31 | | IBitrixStockSyncReportPricePushService pricePushService, |
| | 19 | 32 | | IBitrixStockOzonOrphanCleanupService orphanCleanupService, |
| | 19 | 33 | | IHubContext<JobProgressHub> jobProgressHub, |
| | 19 | 34 | | BitrixStockActiveStageStore stageStore, |
| | 19 | 35 | | BitrixStockRolloutTelegramReporter telegramReporter, |
| | 19 | 36 | | ILogger<BitrixStockSyncPipelineOrchestrator> logger, |
| | 19 | 37 | | IBitrixStockRunFileLogger? runFileLogger = null) |
| | | 38 | | { |
| | 19 | 39 | | _reportService = reportService; |
| | 19 | 40 | | _applyService = applyService; |
| | 19 | 41 | | _cleanupService = cleanupService; |
| | 19 | 42 | | _pricePushService = pricePushService; |
| | 19 | 43 | | _orphanCleanupService = orphanCleanupService; |
| | 19 | 44 | | _jobProgressHub = jobProgressHub; |
| | 19 | 45 | | _stageStore = stageStore; |
| | 19 | 46 | | _telegramReporter = telegramReporter; |
| | 19 | 47 | | _logger = logger; |
| | 19 | 48 | | _runFileLogger = runFileLogger; |
| | 19 | 49 | | } |
| | | 50 | | |
| | | 51 | | [Queue(HangfireQueueNames.Main)] |
| | | 52 | | public async Task<BitrixStockPipelineResult> RunPipelineAsync( |
| | | 53 | | string reportId, |
| | | 54 | | BitrixStockSyncReportStartRequest request, |
| | | 55 | | string? rootJobId, |
| | | 56 | | CancellationToken cancellationToken = default) |
| | | 57 | | { |
| | 19 | 58 | | var hubId = rootJobId?.Trim() ?? string.Empty; |
| | 19 | 59 | | var hasHub = !string.IsNullOrWhiteSpace(hubId); |
| | 19 | 60 | | var ss = request.StageSettings; |
| | 19 | 61 | | var result = new BitrixStockPipelineResult(); |
| | 19 | 62 | | using var fileRun = _runFileLogger?.BeginRun(new BitrixStockRunContext( |
| | 19 | 63 | | reportId, rootJobId == null ? "PresetPipeline" : "UI", reportId, rootJobId, |
| | 19 | 64 | | ClientName: string.Join(",", request.OzonClientIds))); |
| | | 65 | | |
| | | 66 | | async Task EmitStarted(IReadOnlyList<int> stages) |
| | | 67 | | { |
| | 17 | 68 | | if (hasHub) |
| | 3 | 69 | | await _jobProgressHub.Clients.Group(hubId) |
| | 3 | 70 | | .SendAsync("PipelineStarted", new { reportId, stages }, cancellationToken); |
| | 17 | 71 | | } |
| | | 72 | | |
| | | 73 | | async Task EmitStage(int stage, string state, object? stats = null) |
| | | 74 | | { |
| | 80 | 75 | | if (hasHub) |
| | 9 | 76 | | await _jobProgressHub.Clients.Group(hubId) |
| | 9 | 77 | | .SendAsync("PipelineStage", new { reportId, stage, state, stats }, cancellationToken); |
| | 80 | 78 | | } |
| | | 79 | | |
| | | 80 | | async Task EmitFinished(bool success, string? error) |
| | | 81 | | { |
| | 17 | 82 | | if (hasHub) |
| | 3 | 83 | | await _jobProgressHub.Clients.Group(hubId) |
| | 3 | 84 | | .SendAsync("PipelineFinished", new { reportId, success, error }, cancellationToken); |
| | 17 | 85 | | } |
| | | 86 | | |
| | 19 | 87 | | if (_stageStore.Get(reportId) != 0) |
| | | 88 | | { |
| | 1 | 89 | | _logger.LogInformation("Pipeline {ReportId} уже выполняется — повторный запуск отменён", reportId); |
| | 1 | 90 | | result.Error = "Цепочка уже выполняется"; |
| | 1 | 91 | | return result; |
| | | 92 | | } |
| | | 93 | | |
| | 18 | 94 | | var otherActive = _stageStore.GetAnyActiveReportId(); |
| | 18 | 95 | | if (otherActive != null && !string.Equals(otherActive, reportId, StringComparison.Ordinal)) |
| | | 96 | | { |
| | 1 | 97 | | _logger.LogInformation("Pipeline {ReportId} не запущен: уже активна цепочка {Other}", reportId, otherActive) |
| | 1 | 98 | | result.Error = $"Уже выполняется цепочка {otherActive}"; |
| | 1 | 99 | | return result; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | try |
| | | 103 | | { |
| | 17 | 104 | | await EmitStarted(PlanStages(ss)); |
| | 17 | 105 | | await FileProcessAsync("Запуск pipeline этапов 1–5"); |
| | | 106 | | |
| | 17 | 107 | | _stageStore.Set(reportId, 1); |
| | 17 | 108 | | await FileProcessAsync("Этап 1: формирование отчёта"); |
| | 17 | 109 | | await EmitStage(1, "running"); |
| | 17 | 110 | | request.Chained = true; |
| | 17 | 111 | | await _reportService.BuildReportAsync(reportId, request, hasHub ? hubId : null, cancellationToken); |
| | 13 | 112 | | await EmitStage(1, "done"); |
| | 13 | 113 | | await FileProcessAsync("Этап 1 завершён"); |
| | 13 | 114 | | result.LastStage = 1; |
| | | 115 | | |
| | 13 | 116 | | if (ss?.AutoRunApply == true) |
| | | 117 | | { |
| | 11 | 118 | | _stageStore.Set(reportId, 2); |
| | 11 | 119 | | await FileProcessAsync("Этап 2: накат остатков"); |
| | 11 | 120 | | await EmitStage(2, "running"); |
| | 11 | 121 | | var applyResult = await _applyService.ApplyReportAsync( |
| | 11 | 122 | | reportId, BuildApplyRequest(reportId, ss, hubId), hasHub ? hubId : null, cancellationToken); |
| | 11 | 123 | | await EmitStage(2, "done", new { applyResult.SuccessCount, applyResult.FailedCount, applyResult.Total }) |
| | 11 | 124 | | result.LastStage = 2; |
| | 11 | 125 | | await FileProcessAsync($"Этап 2 завершён: успешно {applyResult.SuccessCount}, ошибок {applyResult.Failed |
| | | 126 | | |
| | 11 | 127 | | if (applyResult.SuccessCount > 0) |
| | | 128 | | { |
| | 8 | 129 | | if (ss.AutoRunCleanup) |
| | | 130 | | { |
| | 4 | 131 | | _stageStore.Set(reportId, 3); |
| | 4 | 132 | | await FileProcessAsync("Этап 3: очистка FBS"); |
| | 4 | 133 | | await EmitStage(3, "running"); |
| | 4 | 134 | | await _cleanupService.RunCleanupAsync( |
| | 4 | 135 | | reportId, BuildCleanupRequest(reportId, ss, hubId), hasHub ? hubId : null, cancellationToken |
| | 4 | 136 | | await EmitStage(3, "done"); |
| | 4 | 137 | | result.LastStage = 3; |
| | 4 | 138 | | await FileProcessAsync("Этап 3 завершён"); |
| | | 139 | | } |
| | | 140 | | |
| | 8 | 141 | | if (ss.AutoRunPricePush) |
| | | 142 | | { |
| | 3 | 143 | | _stageStore.Set(reportId, 4); |
| | 3 | 144 | | await FileProcessAsync("Этап 4: накат цен"); |
| | 3 | 145 | | await EmitStage(4, "running"); |
| | 3 | 146 | | await _pricePushService.PushPricesAsync( |
| | 3 | 147 | | reportId, BuildPricePushRequest(reportId, ss, hubId), hasHub ? hubId : null, cancellationTok |
| | 3 | 148 | | await EmitStage(4, "done"); |
| | 3 | 149 | | result.LastStage = 4; |
| | 3 | 150 | | await FileProcessAsync("Этап 4 завершён"); |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 6 | 155 | | if (ss.AutoRunCleanup) await EmitStage(3, "skipped", new { reason = "0 успешных применений" }); |
| | 4 | 156 | | if (ss.AutoRunPricePush) await EmitStage(4, "skipped", new { reason = "0 успешных применений" }); |
| | 3 | 157 | | _logger.LogInformation("Pipeline {ReportId}: этап 2 без успешных применений — этапы 3/4 пропущены", |
| | | 158 | | } |
| | 11 | 159 | | } |
| | | 160 | | |
| | 13 | 161 | | if (ss?.AutoRunOzonOrphanCleanup == true) |
| | | 162 | | { |
| | 4 | 163 | | var catalogSource = ss.Stage5CatalogSource ?? "OzonProductsDb"; |
| | 4 | 164 | | _logger.LogInformation( |
| | 4 | 165 | | "Pipeline {ReportId}: этап 5 — источник каталога {CatalogSource}", |
| | 4 | 166 | | reportId, catalogSource); |
| | 4 | 167 | | _stageStore.Set(reportId, 5); |
| | 4 | 168 | | await FileProcessAsync("Этап 5: обработка сирот Ozon"); |
| | 4 | 169 | | await EmitStage(5, "running", new { catalogSource }); |
| | 4 | 170 | | await _orphanCleanupService.RunOrphanCleanupAsync( |
| | 4 | 171 | | reportId, BuildOrphanRequest(reportId, ss, hubId), hasHub ? hubId : null, cancellationToken); |
| | 4 | 172 | | await EmitStage(5, "done"); |
| | 4 | 173 | | result.LastStage = 5; |
| | 4 | 174 | | await FileProcessAsync("Этап 5 завершён"); |
| | 4 | 175 | | } |
| | | 176 | | |
| | 13 | 177 | | result.Success = true; |
| | 13 | 178 | | if (_runFileLogger != null) await _runFileLogger.CompleteAsync(true, cancellationToken: cancellationToken); |
| | 13 | 179 | | await TryNotifyCompletedAsync(reportId, result.LastStage, cancellationToken); |
| | 13 | 180 | | await EmitFinished(true, null); |
| | 13 | 181 | | return result; |
| | | 182 | | } |
| | 2 | 183 | | catch (OperationCanceledException) |
| | | 184 | | { |
| | 2 | 185 | | if (_runFileLogger != null) await _runFileLogger.CompleteAsync(false, "Отменено", CancellationToken.None); |
| | 2 | 186 | | await EmitFinished(false, "Отменено"); |
| | 2 | 187 | | throw; |
| | 0 | 188 | | } |
| | 2 | 189 | | catch (Exception ex) |
| | | 190 | | { |
| | 2 | 191 | | if (_runFileLogger != null) await _runFileLogger.CompleteAsync(false, ex.Message, CancellationToken.None); |
| | 2 | 192 | | _logger.LogError(ex, "Pipeline {ReportId} прерван на этапе {Stage}", reportId, result.LastStage + 1); |
| | 2 | 193 | | result.Error = ex.Message; |
| | 2 | 194 | | await EmitStage(result.LastStage + 1, "failed", new { error = ex.Message }); |
| | 2 | 195 | | await TryNotifyFailedAsync(reportId, result.LastStage + 1, ex.Message); |
| | 2 | 196 | | await EmitFinished(false, ex.Message); |
| | 2 | 197 | | return result; |
| | | 198 | | } |
| | | 199 | | finally |
| | | 200 | | { |
| | 17 | 201 | | _stageStore.Clear(reportId); |
| | | 202 | | } |
| | | 203 | | |
| | 91 | 204 | | Task FileProcessAsync(string message) => _runFileLogger?.ProcessAsync(message, cancellationToken: cancellationTo |
| | 17 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary>Сообщение в Telegram о том, что конвейер прерван: этап, ошибка и то, что успело примениться.</summary> |
| | | 208 | | private async Task TryNotifyFailedAsync(string reportId, int failedStage, string error) |
| | | 209 | | { |
| | | 210 | | try |
| | | 211 | | { |
| | 2 | 212 | | BitrixStockSyncReportRunDto? run = null; |
| | | 213 | | try |
| | | 214 | | { |
| | 2 | 215 | | var page = await _reportService.ListReportsAsync(new BitrixStockSyncReportRunsQuery { PageSize = 5 }, Ca |
| | 0 | 216 | | run = page.Items.FirstOrDefault(r => r.ReportId == reportId); |
| | 0 | 217 | | } |
| | 2 | 218 | | catch |
| | | 219 | | { |
| | | 220 | | // Отчёта может не быть (сбой на этапе 1) — сообщим без частичных результатов. |
| | 2 | 221 | | } |
| | | 222 | | |
| | 2 | 223 | | await _telegramReporter.NotifyFailedAsync(reportId, run, failedStage, error); |
| | 0 | 224 | | } |
| | 2 | 225 | | catch |
| | | 226 | | { |
| | | 227 | | // Уведомление не должно менять исход пайплайна. |
| | 2 | 228 | | } |
| | 2 | 229 | | } |
| | | 230 | | |
| | | 231 | | private async Task TryNotifyCompletedAsync(string reportId, int lastStage, CancellationToken ct) |
| | | 232 | | { |
| | | 233 | | BitrixStockSyncReportRunDto? run; |
| | | 234 | | try |
| | | 235 | | { |
| | 13 | 236 | | var page = await _reportService.ListReportsAsync( |
| | 13 | 237 | | new BitrixStockSyncReportRunsQuery { PageSize = 5 }, ct); |
| | 0 | 238 | | run = page.Items.FirstOrDefault(r => r.ReportId == reportId); |
| | 0 | 239 | | } |
| | 26 | 240 | | catch { return; } |
| | | 241 | | |
| | 0 | 242 | | if (run == null) return; |
| | | 243 | | |
| | | 244 | | // Необнулённая сирота — такая же ошибка, как отказ этапа 2: остаток продолжает продаваться |
| | | 245 | | // при нулях в 1С, пока кто-нибудь не заметит расхождение вручную. |
| | 0 | 246 | | var hasErrors = run.OzonApplyFailedCount > 0 || run.PricePushErrorCount > 0 |
| | 0 | 247 | | || run.OzonCleanupFailedCount > 0 || run.OrphansZeroed < run.OrphansFound; |
| | | 248 | | |
| | 0 | 249 | | try { await NotifyBellAsync(run, hasErrors); } |
| | 0 | 250 | | catch { /* не роняем пайплайн */ } |
| | | 251 | | |
| | 0 | 252 | | try { await _telegramReporter.NotifyAsync(run, ct); } |
| | 0 | 253 | | catch { /* не роняем пайплайн */ } |
| | 13 | 254 | | } |
| | | 255 | | |
| | | 256 | | /// <summary>Короткое уведомление-колокольчик в UI (шапка Studio2).</summary> |
| | | 257 | | private static async Task NotifyBellAsync(BitrixStockSyncReportRunDto run, bool hasErrors) |
| | | 258 | | { |
| | 0 | 259 | | var parts = new List<string>(); |
| | | 260 | | |
| | 0 | 261 | | if (run.PushCount > 0 || run.DecreaseCount > 0 || run.ClearCount > 0) |
| | 0 | 262 | | parts.Add($"↑{run.PushCount} добавить · ↓{run.DecreaseCount} убавить · ⊘{run.ClearCount} обнулить"); |
| | | 263 | | |
| | 0 | 264 | | if (run.OzonAppliedSuccessCount > 0 || run.OzonApplyFailedCount > 0) |
| | 0 | 265 | | parts.Add($"применено {run.OzonAppliedSuccessCount}" + |
| | 0 | 266 | | (run.OzonApplyFailedCount > 0 ? $", ошибок {run.OzonApplyFailedCount}" : "")); |
| | | 267 | | |
| | 0 | 268 | | if (run.PricePushCount > 0 || run.PricePushUnchangedCount > 0) |
| | 0 | 269 | | parts.Add($"₽{run.PricePushCount} цен" + |
| | 0 | 270 | | (run.PricePushUnchangedCount > 0 ? $" · ≈{run.PricePushUnchangedCount} без изм" : "")); |
| | | 271 | | |
| | 0 | 272 | | if (run.OrphansFound > 0 || run.OrphansZeroed > 0) |
| | 0 | 273 | | parts.Add($"{run.OrphansFound}/{run.OrphansZeroed} сирот"); |
| | | 274 | | |
| | 0 | 275 | | if (parts.Count == 0) return; |
| | | 276 | | |
| | 0 | 277 | | await NotificationService.NotifyAsync( |
| | 0 | 278 | | "Bitrix Остатки", |
| | 0 | 279 | | hasErrors ? "#D97706" : "#6366F1", |
| | 0 | 280 | | string.Join(" | ", parts), |
| | 0 | 281 | | "fa-arrows-rotate"); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | private static List<int> PlanStages(PresetStageSettings? ss) |
| | | 285 | | { |
| | 17 | 286 | | var stages = new List<int> { 1 }; |
| | 17 | 287 | | if (ss?.AutoRunApply == true) |
| | | 288 | | { |
| | 15 | 289 | | stages.Add(2); |
| | 22 | 290 | | if (ss.AutoRunCleanup) stages.Add(3); |
| | 19 | 291 | | if (ss.AutoRunPricePush) stages.Add(4); |
| | | 292 | | } |
| | 21 | 293 | | if (ss?.AutoRunOzonOrphanCleanup == true) stages.Add(5); |
| | 17 | 294 | | return stages; |
| | | 295 | | } |
| | | 296 | | |
| | 11 | 297 | | private static BitrixStockSyncReportApplyStartRequest BuildApplyRequest(string reportId, PresetStageSettings ss, str |
| | 11 | 298 | | { |
| | 11 | 299 | | JobId = rootJobId, |
| | 11 | 300 | | ReportId = reportId, |
| | 11 | 301 | | OnlyPending = true, |
| | 11 | 302 | | Actions = (ss.ApplyActions ?? []).Count > 0 |
| | 11 | 303 | | ? ss.ApplyActions! |
| | 11 | 304 | | : new List<string> { "Push", "Decrease", "Clear" }, |
| | 11 | 305 | | ArchivedMode = (ArchivedProductMode)ss.ApplyArchivedMode, |
| | 11 | 306 | | UnarchiveFilter = (UnarchiveFilterMode)ss.ApplyUnarchiveFilter, |
| | 11 | 307 | | MaxLines = ss.ApplyMaxLines, |
| | 11 | 308 | | DisabledWarehouseMappingIds = (ss.ApplyDisabledWarehouseMappingIds ?? []).Count > 0 |
| | 11 | 309 | | ? ss.ApplyDisabledWarehouseMappingIds |
| | 11 | 310 | | : null, |
| | 11 | 311 | | StageSettings = ss, |
| | 11 | 312 | | Chained = true |
| | 11 | 313 | | }; |
| | | 314 | | |
| | 4 | 315 | | private static BitrixStockSyncCleanupStartRequest BuildCleanupRequest(string reportId, PresetStageSettings ss, strin |
| | 4 | 316 | | { |
| | 4 | 317 | | JobId = rootJobId, |
| | 4 | 318 | | ReportId = reportId, |
| | 4 | 319 | | OnlyPending = ss.CleanupOnlyPending, |
| | 4 | 320 | | MaxProducts = null, |
| | 4 | 321 | | StageSettings = ss, |
| | 4 | 322 | | Chained = true |
| | 4 | 323 | | }; |
| | | 324 | | |
| | 3 | 325 | | private static BitrixStockSyncReportPricePushStartRequest BuildPricePushRequest(string reportId, PresetStageSettings |
| | 3 | 326 | | { |
| | 3 | 327 | | JobId = rootJobId, |
| | 3 | 328 | | ReportId = reportId, |
| | 3 | 329 | | OnlyPending = true, |
| | 3 | 330 | | StageSettings = ss, |
| | 3 | 331 | | Chained = true |
| | 3 | 332 | | }; |
| | | 333 | | |
| | 4 | 334 | | private static BitrixStockOzonOrphanCleanupRequest BuildOrphanRequest(string reportId, PresetStageSettings ss, strin |
| | 4 | 335 | | { |
| | 4 | 336 | | ReportId = reportId, |
| | 4 | 337 | | JobId = rootJobId, |
| | 4 | 338 | | DryRun = false, |
| | 4 | 339 | | StageSettings = ss, |
| | 4 | 340 | | Chained = true |
| | 4 | 341 | | }; |
| | | 342 | | } |