private void PublishEventsForPreviouslySubmittedFeeds(IFeedSubmissionEntryService feedSubmissionService) { var previouslySubmittedFeeds = feedSubmissionService.GetAllFromQueueOfFeedsReadyForCallback(_merchantId, _region); foreach (var feedSubmissionEntry in previouslySubmittedFeeds) { try { var processingReportContent = ZipHelper.ExtractArchivedSingleFileToStream(feedSubmissionEntry.Details.FeedSubmissionReport); var feedType = feedSubmissionEntry.FeedType; var handlerId = feedSubmissionEntry.TargetHandlerId; var handledArgs = feedSubmissionEntry.TargetHandlerArgs == null ? null : new ReadOnlyDictionary <string, object>(JsonConvert.DeserializeObject <Dictionary <string, object> >(feedSubmissionEntry.TargetHandlerArgs)); var eventArgs = new FeedUploadedEventArgs(processingReportContent, feedType, handlerId, handledArgs); _logger.Debug($"Attempting publish FeedUploaded for the next submitted feed in queue : {feedSubmissionEntry.EntryIdentityDescription}"); OnFeedUploaded(eventArgs); feedSubmissionService.Delete(feedSubmissionEntry); _logger.Info($"Event publishing has succeeded for {feedSubmissionEntry.EntryIdentityDescription}."); } catch (SqlException e) { _logger.Error($"Event publishing failed for {feedSubmissionEntry.EntryIdentityDescription} due to an internal error '{e.Message}'. The event publishing will be retried at the next poll request", e); feedSubmissionService.Unlock(feedSubmissionEntry, "Unlocking single feed submission entry - an SQL exception occurred while trying to invoke callback."); feedSubmissionService.Update(feedSubmissionEntry); } catch (Exception e) { _logger.Error($"Event publishing failed for {feedSubmissionEntry.EntryIdentityDescription}. Current retry count is :{feedSubmissionEntry.FeedSubmissionRetryCount}. {e.Message}", e); feedSubmissionEntry.InvokeCallbackRetryCount++; feedSubmissionService.Unlock(feedSubmissionEntry, "Unlocking single feed submission entry - an exception occurred while trying to invoke callback."); feedSubmissionService.Update(feedSubmissionEntry); } } feedSubmissionService.SaveChanges(); }
public void SubmitFeedToAmazon(IFeedSubmissionEntryService feedSubmissionService, FeedSubmissionEntry feedSubmission) { void HandleSubmitFeedSuccess(SubmitFeedResponse response) { var requestId = response?.ResponseHeaderMetadata?.RequestId ?? "unknown"; var timestamp = response?.ResponseHeaderMetadata?.Timestamp ?? "unknown"; feedSubmission.FeedSubmissionId = response?.SubmitFeedResult?.FeedSubmissionInfo?.FeedSubmissionId; feedSubmission.FeedSubmissionRetryCount = 0; _logger.Info($"AmazonMWS feed submission has succeeded for {feedSubmission.EntryIdentityDescription}. FeedSubmissionId:'{feedSubmission.FeedSubmissionId}'.", new RequestInfo(timestamp, requestId)); } void HandleMissingFeedSubmissionId() { feedSubmission.FeedSubmissionRetryCount++; _logger.Warn($"SubmitFeed did not generate a FeedSubmissionId for {feedSubmission.EntryIdentityDescription}. Feed submission will be retried. FeedSubmissionRetryCount is now : {feedSubmission.FeedSubmissionRetryCount}."); } void HandleNonFatalOrGenericException(Exception e) { feedSubmission.FeedSubmissionRetryCount++; feedSubmission.LastSubmitted = DateTime.UtcNow; _logger.Warn($"AmazonMWS SubmitFeed failed for {feedSubmission.EntryIdentityDescription}. Feed submission will be retried. FeedSubmissionRetryCount is now : {feedSubmission.FeedSubmissionRetryCount}.", e); } void HandleFatalException(Exception e) { feedSubmissionService.Delete(feedSubmission); _logger.Error($"AmazonMWS SubmitFeed failed for {feedSubmission.EntryIdentityDescription}. The entry will now be removed from queue", e); } var missingInformationExceptionMessage = "Cannot submit queued feed to amazon due to missing feed submission information"; if (feedSubmission == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed submission entry is null"); } if (feedSubmission.FeedSubmissionData == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed submission data is null"); } var feedContentZip = feedSubmission.Details?.FeedContent; if (feedContentZip == null) { throw new ArgumentNullException($"{missingInformationExceptionMessage}: Feed content is missing"); } if (string.IsNullOrEmpty(feedSubmission?.FeedType)) { throw new ArgumentException($"{missingInformationExceptionMessage}: Feed type is missing"); } _logger.Debug($"Attempting to submit the next feed in queue to Amazon: {feedSubmission.EntryIdentityDescription}."); var feedSubmissionData = feedSubmission.GetPropertiesContainer(); using (var stream = ZipHelper.ExtractArchivedSingleFileToStream(feedContentZip)) { var submitFeedRequest = new SubmitFeedRequest { Merchant = feedSubmission.MerchantId, FeedType = feedSubmission.FeedType, FeedContent = stream, MarketplaceIdList = feedSubmissionData.MarketplaceIdList == null ? null : new IdList { Id = feedSubmissionData.MarketplaceIdList }, PurgeAndReplace = feedSubmissionData.PurgeAndReplace ?? false, ContentMD5 = MD5ChecksumHelper.ComputeHashForAmazon(stream), }; if (!string.IsNullOrEmpty(_mWSAuthToken)) { submitFeedRequest.MWSAuthToken = _mWSAuthToken; } try { feedSubmissionService.Unlock(feedSubmission, "Unlocking single feed submission entry - finished attempt to submit feed to amazon."); feedSubmissionService.Update(feedSubmission); var response = _marketplaceWebServiceClient.SubmitFeed(submitFeedRequest); feedSubmission.LastSubmitted = DateTime.UtcNow; if (string.IsNullOrEmpty(response?.SubmitFeedResult?.FeedSubmissionInfo?.FeedSubmissionId)) { HandleMissingFeedSubmissionId(); } else { HandleSubmitFeedSuccess(response); } } catch (MarketplaceWebServiceException e) when(e.StatusCode == HttpStatusCode.BadRequest && IsAmazonErrorCodeFatal(e.ErrorCode)) { HandleFatalException(e); } catch (MarketplaceWebServiceException e) when(IsAmazonErrorCodeNonFatal(e.ErrorCode)) { HandleNonFatalOrGenericException(e); } catch (Exception e) { HandleNonFatalOrGenericException(e); } finally { stream.Dispose(); feedSubmissionService.SaveChanges(); } } }