Пример #1
0
        public void PurgeQueue(IReportRequestEntryService reportRequestService)
        {
            var entriesToDelete = reportRequestService.GetAll().Where(rre => rre.AmazonRegion == _region && rre.MerchantId == _merchantId);

            reportRequestService.DeleteRange(entriesToDelete);
            reportRequestService.SaveChanges();
        }
Пример #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());
        }
Пример #3
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();
 }
Пример #4
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();
        }
Пример #5
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);
            }
        }
Пример #6
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();
            }
        }