コード例 #1
0
ファイル: ReportProcessor.cs プロジェクト: jdease/EasyMWS
        private void PublishEventsForPreviouslyDownloadedReports(IReportRequestEntryService reportRequestService)
        {
            var reportsReadyForCallback = reportRequestService.GetAllFromQueueOfReportsReadyForCallback(_merchantId, _region);

            foreach (var reportEntry in reportsReadyForCallback)
            {
                try
                {
                    var reportType  = reportEntry.ReportType;
                    var handledId   = reportEntry.TargetHandlerId;
                    var handlerArgs = (reportEntry.TargetHandlerArgs == null) ? null : new ReadOnlyDictionary <string, object>(JsonConvert.DeserializeObject <Dictionary <string, object> >(reportEntry.TargetHandlerArgs));

                    if (reportEntry.Details == null && reportEntry.LastAmazonReportProcessingStatus == AmazonReportProcessingStatus.DoneNoData && !_options.EventPublishingOptions.EventPublishingForReportStatusDoneNoData)
                    {
                        _logger.Debug($"An attempt will not be made to publish event ReportDownloaded for the following report in queue : {reportEntry.EntryIdentityDescription}, because AmazonProcessingStatus for this report is _DONE_NO_DATA_ but EventPublishingForReportStatusDoneNoData EasyMwsOption is FALSE.");
                    }
                    else if (reportEntry.Details == null && reportEntry.LastAmazonReportProcessingStatus == AmazonReportProcessingStatus.DoneNoData && _options.EventPublishingOptions.EventPublishingForReportStatusDoneNoData)
                    {
                        _logger.Warn($"Attempting to publish event ReportDownloaded for the following report in queue : {reportEntry.EntryIdentityDescription}, but the AmazonProcessingStatus for this report is _DONE_NO_DATA_ therefore the Stream argument will be null at invocation time.");
                        var eventArgs = new ReportDownloadedEventArgs(null, reportType, handledId, handlerArgs);
                        OnReportDownloaded(eventArgs);
                    }
                    else
                    {
                        _logger.Debug($"Attempting to publish event ReportDownloaded for the next downloaded report in queue : {reportEntry.EntryIdentityDescription}.");
                        var reportContent = ZipHelper.ExtractArchivedSingleFileToStream(reportEntry.Details?.ReportContent);
                        var eventArgs     = new ReportDownloadedEventArgs(reportContent, reportType, handledId, handlerArgs);
                        OnReportDownloaded(eventArgs);
                    }

                    reportRequestService.Delete(reportEntry);
                    _logger.Info($"Event publishing has succeeded for {reportEntry.EntryIdentityDescription}.");
                }
                catch (SqlException e)
                {
                    _logger.Error($"ReportDownloaded event publishing failed for {reportEntry.EntryIdentityDescription} due to an internal error '{e.Message}'. The event publishing will be retried at the next poll request", e);
                    reportRequestService.Unlock(reportEntry, "Unlocking single report request entry - an SQL exception occurred while trying to invoke callback.");
                    reportRequestService.Update(reportEntry);
                }
                catch (Exception e)
                {
                    _logger.Error($"ReportDownloaded event publishing failed for {reportEntry.EntryIdentityDescription}. Current retry count is :{reportEntry.InvokeCallbackRetryCount}. {e.Message}", e);
                    reportEntry.InvokeCallbackRetryCount++;
                    reportRequestService.Unlock(reportEntry, "Unlocking single report request entry - an exception occurred while trying to invoke callback.");
                    reportRequestService.Update(reportEntry);
                }
            }

            reportRequestService.SaveChanges();
        }
コード例 #2
0
        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();
                }
            }
        }
コード例 #3
0
ファイル: EasyMwsLoggerTests.cs プロジェクト: jdease/EasyMWS
 public void ActionThatLogsWarningWithRequestInfo()
 {
     _logger.Warn("testMessage_ActionThatLogsWarningWithRequestInfo", new RequestInfo("testTimestamp2", "testRequestId2", HttpStatusCode.Continue));
 }
コード例 #4
0
        public void RequestReportFromAmazon(IReportRequestEntryService reportRequestService, ReportRequestEntry reportRequestEntry)
        {
            var missingInformationExceptionMessage = "Cannot request report from amazon due to missing report request information";

            if (reportRequestEntry?.ReportRequestData == null)
            {
                throw new ArgumentNullException($"{missingInformationExceptionMessage}: Report request data is missing");
            }
            if (string.IsNullOrEmpty(reportRequestEntry?.ReportType))
            {
                throw new ArgumentException($"{missingInformationExceptionMessage}: Report Type is missing");
            }

            var reportRequestData = reportRequestEntry.GetPropertiesContainer();

            _logger.Debug($"Attempting to request the next report in queue from Amazon: {reportRequestEntry.EntryIdentityDescription}.");

            var reportRequest = new RequestReportRequest
            {
                Merchant   = reportRequestEntry.MerchantId,
                ReportType = reportRequestEntry.ReportType,
            };

            if (!string.IsNullOrEmpty(_mWSAuthToken))
            {
                reportRequest.MWSAuthToken = _mWSAuthToken;
            }

            if (reportRequestData.MarketplaceIdList != null)
            {
                reportRequest.MarketplaceIdList = new IdList {
                    Id = reportRequestData.MarketplaceIdList.ToList()
                }
            }
            ;
            if (reportRequestData.StartDate.HasValue)
            {
                reportRequest.StartDate = reportRequestData.StartDate.Value;
            }
            if (reportRequestData.EndDate.HasValue)
            {
                reportRequest.EndDate = reportRequestData.EndDate.Value;
            }
            if (!string.IsNullOrEmpty(reportRequestData.ReportOptions))
            {
                reportRequest.ReportOptions = reportRequestData.ReportOptions;
            }

            try
            {
                reportRequestService.Unlock(reportRequestEntry, "Unlocking single report request entry - attempt to request report from amazon has been completed.");
                reportRequestService.Update(reportRequestEntry);

                var reportResponse = _marketplaceWebServiceClient.RequestReport(reportRequest);
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                reportRequestEntry.RequestReportId       = reportResponse?.RequestReportResult?.ReportRequestInfo?.ReportRequestId;

                var requestId = reportResponse?.ResponseHeaderMetadata?.RequestId ?? "unknown";
                var timestamp = reportResponse?.ResponseHeaderMetadata?.Timestamp ?? "unknown";

                if (string.IsNullOrEmpty(reportRequestEntry.RequestReportId))
                {
                    reportRequestEntry.ReportRequestRetryCount++;
                    _logger.Warn($"RequestReport did not generate a ReportRequestId for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.",
                                 new RequestInfo(timestamp, requestId));
                }
                else
                {
                    reportRequestEntry.ReportRequestRetryCount = 0;
                    _logger.Info($"AmazonMWS RequestReport succeeded for {reportRequestEntry.EntryIdentityDescription}. ReportRequestId:'{reportRequestEntry.RequestReportId}'.",
                                 new RequestInfo(timestamp, requestId));
                }
            }
            catch (MarketplaceWebServiceException e) when(e.StatusCode == HttpStatusCode.BadRequest && IsAmazonErrorCodeFatal(e.ErrorCode))
            {
                reportRequestService.Delete(reportRequestEntry);
                _logger.Error($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. The entry will now be removed from queue", e);
            }
            catch (MarketplaceWebServiceException e) when(IsAmazonErrorCodeNonFatal(e.ErrorCode))
            {
                reportRequestEntry.ReportRequestRetryCount++;
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                _logger.Warn($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.", e);
            }
            catch (Exception e)
            {
                reportRequestEntry.ReportRequestRetryCount++;
                reportRequestEntry.LastAmazonRequestDate = DateTime.UtcNow;
                _logger.Warn($"AmazonMWS RequestReport failed for {reportRequestEntry.EntryIdentityDescription}. Report request will be retried. ReportRequestRetryCount is now : {reportRequestEntry.ReportRequestRetryCount}.", e);
            }
            finally
            {
                reportRequestService.SaveChanges();
            }
        }