| | | 1 | | using Hangfire; |
| | | 2 | | using Hangfire.Common; |
| | | 3 | | using Hangfire.States; |
| | | 4 | | using Hangfire.Storage; |
| | | 5 | | using Microsoft.AspNetCore.SignalR; |
| | | 6 | | using OzonDomains.Models; |
| | | 7 | | using OzonRepositories.Data; |
| | | 8 | | using Services.BackgroundJobsServices; |
| | | 9 | | using Services.HangfireService; |
| | | 10 | | using Services.SignalRServices; |
| | | 11 | | |
| | | 12 | | namespace Services.BitrixStockServices; |
| | | 13 | | |
| | | 14 | | public interface IBitrixStockAutoScheduleService |
| | | 15 | | { |
| | | 16 | | Task<List<BitrixStockAutoSchedule>> GetAllAsync(CancellationToken cancellationToken = default); |
| | | 17 | | Task<BitrixStockAutoSchedule?> GetByIdAsync(int id, CancellationToken cancellationToken = default); |
| | | 18 | | Task<int> CreateAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default); |
| | | 19 | | Task UpdateAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default); |
| | | 20 | | Task DeleteAsync(int id, CancellationToken cancellationToken = default); |
| | | 21 | | Task RegisterAllActiveAsync(CancellationToken cancellationToken = default); |
| | | 22 | | Task RegisterInHangfireAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default); |
| | | 23 | | Task UnregisterFromHangfireAsync(int scheduleId, CancellationToken cancellationToken = default); |
| | | 24 | | Task TriggerManualAsync(int scheduleId, string jobId, string? userName = null, |
| | | 25 | | CancellationToken cancellationToken = default); |
| | | 26 | | Task ProcessScheduleAsync(int scheduleId, string? progressJobId = null, string? manualUserName = null, |
| | | 27 | | CancellationToken cancellationToken = default); |
| | | 28 | | Task<List<BitrixStockAutoScheduleRun>> GetRunsAsync(int scheduleId, CancellationToken cancellationToken = default); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public class BitrixStockAutoScheduleService : IBitrixStockAutoScheduleService |
| | | 32 | | { |
| | | 33 | | /// <summary>Защита от бесконечного цикла (пакетов по MaxItemsPerRun).</summary> |
| | | 34 | | private const int MaxBatchesPerScheduleRun = 2000; |
| | | 35 | | |
| | | 36 | | /// <summary>Макс. строк с артикулами в RunLog (остальное — сводка).</summary> |
| | | 37 | | private const int MaxArticleLinesInRunLog = 400; |
| | | 38 | | |
| | | 39 | | private const int MaxArticleLinesInHubLog = 25; |
| | | 40 | | |
| | | 41 | | private readonly IBitrixStockAutoScheduleRepository _repository; |
| | | 42 | | private readonly IBitrixStockInventoryService _inventoryService; |
| | | 43 | | private readonly IBitrixStockClearJobService _clearJobService; |
| | | 44 | | private readonly IBitrixStockPushJobService _pushJobService; |
| | | 45 | | private readonly IBitrixStockClearJobRepository _clearJobRepository; |
| | | 46 | | private readonly IBitrixStockPushJobRepository _pushJobRepository; |
| | | 47 | | private readonly IRecurringJobManager _recurringJobManager; |
| | | 48 | | private readonly IHubContext<JobProgressHub> _jobProgressHub; |
| | | 49 | | private readonly IBitrixStockRunFileLogger? _runFileLogger; |
| | | 50 | | |
| | 0 | 51 | | public BitrixStockAutoScheduleService( |
| | 0 | 52 | | IBitrixStockAutoScheduleRepository repository, |
| | 0 | 53 | | IBitrixStockInventoryService inventoryService, |
| | 0 | 54 | | IBitrixStockClearJobService clearJobService, |
| | 0 | 55 | | IBitrixStockPushJobService pushJobService, |
| | 0 | 56 | | IBitrixStockClearJobRepository clearJobRepository, |
| | 0 | 57 | | IBitrixStockPushJobRepository pushJobRepository, |
| | 0 | 58 | | IRecurringJobManager recurringJobManager, |
| | 0 | 59 | | IHubContext<JobProgressHub> jobProgressHub, |
| | 0 | 60 | | IBitrixStockRunFileLogger? runFileLogger = null) |
| | | 61 | | { |
| | 0 | 62 | | _repository = repository; |
| | 0 | 63 | | _inventoryService = inventoryService; |
| | 0 | 64 | | _clearJobService = clearJobService; |
| | 0 | 65 | | _pushJobService = pushJobService; |
| | 0 | 66 | | _clearJobRepository = clearJobRepository; |
| | 0 | 67 | | _pushJobRepository = pushJobRepository; |
| | 0 | 68 | | _recurringJobManager = recurringJobManager; |
| | 0 | 69 | | _jobProgressHub = jobProgressHub; |
| | 0 | 70 | | _runFileLogger = runFileLogger; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public Task<List<BitrixStockAutoSchedule>> GetAllAsync(CancellationToken cancellationToken = default) => |
| | 0 | 74 | | _repository.GetAllAsync(includeClient: true, cancellationToken); |
| | | 75 | | |
| | | 76 | | public Task<BitrixStockAutoSchedule?> GetByIdAsync(int id, CancellationToken cancellationToken = default) => |
| | 0 | 77 | | _repository.GetByIdAsync(id, cancellationToken); |
| | | 78 | | |
| | | 79 | | public async Task<int> CreateAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default) |
| | | 80 | | { |
| | 0 | 81 | | var id = await _repository.AddAsync(schedule, cancellationToken); |
| | 0 | 82 | | if (schedule.IsActive) |
| | | 83 | | { |
| | 0 | 84 | | schedule.Id = id; |
| | 0 | 85 | | await RegisterInHangfireAsync(schedule, cancellationToken); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | return id; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public async Task UpdateAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default) |
| | | 92 | | { |
| | 0 | 93 | | await _repository.UpdateAsync(schedule, cancellationToken); |
| | | 94 | | |
| | 0 | 95 | | await UnregisterFromHangfireAsync(schedule.Id, cancellationToken); |
| | 0 | 96 | | if (schedule.IsActive) |
| | | 97 | | { |
| | 0 | 98 | | await RegisterInHangfireAsync(schedule, cancellationToken); |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public async Task DeleteAsync(int id, CancellationToken cancellationToken = default) |
| | | 103 | | { |
| | 0 | 104 | | await UnregisterFromHangfireAsync(id, cancellationToken); |
| | 0 | 105 | | await _repository.DeleteAsync(id, cancellationToken); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public async Task RegisterAllActiveAsync(CancellationToken cancellationToken = default) |
| | | 109 | | { |
| | 0 | 110 | | var active = (await _repository.GetAllAsync(cancellationToken: cancellationToken)) |
| | | 111 | | .Where(s => s.IsActive) |
| | 0 | 112 | | .ToList(); |
| | 0 | 113 | | foreach (var schedule in active) |
| | | 114 | | { |
| | 0 | 115 | | await RegisterInHangfireAsync(schedule, cancellationToken); |
| | | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public async Task RegisterInHangfireAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = de |
| | | 120 | | { |
| | 0 | 121 | | var jobId = HangfireJobId(schedule.Id); |
| | 0 | 122 | | _recurringJobManager.RemoveIfExists(jobId); |
| | 0 | 123 | | _recurringJobManager.AddOrUpdate<IBitrixStockAutoScheduleService>( |
| | 0 | 124 | | jobId, |
| | 0 | 125 | | s => s.ProcessScheduleAsync(schedule.Id, null, null, CancellationToken.None), |
| | 0 | 126 | | schedule.CronExpression, |
| | 0 | 127 | | new RecurringJobOptions |
| | 0 | 128 | | { |
| | 0 | 129 | | TimeZone = RussianTimeZoneHelper.Zone |
| | 0 | 130 | | }); |
| | | 131 | | |
| | 0 | 132 | | schedule.NextRunAt = GetNextRunUtc(schedule.CronExpression); |
| | 0 | 133 | | schedule.UpdatedAt = DateTime.UtcNow; |
| | 0 | 134 | | await _repository.UpdateAsync(schedule, cancellationToken); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | public async Task UnregisterFromHangfireAsync(int scheduleId, CancellationToken cancellationToken = default) |
| | | 138 | | { |
| | 0 | 139 | | _recurringJobManager.RemoveIfExists(HangfireJobId(scheduleId)); |
| | 0 | 140 | | var schedule = await _repository.GetByIdAsync(scheduleId, cancellationToken); |
| | 0 | 141 | | if (schedule != null) |
| | | 142 | | { |
| | 0 | 143 | | schedule.NextRunAt = null; |
| | 0 | 144 | | await _repository.UpdateAsync(schedule, cancellationToken); |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | public Task TriggerManualAsync(int scheduleId, string jobId, string? userName = null, |
| | | 149 | | CancellationToken cancellationToken = default) |
| | | 150 | | { |
| | 0 | 151 | | BackgroundJob.Enqueue<IBitrixStockAutoScheduleService>( |
| | 0 | 152 | | s => s.ProcessScheduleAsync(scheduleId, jobId, userName, CancellationToken.None)); |
| | 0 | 153 | | return Task.CompletedTask; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public async Task<List<BitrixStockAutoScheduleRun>> GetRunsAsync(int scheduleId, |
| | | 157 | | CancellationToken cancellationToken = default) => |
| | 0 | 158 | | await _repository.GetRunsAsync(scheduleId, 30, cancellationToken); |
| | | 159 | | |
| | | 160 | | [Queue(HangfireQueueNames.Main)] |
| | | 161 | | public async Task ProcessScheduleAsync(int scheduleId, string? progressJobId = null, string? manualUserName = null, |
| | | 162 | | CancellationToken cancellationToken = default) |
| | | 163 | | { |
| | 0 | 164 | | var isManual = !string.IsNullOrWhiteSpace(progressJobId); |
| | 0 | 165 | | var hubJobId = progressJobId?.Trim() ?? string.Empty; |
| | 0 | 166 | | var hasHub = isManual; |
| | | 167 | | |
| | | 168 | | async Task SendLog(string message) |
| | | 169 | | { |
| | 0 | 170 | | if (_runFileLogger != null) await _runFileLogger.ProcessAsync(message, cancellationToken: cancellationToken) |
| | 0 | 171 | | if (hasHub) |
| | | 172 | | { |
| | 0 | 173 | | await _jobProgressHub.Clients.Group(hubJobId).SendAsync("Log", message, cancellationToken); |
| | | 174 | | } |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | async Task SendProgress(int current, int total, string statusText) |
| | | 178 | | { |
| | 0 | 179 | | if (hasHub) |
| | | 180 | | { |
| | 0 | 181 | | await _jobProgressHub.Clients.Group(hubJobId) |
| | 0 | 182 | | .SendAsync("Progress", current, total, statusText ?? "", cancellationToken); |
| | | 183 | | } |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | async Task SendComplete(bool success, string report) |
| | | 187 | | { |
| | 0 | 188 | | if (hasHub) |
| | | 189 | | { |
| | 0 | 190 | | await _jobProgressHub.Clients.Group(hubJobId) |
| | 0 | 191 | | .SendAsync("Complete", success, report ?? "", cancellationToken); |
| | | 192 | | } |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | async Task SendError(string message) |
| | | 196 | | { |
| | 0 | 197 | | if (hasHub) |
| | | 198 | | { |
| | 0 | 199 | | await _jobProgressHub.Clients.Group(hubJobId).SendAsync("Error", message, cancellationToken); |
| | | 200 | | } |
| | 0 | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | var schedule = await _repository.GetByIdAsync(scheduleId, cancellationToken); |
| | 0 | 204 | | if (schedule == null) |
| | | 205 | | { |
| | 0 | 206 | | await SendLog("Расписание не найдено"); |
| | 0 | 207 | | await SendComplete(false, "Расписание не найдено"); |
| | 0 | 208 | | return; |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (!schedule.IsActive && !isManual) |
| | | 212 | | { |
| | 0 | 213 | | return; |
| | | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | var isClear = string.Equals(schedule.ActionType, BitrixStockAutoActionTypes.Clear, StringComparison.OrdinalIgnor |
| | 0 | 217 | | var actionLabel = isClear ? "обнуление" : "накат"; |
| | | 218 | | |
| | 0 | 219 | | if (isManual) |
| | | 220 | | { |
| | 0 | 221 | | await SendLog($"Ручной запуск задачи «{schedule.Name}» ({actionLabel})"); |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | var run = new BitrixStockAutoScheduleRun |
| | 0 | 225 | | { |
| | 0 | 226 | | ScheduleId = scheduleId, |
| | 0 | 227 | | Status = BitrixStockAutoScheduleRunStatus.Running, |
| | 0 | 228 | | StartedAt = DateTime.UtcNow |
| | 0 | 229 | | }; |
| | 0 | 230 | | await _repository.AddRunAsync(run, cancellationToken); |
| | | 231 | | |
| | 0 | 232 | | schedule.LastRunAt = DateTime.UtcNow; |
| | 0 | 233 | | schedule.NextRunAt = GetNextRunUtc(schedule.CronExpression); |
| | 0 | 234 | | await _repository.UpdateAsync(schedule, cancellationToken); |
| | | 235 | | |
| | 0 | 236 | | using var fileRun = _runFileLogger?.BeginRun(new BitrixStockRunContext( |
| | 0 | 237 | | run.Id.ToString(), isManual ? "AutoScheduleManual" : "AutoSchedule", HangfireJobId: progressJobId, |
| | 0 | 238 | | UserName: manualUserName, ClientName: schedule.Name)); |
| | | 239 | | |
| | 0 | 240 | | var triggerType = isManual ? "Manual" : "Scheduled"; |
| | 0 | 241 | | var triggeredBy = isManual && !string.IsNullOrWhiteSpace(manualUserName) |
| | 0 | 242 | | ? $"{manualUserName} (задача #{schedule.Id})" |
| | 0 | 243 | | : $"Расписание #{schedule.Id}: {schedule.Name}"; |
| | | 244 | | |
| | | 245 | | try |
| | | 246 | | { |
| | 0 | 247 | | var lockKey = isClear ? BitrixStockClearJobService.ClearLockKey : BitrixStockPushJobService.PushLockKey; |
| | | 248 | | |
| | | 249 | | try |
| | | 250 | | { |
| | 0 | 251 | | var lockConnection = JobStorage.Current.GetConnection(); |
| | 0 | 252 | | using var distLock = lockConnection.AcquireDistributedLock(lockKey, TimeSpan.FromMilliseconds(1)); |
| | 0 | 253 | | } |
| | | 254 | | catch (DistributedLockTimeoutException) |
| | | 255 | | { |
| | | 256 | | const string lockMsg = "Другая операция Ozon уже выполняется"; |
| | 0 | 257 | | var lockJobId = isManual ? hubJobId : Guid.NewGuid().ToString(); |
| | 0 | 258 | | await RecordSkippedExecutionAsync( |
| | 0 | 259 | | isClear, lockJobId, triggeredBy, triggerType, lockMsg, run, cancellationToken); |
| | 0 | 260 | | run.Status = BitrixStockAutoScheduleRunStatus.Skipped; |
| | 0 | 261 | | run.ErrorMessage = lockMsg; |
| | 0 | 262 | | run.CompletedAt = DateTime.UtcNow; |
| | 0 | 263 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | 0 | 264 | | await SendError(lockMsg); |
| | 0 | 265 | | await SendComplete(false, lockMsg); |
| | 0 | 266 | | return; |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | var resolvedFilter = BitrixStockAutoFilterFactory.ResolveForSchedule(schedule, null); |
| | 0 | 270 | | await SendLog($"Фильтр задачи: {BitrixStockAutoFilterFactory.DescribeFilter(resolvedFilter, schedule.ActionT |
| | | 271 | | |
| | 0 | 272 | | var batchSize = schedule.MaxItemsPerRun > 0 ? schedule.MaxItemsPerRun : 500; |
| | 0 | 273 | | await SendLog($"Размер пакета: {batchSize}. Позиции обрабатываются пакетами до исчерпания фильтра."); |
| | | 274 | | |
| | 0 | 275 | | var runLog = new List<string>(); |
| | 0 | 276 | | void AddRunLog(string line) => runLog.Add(line); |
| | | 277 | | |
| | 0 | 278 | | var totalQueued = 0; |
| | 0 | 279 | | var batchNumber = 0; |
| | 0 | 280 | | var lastWarning = (string?)null; |
| | 0 | 281 | | var mappingsLogged = false; |
| | | 282 | | |
| | 0 | 283 | | while (batchNumber < MaxBatchesPerScheduleRun) |
| | | 284 | | { |
| | 0 | 285 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 286 | | batchNumber++; |
| | | 287 | | |
| | 0 | 288 | | await SendLog($"Пакет {batchNumber}: поиск позиций по фильтру…"); |
| | 0 | 289 | | await SendProgress(totalQueued, Math.Max(totalQueued, 1), |
| | 0 | 290 | | $"Пакет {batchNumber}: загрузка Bitrix и Ozon…"); |
| | | 291 | | |
| | 0 | 292 | | var collected = await _inventoryService.CollectForScheduleAsync(schedule, cancellationToken); |
| | 0 | 293 | | if (batchNumber == 1) |
| | | 294 | | { |
| | 0 | 295 | | run.CandidatesFound = collected.ProductsScanned; |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | var batchItems = isClear ? collected.ClearItems.Count : collected.PushItems.Count; |
| | 0 | 299 | | if (!string.IsNullOrEmpty(collected.Warning)) |
| | | 300 | | { |
| | 0 | 301 | | lastWarning = collected.Warning; |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | if (!mappingsLogged && |
| | 0 | 305 | | (collected.MappingsCreated > 0 || collected.MappingsUpdated > 0)) |
| | | 306 | | { |
| | 0 | 307 | | mappingsLogged = true; |
| | 0 | 308 | | var mapMsg = |
| | 0 | 309 | | $"Связи складов: создано {collected.MappingsCreated}, обновлено id {collected.MappingsUpdated}"; |
| | 0 | 310 | | AddRunLog(mapMsg); |
| | 0 | 311 | | await SendLog(mapMsg); |
| | | 312 | | } |
| | | 313 | | |
| | 0 | 314 | | if (batchNumber == 1) |
| | | 315 | | { |
| | 0 | 316 | | foreach (var line in collected.LogLines) |
| | | 317 | | { |
| | 0 | 318 | | AddRunLog(line); |
| | 0 | 319 | | await SendLog(line); |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | |
| | 0 | 323 | | if (batchItems == 0) |
| | | 324 | | { |
| | 0 | 325 | | if (batchNumber == 1) |
| | | 326 | | { |
| | 0 | 327 | | var emptyMsg = string.IsNullOrEmpty(collected.Warning) |
| | 0 | 328 | | ? "Нет позиций по фильтру" |
| | 0 | 329 | | : collected.Warning; |
| | 0 | 330 | | AddRunLog(emptyMsg ?? "Нет позиций по фильтру"); |
| | 0 | 331 | | run.RunLog = string.Join(Environment.NewLine, runLog); |
| | 0 | 332 | | var emptyJobId = isManual ? hubJobId : Guid.NewGuid().ToString(); |
| | 0 | 333 | | await RecordSkippedExecutionAsync( |
| | 0 | 334 | | isClear, emptyJobId, triggeredBy, triggerType, emptyMsg, run, cancellationToken); |
| | 0 | 335 | | run.Status = BitrixStockAutoScheduleRunStatus.Skipped; |
| | 0 | 336 | | run.ErrorMessage = emptyMsg; |
| | 0 | 337 | | run.CompletedAt = DateTime.UtcNow; |
| | 0 | 338 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | 0 | 339 | | await SendComplete(false, emptyMsg ?? "Нет позиций"); |
| | 0 | 340 | | return; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | AddRunLog($"Пакет {batchNumber}: новых позиций нет — обработка завершена."); |
| | 0 | 344 | | await SendLog("Все позиции по фильтру обработаны."); |
| | 0 | 345 | | break; |
| | | 346 | | } |
| | | 347 | | |
| | 0 | 348 | | totalQueued += batchItems; |
| | 0 | 349 | | run.ItemsQueued = totalQueued; |
| | 0 | 350 | | await SendLog( |
| | 0 | 351 | | $"Пакет {batchNumber}: проверено товаров {collected.ProductsScanned}, к {actionLabel} {batchItems} ( |
| | | 352 | | |
| | 0 | 353 | | AppendBatchArticlesRunLog(runLog, batchNumber, actionLabel, isClear, collected); |
| | 0 | 354 | | foreach (var previewLine in BuildBatchArticlesHubPreview(batchNumber, isClear, collected)) |
| | | 355 | | { |
| | 0 | 356 | | await SendLog(previewLine); |
| | | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | var executionJobId = isManual ? hubJobId : Guid.NewGuid().ToString(); |
| | 0 | 360 | | var isPartialBatch = batchItems < batchSize; |
| | | 361 | | |
| | 0 | 362 | | if (isClear) |
| | | 363 | | { |
| | 0 | 364 | | await SendLog($"Пакет {batchNumber}: задание обнуления ({batchItems} поз.)…"); |
| | 0 | 365 | | var execution = await _clearJobService.CreateExecutionAsync( |
| | 0 | 366 | | executionJobId, |
| | 0 | 367 | | collected.ClearItems, |
| | 0 | 368 | | null, |
| | 0 | 369 | | triggeredBy, |
| | 0 | 370 | | triggerType); |
| | | 371 | | |
| | 0 | 372 | | run.ClearExecutionId = execution.Id; |
| | 0 | 373 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | | 374 | | |
| | 0 | 375 | | await _clearJobService.ProcessClearAsync( |
| | 0 | 376 | | execution.Id, |
| | 0 | 377 | | executionJobId, |
| | 0 | 378 | | sendHubComplete: false, |
| | 0 | 379 | | cancellationToken); |
| | 0 | 380 | | } |
| | | 381 | | else |
| | | 382 | | { |
| | 0 | 383 | | await SendLog($"Пакет {batchNumber}: задание наката ({batchItems} поз.)…"); |
| | 0 | 384 | | var execution = await _pushJobService.CreateExecutionAsync( |
| | 0 | 385 | | executionJobId, |
| | 0 | 386 | | collected.PushItems, |
| | 0 | 387 | | null, |
| | 0 | 388 | | triggeredBy, |
| | 0 | 389 | | triggerType); |
| | | 390 | | |
| | 0 | 391 | | run.PushExecutionId = execution.Id; |
| | 0 | 392 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | | 393 | | |
| | 0 | 394 | | await _pushJobService.ProcessPushAsync( |
| | 0 | 395 | | execution.Id, |
| | 0 | 396 | | executionJobId, |
| | 0 | 397 | | sendHubComplete: false, |
| | 0 | 398 | | cancellationToken); |
| | 0 | 399 | | } |
| | | 400 | | |
| | 0 | 401 | | AddRunLog(isClear |
| | 0 | 402 | | ? $"Пакет {batchNumber}: обнуление #{run.ClearExecutionId} — {batchItems} поз." |
| | 0 | 403 | | : $"Пакет {batchNumber}: накат #{run.PushExecutionId} — {batchItems} поз."); |
| | | 404 | | |
| | 0 | 405 | | if (isPartialBatch) |
| | | 406 | | { |
| | 0 | 407 | | AddRunLog($"Последний пакет ({batchNumber}): меньше {batchSize} позиций."); |
| | 0 | 408 | | break; |
| | | 409 | | } |
| | 0 | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | if (batchNumber >= MaxBatchesPerScheduleRun) |
| | | 413 | | { |
| | 0 | 414 | | var limitMsg = |
| | 0 | 415 | | $"Достигнут лимит {MaxBatchesPerScheduleRun} пакетов за один запуск. Обработано {totalQueued} поз."; |
| | 0 | 416 | | AddRunLog(limitMsg); |
| | 0 | 417 | | await SendLog(limitMsg); |
| | 0 | 418 | | lastWarning = limitMsg; |
| | 0 | 419 | | } |
| | | 420 | | |
| | 0 | 421 | | run.Status = BitrixStockAutoScheduleRunStatus.Success; |
| | 0 | 422 | | if (!string.IsNullOrEmpty(lastWarning)) |
| | | 423 | | { |
| | 0 | 424 | | run.ErrorMessage = lastWarning; |
| | | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | var summary = isClear |
| | 0 | 428 | | ? $"Обнуление завершено: {totalQueued} поз. в {batchNumber} пакет(ах). Последнее задание #{run.ClearExec |
| | 0 | 429 | | : $"Накат завершён: {totalQueued} поз. в {batchNumber} пакет(ах). Последнее задание #{run.PushExecutionI |
| | 0 | 430 | | AddRunLog(summary); |
| | 0 | 431 | | run.RunLog = string.Join(Environment.NewLine, runLog); |
| | | 432 | | |
| | 0 | 433 | | run.CompletedAt = DateTime.UtcNow; |
| | 0 | 434 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | 0 | 435 | | if (_runFileLogger != null) await _runFileLogger.CompleteAsync(true, cancellationToken: cancellationToken); |
| | | 436 | | |
| | 0 | 437 | | if (isManual) |
| | | 438 | | { |
| | 0 | 439 | | await SendProgress(totalQueued, totalQueued, summary); |
| | 0 | 440 | | await SendComplete(string.IsNullOrEmpty(lastWarning) || totalQueued > 0, summary); |
| | | 441 | | } |
| | 0 | 442 | | } |
| | 0 | 443 | | catch (Exception ex) |
| | | 444 | | { |
| | 0 | 445 | | if (_runFileLogger != null) await _runFileLogger.CompleteAsync(false, ex.Message, CancellationToken.None); |
| | 0 | 446 | | run.Status = BitrixStockAutoScheduleRunStatus.Failed; |
| | 0 | 447 | | run.ErrorMessage = ex.Message; |
| | 0 | 448 | | run.CompletedAt = DateTime.UtcNow; |
| | 0 | 449 | | await _repository.UpdateRunAsync(run, cancellationToken); |
| | 0 | 450 | | await SendLog($"Критическая ошибка: {ex.Message}"); |
| | 0 | 451 | | await SendError(ex.Message); |
| | 0 | 452 | | await SendComplete(false, ex.Message); |
| | 0 | 453 | | if (!isManual) |
| | | 454 | | { |
| | 0 | 455 | | throw; |
| | | 456 | | } |
| | 0 | 457 | | } |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | private async Task RecordSkippedExecutionAsync( |
| | | 461 | | bool isClear, |
| | | 462 | | string jobId, |
| | | 463 | | string triggeredBy, |
| | | 464 | | string triggerType, |
| | | 465 | | string? message, |
| | | 466 | | BitrixStockAutoScheduleRun run, |
| | | 467 | | CancellationToken cancellationToken) |
| | | 468 | | { |
| | 0 | 469 | | if (isClear) |
| | | 470 | | { |
| | 0 | 471 | | var execution = await _clearJobService.CreateExecutionAsync( |
| | 0 | 472 | | jobId, |
| | 0 | 473 | | Array.Empty<BitrixStockClearItemDto>(), |
| | 0 | 474 | | null, |
| | 0 | 475 | | triggeredBy, |
| | 0 | 476 | | triggerType, |
| | 0 | 477 | | cancellationToken); |
| | 0 | 478 | | await _clearJobRepository.CompleteExecutionAsync( |
| | 0 | 479 | | execution.Id, BitrixStockClearExecutionStatus.Success, message, cancellationToken); |
| | 0 | 480 | | run.ClearExecutionId = execution.Id; |
| | 0 | 481 | | } |
| | | 482 | | else |
| | | 483 | | { |
| | 0 | 484 | | var execution = await _pushJobService.CreateExecutionAsync( |
| | 0 | 485 | | jobId, |
| | 0 | 486 | | Array.Empty<BitrixStockPushItemDto>(), |
| | 0 | 487 | | null, |
| | 0 | 488 | | triggeredBy, |
| | 0 | 489 | | triggerType, |
| | 0 | 490 | | cancellationToken); |
| | 0 | 491 | | await _pushJobRepository.CompleteExecutionAsync( |
| | 0 | 492 | | execution.Id, BitrixStockPushExecutionStatus.Success, message, cancellationToken); |
| | 0 | 493 | | run.PushExecutionId = execution.Id; |
| | 0 | 494 | | } |
| | 0 | 495 | | } |
| | | 496 | | |
| | | 497 | | private static void AppendBatchArticlesRunLog( |
| | | 498 | | ICollection<string> runLog, |
| | | 499 | | int batchNumber, |
| | | 500 | | string actionLabel, |
| | | 501 | | bool isClear, |
| | | 502 | | BitrixStockAutoCollectResult collected) |
| | | 503 | | { |
| | 0 | 504 | | if (isClear) |
| | | 505 | | { |
| | 0 | 506 | | AppendClearItemsRunLog(runLog, batchNumber, actionLabel, collected.ClearItems); |
| | | 507 | | } |
| | | 508 | | else |
| | | 509 | | { |
| | 0 | 510 | | AppendPushItemsRunLog(runLog, batchNumber, actionLabel, collected.PushItems); |
| | | 511 | | } |
| | 0 | 512 | | } |
| | | 513 | | |
| | | 514 | | private static IEnumerable<string> BuildBatchArticlesHubPreview( |
| | | 515 | | int batchNumber, |
| | | 516 | | bool isClear, |
| | | 517 | | BitrixStockAutoCollectResult collected) |
| | | 518 | | { |
| | 0 | 519 | | var lines = isClear |
| | 0 | 520 | | ? FormatClearItemLines(collected.ClearItems) |
| | 0 | 521 | | : FormatPushItemLines(collected.PushItems); |
| | | 522 | | |
| | 0 | 523 | | if (lines.Count == 0) |
| | | 524 | | { |
| | 0 | 525 | | yield break; |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | var uniqueArticles = lines |
| | | 529 | | .Select(l => l.ArticleKey) |
| | 0 | 530 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 531 | | .Count(); |
| | | 532 | | |
| | 0 | 533 | | yield return $"Пакет {batchNumber}: артикулы ({uniqueArticles} уник., {lines.Count} поз.):"; |
| | | 534 | | |
| | 0 | 535 | | foreach (var line in lines.Take(MaxArticleLinesInHubLog)) |
| | | 536 | | { |
| | 0 | 537 | | yield return " " + line.Text; |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | if (lines.Count > MaxArticleLinesInHubLog) |
| | | 541 | | { |
| | 0 | 542 | | yield return $" … ещё {lines.Count - MaxArticleLinesInHubLog} поз. (полный список в журнале запуска)"; |
| | | 543 | | } |
| | 0 | 544 | | } |
| | | 545 | | |
| | | 546 | | private static void AppendClearItemsRunLog( |
| | | 547 | | ICollection<string> runLog, |
| | | 548 | | int batchNumber, |
| | | 549 | | string actionLabel, |
| | | 550 | | IReadOnlyList<BitrixStockClearItemDto> items) |
| | | 551 | | { |
| | 0 | 552 | | var lines = FormatClearItemLines(items); |
| | 0 | 553 | | if (lines.Count == 0) |
| | | 554 | | { |
| | 0 | 555 | | return; |
| | | 556 | | } |
| | | 557 | | |
| | 0 | 558 | | var uniqueArticles = lines.Select(l => l.ArticleKey).Distinct(StringComparer.OrdinalIgnoreCase).Count(); |
| | 0 | 559 | | runLog.Add($"Пакет {batchNumber} — к {actionLabel} ({lines.Count} поз., {uniqueArticles} арт.):"); |
| | 0 | 560 | | AppendFormattedLines(runLog, lines); |
| | 0 | 561 | | } |
| | | 562 | | |
| | | 563 | | private static void AppendPushItemsRunLog( |
| | | 564 | | ICollection<string> runLog, |
| | | 565 | | int batchNumber, |
| | | 566 | | string actionLabel, |
| | | 567 | | IReadOnlyList<BitrixStockPushItemDto> items) |
| | | 568 | | { |
| | 0 | 569 | | var lines = FormatPushItemLines(items); |
| | 0 | 570 | | if (lines.Count == 0) |
| | | 571 | | { |
| | 0 | 572 | | return; |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | var uniqueArticles = lines.Select(l => l.ArticleKey).Distinct(StringComparer.OrdinalIgnoreCase).Count(); |
| | 0 | 576 | | runLog.Add($"Пакет {batchNumber} — к {actionLabel} ({lines.Count} поз., {uniqueArticles} арт.):"); |
| | 0 | 577 | | AppendFormattedLines(runLog, lines); |
| | 0 | 578 | | } |
| | | 579 | | |
| | | 580 | | private static void AppendFormattedLines(ICollection<string> runLog, List<BatchArticleLogLine> lines) |
| | | 581 | | { |
| | 0 | 582 | | foreach (var line in lines.Take(MaxArticleLinesInRunLog)) |
| | | 583 | | { |
| | 0 | 584 | | runLog.Add(" " + line.Text); |
| | | 585 | | } |
| | | 586 | | |
| | 0 | 587 | | if (lines.Count > MaxArticleLinesInRunLog) |
| | | 588 | | { |
| | 0 | 589 | | runLog.Add($" … ещё {lines.Count - MaxArticleLinesInRunLog} поз. (обрезано в журнале)"); |
| | | 590 | | } |
| | 0 | 591 | | } |
| | | 592 | | |
| | | 593 | | private static List<BatchArticleLogLine> FormatClearItemLines(IReadOnlyList<BitrixStockClearItemDto> items) => |
| | 0 | 594 | | items |
| | 0 | 595 | | .OrderBy(i => i.Article ?? i.OfferId, StringComparer.OrdinalIgnoreCase) |
| | 0 | 596 | | .ThenBy(i => i.WarehouseName) |
| | 0 | 597 | | .Select(i => new BatchArticleLogLine( |
| | 0 | 598 | | ResolveArticleKey(i.Article, i.OfferId ?? ""), |
| | 0 | 599 | | $"{ResolveArticleKey(i.Article, i.OfferId ?? "")} | {i.WarehouseName ?? i.WarehouseId.ToString()} ({i.Pr |
| | 0 | 600 | | .ToList(); |
| | | 601 | | |
| | | 602 | | private static List<BatchArticleLogLine> FormatPushItemLines(IReadOnlyList<BitrixStockPushItemDto> items) => |
| | 0 | 603 | | items |
| | 0 | 604 | | .OrderBy(i => i.Article ?? i.OfferId, StringComparer.OrdinalIgnoreCase) |
| | 0 | 605 | | .ThenBy(i => i.WarehouseName) |
| | 0 | 606 | | .Select(i => new BatchArticleLogLine( |
| | 0 | 607 | | ResolveArticleKey(i.Article, i.OfferId ?? ""), |
| | 0 | 608 | | $"{ResolveArticleKey(i.Article, i.OfferId ?? "")} | {i.WarehouseName ?? i.WarehouseId.ToString()} Bitrix |
| | 0 | 609 | | .ToList(); |
| | | 610 | | |
| | | 611 | | private static string ResolveArticleKey(string? article, string offerId) |
| | | 612 | | { |
| | 0 | 613 | | if (!string.IsNullOrWhiteSpace(article)) |
| | | 614 | | { |
| | 0 | 615 | | return article.Trim(); |
| | | 616 | | } |
| | | 617 | | |
| | 0 | 618 | | if (!string.IsNullOrWhiteSpace(offerId) && offerId.Contains('=')) |
| | | 619 | | { |
| | 0 | 620 | | return offerId.Split('=')[0].Trim(); |
| | | 621 | | } |
| | | 622 | | |
| | 0 | 623 | | return offerId?.Trim() ?? "—"; |
| | | 624 | | } |
| | | 625 | | |
| | 0 | 626 | | private sealed record BatchArticleLogLine(string ArticleKey, string Text); |
| | | 627 | | |
| | 0 | 628 | | private static string HangfireJobId(int scheduleId) => $"bitrix-stock-auto-schedule-{scheduleId}"; |
| | | 629 | | |
| | | 630 | | private static DateTime? GetNextRunUtc(string cronExpression) |
| | | 631 | | { |
| | | 632 | | try |
| | | 633 | | { |
| | 0 | 634 | | var expr = Cronos.CronExpression.Parse(cronExpression); |
| | 0 | 635 | | var tz = RussianTimeZoneHelper.Zone; |
| | 0 | 636 | | var nowRu = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz); |
| | 0 | 637 | | var next = expr.GetNextOccurrence(nowRu, tz); |
| | 0 | 638 | | return next.HasValue ? TimeZoneInfo.ConvertTimeToUtc(next.Value, tz) : null; |
| | | 639 | | } |
| | 0 | 640 | | catch |
| | | 641 | | { |
| | 0 | 642 | | return null; |
| | | 643 | | } |
| | 0 | 644 | | } |
| | | 645 | | } |