Exemple #1
0
        public void should_Generate_Diff_Art_Extracts_Initial()
        {
            var extracts =
                _packager.GenerateDiffBatchExtracts <PatientArtExtractView>(1, 50, "NDWH", nameof(PatientArtExtract));

            Assert.True(extracts.Any());
            foreach (var patientArtExtractView in extracts)
            {
                Log.Debug($"{patientArtExtractView.SiteCode}|{patientArtExtractView.PatientID}|{patientArtExtractView.Date_Created:yyyy MMMM dd}|{patientArtExtractView.Date_Last_Modified:yyyy MMMM dd}");
            }
        }
Exemple #2
0
        public async Task <List <SendCTResponse> > SendDiffBatchExtractsAsync <T>(SendManifestPackageDTO sendTo, int batchSize, IMessageBag <T> messageBag) where T : ClientExtract
        {
            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };
            var client = Client ?? new HttpClient(handler);

            var responses   = new List <SendCTResponse>();
            var packageInfo = _packager.GetPackageInfo <T>(batchSize);
            int sendCound   = 0;
            int count       = 0;
            int total       = packageInfo.PageCount;
            int overall     = 0;

            DomainEvents.Dispatch(new CTStatusNotification(sendTo.ExtractId, sendTo.GetExtractId(messageBag.ExtractName), ExtractStatus.Sending));
            long recordCount = 0;

            try
            {
                for (int page = 1; page <= packageInfo.PageCount; page++)
                {
                    count++;
                    var extracts = _packager.GenerateDiffBatchExtracts <T>(page, packageInfo.PageSize, messageBag.Docket,
                                                                           messageBag.DocketExtract).ToList();
                    recordCount = recordCount + extracts.Count;

                    if (!extracts.Any())
                    {
                        count       = total;
                        recordCount = packageInfo.TotalRecords;
                        DomainEvents.Dispatch(new CTSendNotification(new SendProgress(messageBag.ExtractName, messageBag.GetProgress(count, total), recordCount)));
                        break;
                    }

                    Log.Debug(
                        $">>>> Sending {messageBag.ExtractName} {recordCount}/{packageInfo.TotalRecords} Page:{page} of {packageInfo.PageCount}");
                    messageBag = messageBag.Generate(extracts);
                    var message = messageBag.Messages;
                    try
                    {
                        int  retryCount = 0;
                        bool allowSend  = true;
                        while (allowSend)
                        {
                            var response = await client.PostAsJsonAsync(
                                sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}v2/{messageBag.EndPoint}"), message);

                            if (response.IsSuccessStatusCode)
                            {
                                allowSend = false;
                                // var content = await response.Content.ReadAsJsonAsync<SendCTResponse>();
                                responses.Add(new SendCTResponse());

                                var sentIds = messageBag.SendIds;
                                sendCound += sentIds.Count;
                                DomainEvents.Dispatch(new CTExtractSentEvent(sentIds, SendStatus.Sent,
                                                                             messageBag.ExtractType));
                            }
                            else
                            {
                                retryCount++;
                                if (retryCount == 4)
                                {
                                    var sentIds = messageBag.SendIds;
                                    var error   = await response.Content.ReadAsStringAsync();

                                    DomainEvents.Dispatch(new CTExtractSentEvent(
                                                              sentIds, SendStatus.Failed, messageBag.ExtractType,
                                                              error));
                                    throw new Exception(error);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Extracts{messageBag.ExtractName} Error");
                        throw;
                    }

                    DomainEvents.Dispatch(new CTSendNotification(new SendProgress(messageBag.ExtractName, messageBag.GetProgress(count, total), recordCount)));
                }

                await _mediator.Publish(new DocketExtractSent(messageBag.Docket, messageBag.DocketExtract));
            }
            catch (Exception e)
            {
                Log.Error(e, $"Send Extracts {messageBag.ExtractName} Error");
                throw;
            }

            DomainEvents.Dispatch(new CTSendNotification(new SendProgress(messageBag.ExtractName,
                                                                          messageBag.GetProgress(count, total), recordCount, true)));

            DomainEvents.Dispatch(new CTStatusNotification(sendTo.ExtractId, sendTo.GetExtractId(messageBag.ExtractName), ExtractStatus.Sent, sendCound)
            {
                UpdatePatient = (messageBag is ArtMessageBag || messageBag is BaselineMessageBag || messageBag is StatusMessageBag)
            }
                                  );

            return(responses);
        }