Example #1
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();
        }
Example #2
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();
 }
Example #3
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();
            }
        }