Exemplo n.º 1
0
        public void PurgeQueue(IReportRequestEntryService reportRequestService)
        {
            var entriesToDelete = reportRequestService.GetAll().Where(rre => rre.AmazonRegion == _region && rre.MerchantId == _merchantId);

            reportRequestService.DeleteRange(entriesToDelete);
            reportRequestService.SaveChanges();
        }
Exemplo n.º 2
0
        public void CleanupReportRequests(IReportRequestEntryService reportRequestService)
        {
            _logger.Debug("Executing cleanup of report requests queue.");
            var allEntriesForRegionAndMerchant = reportRequestService.GetAll().Where(rrc => IsMatchForRegionAndMerchantId(rrc));
            var entriesToDelete = new List <EntryToDelete>();

            void DeleteUniqueEntries(IEnumerable <EntryToDelete> entries)
            {
                foreach (var entryToDelete in entries)
                {
                    if (!reportRequestService.GetAll().Any(rrc => rrc.Id == entryToDelete.Entry.Id))
                    {
                        continue;
                    }
                    reportRequestService.Delete(entryToDelete.Entry);
                    reportRequestService.SaveChanges();

                    _logger.Warn($"Report request entry {entryToDelete.Entry.EntryIdentityDescription} deleted from queue. {entryToDelete.ReportRequestEntryDeleteReason.ToString()} exceeded");
                }
            }

            entriesToDelete.AddRange(allEntriesForRegionAndMerchant.Where(IsRequestRetryCountExceeded)
                                     .Select(e => new EntryToDelete {
                Entry = e, ReportRequestEntryDeleteReason = ReportRequestFailureReasonType.ReportRequestMaxRetryCountExceeded
            }));
            entriesToDelete.AddRange(allEntriesForRegionAndMerchant.Where(IsDownloadRetryCountExceeded)
                                     .Select(e => new EntryToDelete {
                Entry = e, ReportRequestEntryDeleteReason = ReportRequestFailureReasonType.ReportDownloadMaxRetryCountExceeded
            }));
            entriesToDelete.AddRange(allEntriesForRegionAndMerchant.Where(IsProcessingRetryCountExceeded)
                                     .Select(e => new EntryToDelete {
                Entry = e, ReportRequestEntryDeleteReason = ReportRequestFailureReasonType.ReportProcessingMaxRetryCountExceeded
            }));
            entriesToDelete.AddRange(allEntriesForRegionAndMerchant.Where(IsExpirationPeriodExceeded)
                                     .Select(e => new EntryToDelete {
                Entry = e, ReportRequestEntryDeleteReason = ReportRequestFailureReasonType.ReportRequestEntryExpirationPeriodExceeded
            }));
            entriesToDelete.AddRange(allEntriesForRegionAndMerchant.Where(IsCallbackInvocationRetryCountExceeded)
                                     .Select(e => new EntryToDelete {
                Entry = e, ReportRequestEntryDeleteReason = ReportRequestFailureReasonType.InvokeCallbackMaxRetryCountExceeded
            }));

            foreach (var entryToDelete in entriesToDelete)
            {
                OnReportEntryWasMarkedForDelete(new ReportRequestFailedEventArgs(
                                                    entryToDelete.ReportRequestEntryDeleteReason,
                                                    entryToDelete.Entry.AmazonRegion,
                                                    entryToDelete.Entry.LastAmazonRequestDate,
                                                    entryToDelete.Entry.LastAmazonReportProcessingStatus,
                                                    entryToDelete.Entry.RequestReportId,
                                                    entryToDelete.Entry.GeneratedReportId,
                                                    entryToDelete.Entry.GetPropertiesContainer(),
                                                    entryToDelete.Entry.TargetHandlerId,
                                                    entryToDelete.Entry.TargetHandlerArgs,
                                                    entryToDelete.Entry.ReportType));
            }

            DeleteUniqueEntries(entriesToDelete.Distinct());
        }
Exemplo n.º 3
0
        public void DownloadNextReportInQueueFromAmazon(IReportRequestEntryService reportRequestService)
        {
            var reportToDownload = reportRequestService.GetNextFromQueueOfReportsToDownload(_merchantId, _region);

            if (reportToDownload == null)
            {
                return;
            }

            _requestReportProcessor.DownloadGeneratedReportFromAmazon(reportRequestService, reportToDownload);
        }
Exemplo n.º 4
0
        public void RequestNextReportInQueueFromAmazon(IReportRequestEntryService reportRequestService)
        {
            var reportRequest = reportRequestService.GetNextFromQueueOfReportsToRequest(_merchantId, _region);

            if (reportRequest == null)
            {
                return;
            }

            _requestReportProcessor.RequestReportFromAmazon(reportRequestService, reportRequest);
        }
Exemplo n.º 5
0
        public List <(string ReportRequestId, string GeneratedReportId, string ReportProcessingStatus)> GetReportProcessingStatusesFromAmazon(
            IReportRequestEntryService reportRequestService, IEnumerable <string> requestIdList, string merchant)
        {
            _logger.Debug($"Attempting to request report processing statuses for all reports in queue.");

            var request = new GetReportRequestListRequest()
            {
                ReportRequestIdList = new IdList(), Merchant = merchant
            };

            request.ReportRequestIdList.Id.AddRange(requestIdList);

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

            try
            {
                var response  = _marketplaceWebServiceClient.GetReportRequestList(request);
                var requestId = response?.ResponseHeaderMetadata?.RequestId ?? "unknown";
                var timestamp = response?.ResponseHeaderMetadata?.Timestamp ?? "unknown";
                _logger.Debug($"Request to MWS.GetReportRequestList was successful!", new RequestInfo(timestamp, requestId));

                var responseInformation =
                    new List <(string ReportRequestId, string GeneratedReportId, string ReportProcessingStatus)>();

                if (response?.GetReportRequestListResult?.ReportRequestInfo != null)
                {
                    foreach (var reportRequestInfo in response.GetReportRequestListResult.ReportRequestInfo)
                    {
                        responseInformation.Add((reportRequestInfo.ReportRequestId, reportRequestInfo.GeneratedReportId, reportRequestInfo.ReportProcessingStatus));
                    }
                }
                else
                {
                    _logger.Warn("AmazonMWS GetReportRequestList response does not contain any results. The operation will be executed again at the next poll request.");
                }

                return(responseInformation);
            }
            catch (MarketplaceWebServiceException e)
            {
                _logger.Warn($"AmazonMWS GetReportRequestList failed! The operation will be executed again at the next poll request.", e);
                return(null);
            }
            catch (Exception e)
            {
                _logger.Warn($"AmazonMWS GetReportRequestList failed! The operation will be executed again at the next poll request.", e);
                return(null);
            }
        }
Exemplo n.º 6
0
 public void UnlockReportRequestEntries(IReportRequestEntryService reportRequestService, IEnumerable <string> reportRequestIds)
 {
     foreach (var reportRequestId in reportRequestIds)
     {
         var reportRequestEntry = reportRequestService.FirstOrDefault(fsc => fsc.RequestReportId == reportRequestId);
         if (reportRequestEntry != null)
         {
             reportRequestService.Unlock(reportRequestEntry, "Unlocking multiple report request entries - amazon processing status update has been completed.");
             reportRequestService.Update(reportRequestEntry);
         }
     }
     reportRequestService.SaveChanges();
 }
Exemplo n.º 7
0
        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();
        }
Exemplo n.º 8
0
        public void PollReports(IReportRequestEntryService reportRequestService)
        {
            _logger.Debug("Executing polling action for report requests.");

            _requestReportProcessor.CleanupReportRequests(reportRequestService);

            RequestNextReportInQueueFromAmazon(reportRequestService);

            RequestReportStatusesFromAmazon(reportRequestService);

            DownloadNextReportInQueueFromAmazon(reportRequestService);

            PublishEventsForPreviouslyDownloadedReports(reportRequestService);
        }
Exemplo n.º 9
0
        public void RequestReportStatusesFromAmazon(IReportRequestEntryService reportRequestService)
        {
            var pendingReportsRequestIds = reportRequestService.GetAllPendingReportFromQueue(_merchantId, _region).ToList();

            if (!pendingReportsRequestIds.Any())
            {
                return;
            }

            var reportRequestStatuses =
                _requestReportProcessor.GetReportProcessingStatusesFromAmazon(reportRequestService, pendingReportsRequestIds, _merchantId);

            if (reportRequestStatuses != null)
            {
                _requestReportProcessor.QueueReportsAccordingToProcessingStatus(reportRequestService, reportRequestStatuses);
            }

            _requestReportProcessor.UnlockReportRequestEntries(reportRequestService, pendingReportsRequestIds);
        }
Exemplo n.º 10
0
        public void QueueReport(IReportRequestEntryService reportRequestService, ReportRequestPropertiesContainer propertiesContainer, string targetEventId, Dictionary <string, object> targetEventArgs)
        {
            try
            {
                if (propertiesContainer == null)
                {
                    throw new ArgumentNullException();
                }

                var serializedPropertiesContainer = JsonConvert.SerializeObject(propertiesContainer);

                var reportRequest = new ReportRequestEntry(serializedPropertiesContainer)
                {
                    AmazonRegion             = _region,
                    MerchantId               = _merchantId,
                    LastAmazonRequestDate    = DateTime.MinValue,
                    DateCreated              = DateTime.UtcNow,
                    ContentUpdateFrequency   = propertiesContainer.UpdateFrequency,
                    RequestReportId          = null,
                    GeneratedReportId        = null,
                    ReportRequestRetryCount  = 0,
                    ReportDownloadRetryCount = 0,
                    ReportProcessRetryCount  = 0,
                    InvokeCallbackRetryCount = 0,
                    ReportType               = propertiesContainer.ReportType,
                    TargetHandlerId          = targetEventId,
                    TargetHandlerArgs        = targetEventArgs == null ? null : JsonConvert.SerializeObject(targetEventArgs),
                    InstanceId               = _options?.EventPublishingOptions?.RestrictInvocationToOriginatingInstance?.HashedInstanceId,
                };

                reportRequestService.Unlock(reportRequest, "Unlocking single report request entry - newly created, ready for processing.");
                reportRequestService.Create(reportRequest);
                reportRequestService.SaveChanges();

                _logger.Info($"The following report was queued for download from Amazon {reportRequest.EntryIdentityDescription}.");
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, e);
            }
        }
Exemplo n.º 11
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();
            }
        }
Exemplo n.º 12
0
 public void QueueReportsAccordingToProcessingStatus(IReportRequestEntryService reportRequestService,
                                                     List <(string ReportRequestId, string GeneratedReportId, string ReportProcessingStatus)> reportGenerationStatuses)