Пример #1
0
        public async Task <List <string> > SendExtractsAsync(SendManifestPackageDTO sendTo)
        {
            //hack to prevent hang fire from requeueing the job every 30 minutes
            if (_patientExtractStatus.LastStatus != nameof(ExtractStatus.Sending))
            {
                var responses             = new List <Task <string> >();
                var output                = new List <string>();
                HttpClientHandler handler = new HttpClientHandler()
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                    client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.Timeout = new TimeSpan(0, 1, 0);
                    var ids = _reader.ReadAllIds().ToList();
                    _total = ids.Count;
                    var sentPatients             = new List <Guid>();
                    var sentPatientArts          = new List <Guid>();
                    var sentPatientBaselines     = new List <Guid>();
                    var sentPatientLabs          = new List <Guid>();
                    var sentPatientPharmacies    = new List <Guid>();
                    var sentPatientStatuses      = new List <Guid>();
                    var sentPatientVisits        = new List <Guid>();
                    var sentPatientAdverseEvents = new List <Guid>();

                    //update status to sending
                    UpdateUiNumbers(ExtractStatus.Sending);
                    //Show error message in UI
                    DomainEvents.Dispatch(new DwhMessageNotification(false, $"Sending started..."));

                    var httpResponseMessages = new List <Task <HttpResponseMessage> >();

                    foreach (var id in ids)
                    {
                        _count++;
                        var patient                 = _packager.GenerateExtracts(id);
                        var artMessageBag           = ArtMessageBag.Create(patient);
                        var artMessage              = artMessageBag.Message;
                        var baselineMessageBag      = BaselineMessageBag.Create(patient);
                        var baselineMessage         = baselineMessageBag.Message;
                        var labMessageBag           = LabMessageBag.Create(patient);
                        var labMessage              = labMessageBag.Message;
                        var pharmacyMessageBag      = PharmacyMessageBag.Create(patient);
                        var pharmacyMessage         = pharmacyMessageBag.Message;
                        var statusMessageBag        = StatusMessageBag.Create(patient);
                        var statusMessage           = statusMessageBag.Message;
                        var visitsMessageBag        = VisitsMessageBag.Create(patient);
                        var visitsMessage           = visitsMessageBag.Message;
                        var adverseEventsMessageBag = AdverseEventsMessageBag.Create(patient);
                        var adverseEventsMessage    = adverseEventsMessageBag.Message;

                        if (artMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{artMessageBag.EndPoint}"), artMessage);
                                httpResponseMessages.Add(response);
                                _artCount += artMessage.ArtExtracts.Count;
                                sentPatientArts.AddRange(artMessage.ArtExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientArt.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, artMessage.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(artMessage);
                                throw;
                            }
                        }

                        if (baselineMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{baselineMessageBag.EndPoint}"), baselineMessage);
                                httpResponseMessages.Add(response);
                                _baselineCount += baselineMessage.BaselinesExtracts.Count;
                                sentPatientBaselines.AddRange(baselineMessage.BaselinesExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientBaseline.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, baselineMessage.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(baselineMessage);
                                throw;
                            }
                        }

                        if (labMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{labMessageBag.EndPoint}"), labMessage);
                                httpResponseMessages.Add(response);
                                _labCount += labMessage.LaboratoryExtracts.Count;
                                sentPatientLabs.AddRange(labMessage.LaboratoryExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientLab.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, labMessage.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(labMessage);
                                throw;
                            }
                        }

                        if (pharmacyMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{pharmacyMessageBag.EndPoint}"), pharmacyMessage);
                                httpResponseMessages.Add(response);
                                _pharmacyCount += pharmacyMessage.PharmacyExtracts.Count;
                                sentPatientPharmacies.AddRange(pharmacyMessage.PharmacyExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientPharmacy.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, pharmacyMessage.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(pharmacyMessage);
                                throw;
                            }
                        }

                        if (statusMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{statusMessageBag.EndPoint}"), statusMessage);
                                httpResponseMessages.Add(response);
                                _statusCount += statusMessage.StatusExtracts.Count;
                                sentPatientStatuses.AddRange(statusMessage.StatusExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientStatus.ToString()}es for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, statusMessage.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                PrintMessage(statusMessage);
                                throw;
                            }
                        }

                        if (visitsMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{visitsMessageBag.EndPoint}"), visitsMessage);
                                httpResponseMessages.Add(response);
                                _visitCount += visitsMessage.VisitExtracts.Count;
                                sentPatientVisits.AddRange(visitsMessage.VisitExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientVisit.ToString()}s for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, visitsMessage.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                                PrintMessage(visitsMessage);
                                throw;
                            }
                        }

                        if (adverseEventsMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{adverseEventsMessageBag.EndPoint}"), adverseEventsMessage);
                                httpResponseMessages.Add(response);
                                _adverseEventCount += adverseEventsMessage.AdverseEventExtracts.Count;
                                sentPatientAdverseEvents.AddRange(adverseEventsMessage.AdverseEventExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientAdverseEvent.ToString()}s for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientAdverseEvent, adverseEventsMessage.AdverseEventExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                                PrintMessage(adverseEventsMessage);
                                throw;
                            }
                        }

                        //update UI in set number of batches
                        if (_count % Batch == 0)
                        {
                            foreach (var httpResponseMessage in httpResponseMessages)
                            {
                                var response = await httpResponseMessage;
                                if (response.IsSuccessStatusCode)
                                {
                                    var content = response.Content.ReadAsStringAsync();
                                    responses.Add(content);
                                    sentPatients.Add(id);
                                }
                                //Retry failed requests for set number of retries
                                else
                                {
                                    int retry = 0;
                                    for (int i = 0; i < MaxRetries; i++)
                                    {
                                        retry = i;
                                        var r = await client.PostAsCompressedAsync(sendTo.GetUrl($"{response.RequestMessage.RequestUri}"), response.RequestMessage.Content);

                                        if (r.IsSuccessStatusCode)
                                        {
                                            var content = response.Content.ReadAsStringAsync();
                                            responses.Add(content);
                                            break;
                                        }
                                    }
                                    //if all retries fail throw error
                                    if (retry == 3)
                                    {
                                        //Show error message in UI
                                        DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending Extracts for patient id {id}"));
                                        var error = await response.Content.ReadAsStringAsync();

                                        Log.Error(error, $"Host Response Error");
                                        throw new Exception(error);
                                    }
                                }
                            }
                            httpResponseMessages.Clear();
                            UpdateUiNumbers(ExtractStatus.Sending);
                        }
                    }
                    //update extract sent field
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.Patient, sentPatients));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientArt, sentPatientArts));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientBaseline, sentPatientBaselines));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientLab, sentPatientLabs));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientPharmacy, sentPatientPharmacies));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientStatus, sentPatientStatuses));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientVisit, sentPatientVisits));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientAdverseEvent, sentPatientAdverseEvents));
                    UpdateUiNumbers(ExtractStatus.Sent);
                    //Show error message in UI
                    DomainEvents.Dispatch(new DwhMessageNotification(false, $"Extracts sent successfully!"));
                }

                foreach (var r in responses)
                {
                    var response = await r;
                    output.Add(response);
                }
                return(output);
            }
            //when hang fire tries to re-queue the job after thirty minutes or when sending was interrupted midway
            return(null);
        }
Пример #2
0
        public async Task <List <string> > SendExtractsAsync(SendManifestPackageDTO sendTo)
        {
            var client    = new HttpClient();
            var responses = new List <string>();

            var ids   = _reader.ReadAllIds().ToList();
            int count = 0;
            int total = ids.Count;

            foreach (var id in ids)
            {
                count++;

                var patient            = _packager.GenerateExtracts(id);
                var artMessageBag      = ArtMessageBag.Create(patient);
                var baselineMessageBag = BaselineMessageBag.Create(patient);
                var labMessageBag      = LabMessageBag.Create(patient);
                var pharmacyMessageBag = PharmacyMessageBag.Create(patient);
                var statusMessageBag   = StatusMessageBag.Create(patient);
                var visitsMessageBag   = VisitsMessageBag.Create(patient);


                foreach (var message in artMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{artMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, message.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, message.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in baselineMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{baselineMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, message.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, message.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in labMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{labMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, message.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, message.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in pharmacyMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{pharmacyMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, message.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, message.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in statusMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{statusMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, message.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Error");
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, message.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in visitsMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{visitsMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, message.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Error");
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, message.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                        PrintMessage(message);
                        throw;
                    }
                }


                DomainEvents.Dispatch(new DwhSendNotification(new SendProgress(nameof(PatientExtract), Common.GetProgress(count, total))));

                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.Patient, new List <Guid> {
                    id
                }, SendStatus.Sent));
            }

            return(responses);
        }