| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net; |
| | | 5 | | using System.Net.Http; |
| | | 6 | | using System.Net.Sockets; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Hangfire; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | using OzonDomains.Models; |
| | | 12 | | using OzonDomains.Models.OzonWebhook; |
| | | 13 | | using Services.DataServices; |
| | | 14 | | using Services.HangfireService; |
| | | 15 | | using Services.OzonWebhookServices.Handlers; |
| | | 16 | | using Services.OzonWebhookServices.Models; |
| | | 17 | | |
| | | 18 | | namespace Services.OzonWebhookServices |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Обработка push-уведомлений Ozon. |
| | | 22 | | /// HTTP-путь только подтверждает приём (< 5 с для Ozon); запись в БД и бизнес-логика — в Hangfire. |
| | | 23 | | /// </summary> |
| | | 24 | | public sealed class OzonWebhookService : IOzonWebhookService |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Высокочастотные типы без бизнес-обработчика — не пишем в БД (иначе забивается пул MySQL). |
| | | 28 | | /// </summary> |
| | 1 | 29 | | private static readonly HashSet<OzonMessageType> AckOnlyMessageTypes = new() |
| | 1 | 30 | | { |
| | 1 | 31 | | OzonMessageType.TYPE_STOCKS_CHANGED |
| | 1 | 32 | | }; |
| | | 33 | | |
| | | 34 | | private readonly IOzonWebhookNotificationFactory _factory; |
| | | 35 | | private readonly OzonPushNotificationDataService _dataService; |
| | | 36 | | private readonly IEnumerable<IWebhookNotificationHandler> _handlers; |
| | | 37 | | private readonly ILogger<OzonWebhookService> _logger; |
| | | 38 | | |
| | 4 | 39 | | public OzonWebhookService( |
| | 4 | 40 | | IOzonWebhookNotificationFactory factory, |
| | 4 | 41 | | OzonPushNotificationDataService dataService, |
| | 4 | 42 | | IEnumerable<IWebhookNotificationHandler> handlers, |
| | 4 | 43 | | ILogger<OzonWebhookService> logger) |
| | | 44 | | { |
| | 4 | 45 | | _factory = factory; |
| | 4 | 46 | | _dataService = dataService; |
| | 4 | 47 | | _handlers = handlers; |
| | 4 | 48 | | _logger = logger; |
| | 4 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Максимальное время на постановку задачи в очередь Hangfire в HTTP-пути. |
| | | 53 | | /// Ozon мониторит время ответа (норма до 1,5 с; свыше 2,5 с адрес «Недоступен» и отключается); не блокируем пот |
| | | 54 | | /// </summary> |
| | 1 | 55 | | private static readonly TimeSpan EnqueueTimeout = TimeSpan.FromSeconds(1); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | | 58 | | public async Task<(object Response, int StatusCode)> ProcessAsync( |
| | | 59 | | string rawJson, |
| | | 60 | | string? sourceIp, |
| | | 61 | | CancellationToken ct = default) |
| | | 62 | | { |
| | 4 | 63 | | if (string.IsNullOrWhiteSpace(rawJson)) |
| | | 64 | | { |
| | 3 | 65 | | _logger.LogInformation("Получен пустой payload от {Ip}", sourceIp); |
| | 3 | 66 | | return (OzonErrorResponseFactory.ParameterMissed("Тело запроса пустое или отсутствует"), 400); |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | var messageType = _factory.GetMessageType(rawJson); |
| | | 70 | | |
| | 1 | 71 | | if (messageType == OzonMessageType.TYPE_PING) |
| | 0 | 72 | | return HandlePing(); |
| | | 73 | | |
| | | 74 | | // Без бизнес-обработчика запись в БД и постановка в очередь Hangfire бессмысленны |
| | | 75 | | // (запись лишь помечалась бы Skipped) и приводят к исчерпанию пула соединений |
| | | 76 | | // на высокочастотных типах (например TYPE_DELIVERY_DATE_CHANGED). Только ack. |
| | 2 | 77 | | var hasHandler = _handlers.Any(h => h.CanHandle(messageType)); |
| | | 78 | | |
| | 1 | 79 | | if (AckOnlyMessageTypes.Contains(messageType) || !hasHandler) |
| | | 80 | | { |
| | 0 | 81 | | _logger.LogDebug( |
| | 0 | 82 | | "Ozon webhook {MessageType} от {Ip}: подтверждение без записи в БД (обработчик не требуется)", |
| | 0 | 83 | | messageType, sourceIp); |
| | 0 | 84 | | return HandleGeneral(); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | | 89 | | // Enqueue синхронно открывает соединение к БД Hangfire и при перегрузке пула |
| | | 90 | | // может висеть до ConnectionTimeout. Ограничиваем ожидание, чтобы не держать |
| | | 91 | | // HTTP-поток (иначе Ozon уходит в ретраи и нагрузка лавинообразно растёт). |
| | 1 | 92 | | var enqueueTask = Task.Run(() => |
| | 1 | 93 | | HangfireJobClientExtensions.EnqueueOzonWebhook<IOzonWebhookService>( |
| | 1 | 94 | | s => s.PersistWebhookInBackgroundAsync(rawJson, sourceIp)), ct); |
| | | 95 | | |
| | 1 | 96 | | await enqueueTask.WaitAsync(EnqueueTimeout, ct); |
| | | 97 | | |
| | 0 | 98 | | _logger.LogDebug( |
| | 0 | 99 | | "Ozon webhook {MessageType} от {Ip}: принят, сохранение в очереди Hangfire", |
| | 0 | 100 | | messageType, sourceIp); |
| | | 101 | | |
| | 0 | 102 | | return HandleGeneral(); |
| | | 103 | | } |
| | 0 | 104 | | catch (TimeoutException) |
| | | 105 | | { |
| | 0 | 106 | | _logger.LogWarning( |
| | 0 | 107 | | "Ozon webhook {MessageType} от {Ip}: постановка в очередь не завершилась за {Timeout}с (перегрузка х |
| | 0 | 108 | | messageType, sourceIp, EnqueueTimeout.TotalSeconds); |
| | 0 | 109 | | return (OzonErrorResponseFactory.Unknown("Сервис временно перегружен, повторите позже"), 503); |
| | | 110 | | } |
| | 1 | 111 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 112 | | { |
| | | 113 | | // Отмену HTTP-запроса должен обработать контроллер и вернуть 499, а не 500. |
| | 1 | 114 | | throw; |
| | | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 0 | 118 | | _logger.LogError(ex, |
| | 0 | 119 | | "Не удалось поставить в очередь уведомление {MessageType} от {Ip}", |
| | 0 | 120 | | messageType, sourceIp); |
| | 0 | 121 | | return (OzonErrorResponseFactory.Unknown("Не удалось принять уведомление"), 500); |
| | | 122 | | } |
| | 3 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Сохраняет уведомление в БД и при необходимости выполняет обработчик (вне HTTP-запроса). |
| | | 127 | | /// </summary> |
| | | 128 | | [AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 15, 60, 120 })] |
| | | 129 | | [Queue("ozon-webhook")] |
| | | 130 | | public async Task PersistWebhookInBackgroundAsync(string rawJson, string? sourceIp) |
| | | 131 | | { |
| | 0 | 132 | | var messageType = _factory.GetMessageType(rawJson); |
| | 0 | 133 | | var record = new OzonPushNotification |
| | 0 | 134 | | { |
| | 0 | 135 | | MessageType = messageType.ToString(), |
| | 0 | 136 | | RawPayload = rawJson, |
| | 0 | 137 | | SourceIpAddress = sourceIp, |
| | 0 | 138 | | ReceivedAt = DateTime.UtcNow, |
| | 0 | 139 | | ProcessingStatus = nameof(OzonNotificationStatus.Received) |
| | 0 | 140 | | }; |
| | | 141 | | |
| | | 142 | | try |
| | | 143 | | { |
| | 0 | 144 | | var notification = _factory.Parse(rawJson); |
| | 0 | 145 | | EnrichRecord(record, notification); |
| | | 146 | | |
| | 0 | 147 | | var hasHandler = _handlers.Any(h => h.CanHandle(messageType)); |
| | | 148 | | |
| | 0 | 149 | | if (messageType == OzonMessageType.UNKNOWN || !hasHandler) |
| | | 150 | | { |
| | 0 | 151 | | record.ProcessingStatus = nameof(OzonNotificationStatus.Skipped); |
| | 0 | 152 | | record.ProcessedAt = DateTime.UtcNow; |
| | 0 | 153 | | if (!hasHandler && messageType != OzonMessageType.UNKNOWN) |
| | 0 | 154 | | record.ActionDetails = $"Обработчик для типа '{messageType}' не зарегистрирован."; |
| | | 155 | | |
| | 0 | 156 | | await _dataService.AddAsync(record, CancellationToken.None); |
| | 0 | 157 | | return; |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | await _dataService.AddAsync(record, CancellationToken.None); |
| | 0 | 161 | | await ProcessNotificationBackgroundAsync(record.Id); |
| | 0 | 162 | | } |
| | 0 | 163 | | catch (Exception ex) |
| | | 164 | | { |
| | 0 | 165 | | if (IsTransientException(ex)) |
| | | 166 | | { |
| | 0 | 167 | | _logger.LogInformation( |
| | 0 | 168 | | "PersistWebhookInBackgroundAsync: временная ошибка {MessageType} от {Ip}, повтор через Hangfire: |
| | 0 | 169 | | messageType, sourceIp, ex.Message); |
| | | 170 | | } |
| | | 171 | | else |
| | | 172 | | { |
| | 0 | 173 | | _logger.LogError(ex, |
| | 0 | 174 | | "PersistWebhookInBackgroundAsync: ошибка {MessageType} от {Ip}", |
| | 0 | 175 | | messageType, sourceIp); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | record.ProcessingStatus = nameof(OzonNotificationStatus.Error); |
| | 0 | 179 | | record.ErrorMessage = ex.Message.Length > 2000 ? ex.Message[..2000] : ex.Message; |
| | 0 | 180 | | record.ProcessedAt = DateTime.UtcNow; |
| | | 181 | | |
| | | 182 | | try |
| | | 183 | | { |
| | 0 | 184 | | await _dataService.AddAsync(record, CancellationToken.None); |
| | 0 | 185 | | } |
| | 0 | 186 | | catch (Exception saveEx) |
| | | 187 | | { |
| | 0 | 188 | | _logger.LogError(saveEx, |
| | 0 | 189 | | "PersistWebhookInBackgroundAsync: не удалось сохранить запись об ошибке"); |
| | 0 | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | throw; |
| | | 193 | | } |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <inheritdoc/> |
| | | 197 | | [AutomaticRetry(Attempts = 3, OnAttemptsExceeded = AttemptsExceededAction.Fail)] |
| | | 198 | | [Queue("ozon-webhook")] |
| | | 199 | | public async Task ProcessNotificationBackgroundAsync(long notificationId) |
| | | 200 | | { |
| | 0 | 201 | | var record = await _dataService.GetByIdAsync(notificationId); |
| | 0 | 202 | | if (record == null) |
| | | 203 | | { |
| | 0 | 204 | | _logger.LogWarning( |
| | 0 | 205 | | "ProcessNotificationBackgroundAsync: запись Id={Id} не найдена в БД", notificationId); |
| | 0 | 206 | | return; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | try |
| | | 210 | | { |
| | 0 | 211 | | if (!Enum.TryParse<OzonMessageType>(record.MessageType, out var messageType)) |
| | 0 | 212 | | messageType = OzonMessageType.UNKNOWN; |
| | | 213 | | |
| | 0 | 214 | | var handler = _handlers.FirstOrDefault(h => h.CanHandle(messageType)); |
| | 0 | 215 | | if (handler == null) |
| | | 216 | | { |
| | 0 | 217 | | _logger.LogWarning( |
| | 0 | 218 | | "ProcessNotificationBackgroundAsync: обработчик для {MessageType} не найден (Id={Id})", |
| | 0 | 219 | | messageType, notificationId); |
| | 0 | 220 | | record.ProcessingStatus = nameof(OzonNotificationStatus.Skipped); |
| | 0 | 221 | | record.ActionDetails = $"Обработчик для типа '{messageType}' не зарегистрирован."; |
| | 0 | 222 | | record.ProcessedAt = DateTime.UtcNow; |
| | 0 | 223 | | await _dataService.UpdateAsync(record, CancellationToken.None); |
| | 0 | 224 | | return; |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | var notification = _factory.Parse(record.RawPayload); |
| | 0 | 228 | | var handlerResult = await handler.HandleAsync(notification, CancellationToken.None); |
| | 0 | 229 | | var actionDetails = handlerResult.ActionDetails; |
| | | 230 | | |
| | 0 | 231 | | record.ProcessingStatus = nameof(OzonNotificationStatus.Processed); |
| | 0 | 232 | | record.ActionDetails = actionDetails; |
| | 0 | 233 | | record.ProcessedAt = DateTime.UtcNow; |
| | 0 | 234 | | await _dataService.UpdateAsync(record, CancellationToken.None); |
| | | 235 | | |
| | | 236 | | try |
| | | 237 | | { |
| | 0 | 238 | | await Services.SignalRServices.NotificationService.NotifyAsync( |
| | 0 | 239 | | "Ozon", "#D97706", actionDetails, "fa-bell"); |
| | 0 | 240 | | } |
| | 0 | 241 | | catch (Exception notifyEx) |
| | | 242 | | { |
| | 0 | 243 | | _logger.LogWarning(notifyEx, |
| | 0 | 244 | | "ProcessNotificationBackgroundAsync: не удалось отправить уведомление в чат для Id={Id}", |
| | 0 | 245 | | notificationId); |
| | 0 | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | _logger.LogInformation( |
| | 0 | 249 | | "ProcessNotificationBackgroundAsync: Id={Id} обработан. {ActionDetails}", |
| | 0 | 250 | | notificationId, actionDetails); |
| | 0 | 251 | | } |
| | 0 | 252 | | catch (Exception ex) |
| | | 253 | | { |
| | 0 | 254 | | if (IsTransientException(ex)) |
| | | 255 | | { |
| | 0 | 256 | | _logger.LogInformation( |
| | 0 | 257 | | "ProcessNotificationBackgroundAsync: временная ошибка при обработке Id={Id}, повтор через Hangfi |
| | 0 | 258 | | notificationId, ex.Message); |
| | | 259 | | } |
| | | 260 | | else |
| | | 261 | | { |
| | 0 | 262 | | _logger.LogError(ex, |
| | 0 | 263 | | "ProcessNotificationBackgroundAsync: ошибка обработки Id={Id}", notificationId); |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | record.ProcessingStatus = nameof(OzonNotificationStatus.Error); |
| | 0 | 267 | | record.ErrorMessage = ex.Message; |
| | 0 | 268 | | record.ProcessedAt = DateTime.UtcNow; |
| | | 269 | | |
| | 0 | 270 | | try { await _dataService.UpdateAsync(record, CancellationToken.None); } |
| | 0 | 271 | | catch (Exception saveEx) |
| | | 272 | | { |
| | 0 | 273 | | _logger.LogError(saveEx, |
| | 0 | 274 | | "ProcessNotificationBackgroundAsync: не удалось сохранить статус ошибки для Id={Id}", |
| | 0 | 275 | | notificationId); |
| | 0 | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | throw; |
| | | 279 | | } |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | private static (object Response, int StatusCode) HandlePing() => |
| | 0 | 283 | | (new OzonPingSuccessResponse { Time = DateTime.UtcNow }, 200); |
| | | 284 | | |
| | | 285 | | private static (object Response, int StatusCode) HandleGeneral() => |
| | 0 | 286 | | (new OzonGeneralSuccessResponse(), 200); |
| | | 287 | | |
| | | 288 | | private static void EnrichRecord(OzonPushNotification record, IOzonWebhookMessage notification) |
| | | 289 | | { |
| | | 290 | | switch (notification) |
| | | 291 | | { |
| | | 292 | | case OzonNewPostingNotification n: |
| | 0 | 293 | | record.PostingNumber = n.PostingNumber; |
| | 0 | 294 | | record.SellerId = n.SellerId; |
| | 0 | 295 | | record.WarehouseId = n.WarehouseId; |
| | 0 | 296 | | break; |
| | | 297 | | |
| | | 298 | | case OzonPostingCancelledNotification n: |
| | 0 | 299 | | record.PostingNumber = n.PostingNumber; |
| | 0 | 300 | | record.SellerId = n.SellerId; |
| | 0 | 301 | | record.WarehouseId = n.WarehouseId; |
| | 0 | 302 | | break; |
| | | 303 | | |
| | | 304 | | case OzonStateChangedNotification n: |
| | 0 | 305 | | record.PostingNumber = n.PostingNumber; |
| | 0 | 306 | | record.SellerId = n.SellerId; |
| | 0 | 307 | | record.WarehouseId = n.WarehouseId; |
| | 0 | 308 | | break; |
| | | 309 | | |
| | | 310 | | case OzonCutoffDateChangedNotification n: |
| | 0 | 311 | | record.PostingNumber = n.PostingNumber; |
| | 0 | 312 | | record.SellerId = n.SellerId; |
| | 0 | 313 | | record.WarehouseId = n.WarehouseId; |
| | 0 | 314 | | break; |
| | | 315 | | |
| | | 316 | | case OzonDeliveryDateChangedNotification n: |
| | 0 | 317 | | record.PostingNumber = n.PostingNumber; |
| | 0 | 318 | | record.SellerId = n.SellerId; |
| | 0 | 319 | | record.WarehouseId = n.WarehouseId; |
| | 0 | 320 | | break; |
| | | 321 | | |
| | | 322 | | case OzonItemNotification n: |
| | 0 | 323 | | record.SellerId = n.SellerId; |
| | 0 | 324 | | break; |
| | | 325 | | |
| | | 326 | | case OzonStocksChangedNotification n: |
| | 0 | 327 | | record.SellerId = n.SellerId; |
| | 0 | 328 | | break; |
| | | 329 | | |
| | | 330 | | case OzonNewMessageNotification n: |
| | 0 | 331 | | record.SellerId = n.SellerId; |
| | 0 | 332 | | break; |
| | | 333 | | |
| | | 334 | | case OzonUpdateMessageNotification n: |
| | 0 | 335 | | record.SellerId = n.SellerId; |
| | 0 | 336 | | break; |
| | | 337 | | |
| | | 338 | | case OzonMessageReadNotification n: |
| | 0 | 339 | | record.SellerId = n.SellerId; |
| | 0 | 340 | | break; |
| | | 341 | | |
| | | 342 | | case OzonChatClosedNotification n: |
| | 0 | 343 | | record.SellerId = n.SellerId; |
| | | 344 | | break; |
| | | 345 | | } |
| | 0 | 346 | | } |
| | | 347 | | |
| | | 348 | | private static bool IsTransientException(Exception ex) |
| | | 349 | | { |
| | 0 | 350 | | if (ex is OperationCanceledException or TimeoutException or HttpRequestException or SocketException) |
| | 0 | 351 | | return true; |
| | | 352 | | |
| | 0 | 353 | | if (ex is InvalidOperationException && ex.Message.Contains("Временная ошибка", StringComparison.OrdinalIgnor |
| | 0 | 354 | | return true; |
| | | 355 | | |
| | 0 | 356 | | if (ex is InvalidOperationException && ex.Message.Contains("повтор", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 357 | | return true; |
| | | 358 | | |
| | 0 | 359 | | if (ex.InnerException != null && IsTransientException(ex.InnerException)) |
| | 0 | 360 | | return true; |
| | | 361 | | |
| | 0 | 362 | | if (ex is AggregateException agg) |
| | 0 | 363 | | return agg.InnerExceptions.Any(IsTransientException); |
| | | 364 | | |
| | 0 | 365 | | return false; |
| | | 366 | | } |
| | | 367 | | } |
| | | 368 | | } |