示例#1
0
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     while (!stoppingToken.IsCancellationRequested)
     {
         var data = JsonConvert.SerializeObject(_client.ReadNextObjectMessage("EmailQueue"));
         if (data.Contains("PatientCreated"))
         {
             string objectId   = GetAttributeValue(GetAttributeValuePair(data, "ID"));
             var    dataObject = JsonConvert.DeserializeObject(data);
             using (EventProducer eventProducer = _eventStore.CreateEventProducer())
             {
                 try
                 {
                     _logger.LogInformation("Email confirmation sent!");
                     EventData eventData = eventProducer.CreateEventData(dataObject, "EmailConfirmation-Sent");
                     eventProducer.SendEvent($"AMQMessages-Patient-{objectId}", eventData);
                 }
                 catch (Exception)
                 {
                     _logger.LogInformation("Email confirmation sent!");
                     EventData eventData = eventProducer.CreateEventData(dataObject, "EmailConfirmation-Failed");
                     eventProducer.SendEvent($"AMQMessages-Patient-{objectId}", eventData);
                 }
             }
         }
         await Task.Delay(1000, stoppingToken);
     }
 }
示例#2
0
        public async Task <PatientDTO> Handle(CreatePatientCommand request, CancellationToken cancellationToken)
        {
            bool tableInsertStatus;

            try
            {
                _patient = await _patientRepository.AddAsync(request.PatientDTO);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Pending");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = true;
            }
            catch (Exception)
            {
                tableInsertStatus = false;
            }
            if (tableInsertStatus == true)
            {
                try
                {
                    PatientMessage patientMessage = new PatientMessage("PatientCreated", _patient);
                    var            message        = await _client.SendObjectMessageAsync("EmailQueue", patientMessage);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(message, "PatientCreatedConfirmation-Sent");
                        eventProducer.SendEvent($"AMQMessages-Patient-{_patient.ID}", eventData);
                    }
                }
                catch (Exception)
                {
                    await _patientRepository.DeleteAsync(_patient.ID);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                        eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                    }
                    tableInsertStatus = false;
                }
            }
            using (EventConsumer eventConsumer = _eventStore.CreateEventConsumer($"AMQMessages-Patient-{_patient.ID}", "EmailCheck"))
            {
                bool eventFound = false;
                while (eventFound == false)
                {
                    var events = await eventConsumer.ReadStreamEventsForwardAsync();

                    if (eventConsumer.FindEventType(events, "EmailConfirmation-Sent"))
                    {
                        eventFound       = true;
                        _workerSucceeded = true;
                    }
                    else if (eventConsumer.FindEventType(events, "EmailConfirmation-Failed"))
                    {
                        eventFound       = true;
                        _workerSucceeded = false;
                    }
                }
            }
            if (_workerSucceeded == false)
            {
                await _patientRepository.DeleteAsync(_patient.ID);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = false;
            }
            if (tableInsertStatus == false)
            {
                return(null);
            }
            using (EventProducer eventProducer = _eventStore.CreateEventProducer())
            {
                EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Committed");
                eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
            }
            return(_patient);
        }