Example #1
0
        public void PurgeQueue(IFeedSubmissionEntryService feedSubmissionService)
        {
            var entriesToDelete = feedSubmissionService.GetAll().Where(rre => rre.AmazonRegion == _region && rre.MerchantId == _merchantId);

            feedSubmissionService.DeleteRange(entriesToDelete);
            feedSubmissionService.SaveChanges();
        }
 public void UnlockFeedSubmissionEntries(IFeedSubmissionEntryService feedSubmissionService, IEnumerable <string> feedSubmissionIds)
 {
     foreach (var submissionId in feedSubmissionIds)
     {
         var feedSubmissionEntry = feedSubmissionService.FirstOrDefault(fsc => fsc.FeedSubmissionId == submissionId);
         if (feedSubmissionEntry != null)
         {
             feedSubmissionService.Unlock(feedSubmissionEntry, "Unlocking multiple feed submission entries - amazon processing status update has been completed.");
             feedSubmissionService.Update(feedSubmissionEntry);
         }
     }
     feedSubmissionService.SaveChanges();
 }
Example #3
0
        public void QueueFeed(IFeedSubmissionEntryService feedSubmissionService, FeedSubmissionPropertiesContainer propertiesContainer, string targetEventId = null, Dictionary <string, object> targetEventArgs = null)
        {
            try
            {
                if (propertiesContainer == null)
                {
                    throw new ArgumentNullException();
                }

                var serializedPropertiesContainer = JsonConvert.SerializeObject(propertiesContainer);

                var feedSubmission = new FeedSubmissionEntry(serializedPropertiesContainer)
                {
                    AmazonRegion             = _region,
                    MerchantId               = _merchantId,
                    LastSubmitted            = DateTime.MinValue,
                    DateCreated              = DateTime.UtcNow,
                    IsProcessingComplete     = false,
                    HasErrors                = false,
                    SubmissionErrorData      = null,
                    FeedSubmissionRetryCount = 0,
                    FeedSubmissionId         = null,
                    FeedType          = propertiesContainer.FeedType,
                    TargetHandlerId   = targetEventId,
                    TargetHandlerArgs = targetEventArgs == null ? null : JsonConvert.SerializeObject(targetEventArgs),
                    InstanceId        = _options?.EventPublishingOptions?.RestrictInvocationToOriginatingInstance?.HashedInstanceId,
                    Details           = new FeedSubmissionDetails
                    {
                        FeedContent = ZipHelper.CreateArchiveFromContent(propertiesContainer.FeedContent)
                    }
                };

                feedSubmissionService.Unlock(feedSubmission, "Unlocking single feed submission entry - newly created, ready for processing.");
                feedSubmissionService.Create(feedSubmission);
                feedSubmissionService.SaveChanges();

                _logger.Debug($"The following feed was queued for submission to Amazon {feedSubmission.EntryIdentityDescription}.");
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, e);
            }
        }
Example #4
0
        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();
                }
            }
        }