Ejemplo n.º 1
0
        public async Task <int> AddJobAsync(Job2Dto job2Dto)
        {
            _logger.LogInformation("Incoming Dto {0}", job2Dto);
            string currentUsername = GetCurrentUserEmail();

            var job = _mapper.Map <Job>(job2Dto.Job);

            if (job != null)
            {
                JobCreatedIntegrationEvent jobCreatedEvent = null;

                await ResilientTransaction.New(_dbContext).ExecuteAsync(async() =>
                {
                    job.TransfereeId  = SaveTransferee(job2Dto.Transferee);
                    job.AccountEntity = _dbContext.AccountEntity.FirstOrDefault(a => a.Id == job.AccountEntityId);

                    var dateTimeAdded = DateTime.UtcNow;
                    job.DateCreated   = dateTimeAdded;
                    job.DateModified  = dateTimeAdded;
                    job.CreatedBy     = GetCurrentUserEmail();
                    job.ModifiedBy    = GetCurrentUserEmail();

                    if (job2Dto.Job.IsSurveyAndBid)
                    {
                        job.JobStatus = JobStatusIdentifier.SURVEY_BID;
                    }

                    _dbContext.Job.Attach(job);
                    _dbContext.Entry(job).State = EntityState.Added;

                    await _dbContext.SaveChangesAsync();

                    //update the fks in the job table
                    var kvp                  = await SaveAddresses(job2Dto.JobInfo);
                    job.OriginAddressId      = kvp.FirstOrDefault(s => s.Key == AddressType.ORIGIN).Value;
                    job.DestinationAddressId = kvp.FirstOrDefault(s => s.Key == AddressType.DESTINATION).Value;

                    await _dbContext.SaveChangesAsync();

                    jobCreatedEvent            = job.FromJob <JobCreatedIntegrationEvent>();
                    jobCreatedEvent.Transferee = job2Dto.ToTransfereeIntegrationEvent <TransfereeCreatedIntegrationEvent>();
                    jobCreatedEvent.UserId     = GetJobsMoveConsultantEmail(job.Id);

                    await _eventLogService.SaveEventAsync(jobCreatedEvent);
                });

                // Publish the integration event through the event bus
                _eventBus.Publish(jobCreatedEvent);
                await _eventLogService.MarkEventAsPublishedAsync(jobCreatedEvent.Id);

                _logger.LogInformation("Job added {0}", job.Id);
            }

            return(job.Id);
        }
Ejemplo n.º 2
0
    private async Task PublishPendingIntegrationEvents(CancellationToken cancellationToken)
    {
        var pendingLogEvents = await _eventLogService
                               .RetrieveScopedEventsPendingToPublishAsync();

        foreach (var logEvt in pendingLogEvents)
        {
            try
            {
                await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                _logger.LogInformation("----- Publishing integration event: {IntegrationEventId}", logEvt.EventId);
                await _eventsPublisher.Publish(logEvt.IntegrationEvent, cancellationToken);

                _logger.LogInformation("----- Published integration event: {IntegrationEventId}", logEvt.EventId);

                await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId}", logEvt.EventId);
                await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
            }
        }
    }
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            foreach (var logEvt in pendingLogEvents)
            {
                _logger.LogInformation(
                    "----- Publishing integration event: {IntegrationEventId} - ({@IntegrationEvent})", logEvt.Id,
                    logEvt.IntegrationEvent);

                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.Id);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.Id);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId}", logEvt.Id);

                    await _eventLogService.MarkEventAsFailedAsync(logEvt.Id);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
        {
            await SaveEventAndOrderingContextChangesAsync(evt);

            _eventBus.Publish(evt);
            await _eventLogService.MarkEventAsPublishedAsync(evt);
        }
        /// <summary>
        /// Fetches any pending events from the current transaction and publishes these through the event bus one by one.
        /// Each event passes through several states, making it easier to track when and if an event fails to be published.
        /// If any failures are detected, publishing that event will NOT be retried (no logic for handling this exists at this point).
        /// </summary>
        /// <param name="transactionId">The transaction id of the DbContextTransaction the given events where created during.</param>
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            var pendingEvents = await _integrationEventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            foreach (var logEvent in pendingEvents)
            {
                try
                {
                    await _integrationEventLogService.MarkEventAsInProgressAsync(logEvent.EventId);

                    _logger.LogDebug("Event {0} status marked as in progress.", logEvent.EventId);

                    await _eventBus.Publish(logEvent.IntegrationEvent, logEvent.EventType);

                    _logger.LogDebug("Published event {0} with content: {1}", logEvent.EventId, logEvent.Content);

                    await _integrationEventLogService.MarkEventAsPublishedAsync(logEvent.EventId);

                    _logger.LogDebug("Event {0} status marked as published.", logEvent.EventId);
                }
                catch (Exception ex)
                {
                    await _integrationEventLogService.MarkEventAsFailedAsync(logEvent.EventId);

                    _logger.LogError(ex, "Event {0} status marked as failed", logEvent.EventId);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Publish the events through the event bus async
        /// </summary>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        public async Task PublishEventsThroughEventBusAsync(Guid transactionId)
        {
            // Retrieve all the pending events to be published
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync(transactionId);

            // Loop through each integration event
            foreach (var logEvt in pendingLogEvents)
            {
                // Log publishing the event
                _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", logEvt.EventId, Program.AppName, logEvt.IntegrationEvent);

                try
                {
                    // Mark the event as in progress
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    // Publish the event
                    _eventBus.Publish(logEvt.IntegrationEvent);
                    // Mark the event as published
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception ex)
                {
                    // In case of exception, log the exception
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId} from {AppName}", logEvt.EventId, Program.AppName);
                    // Mark the event as failed
                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 通过事件总线发布事件
 /// </summary>
 /// <param name="evt">事件</param>
 /// <returns></returns>
 public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
 {
     //001.发布事件
     _eventBus.Publish(evt);
     //002.标志事件为已发布
     await _eventLogService.MarkEventAsPublishedAsync(evt);
 }
        public async Task <ActionResult> DeleteAsync(long id)
        {
            var room = await roomRepository.GetByIdAsync(id);

            if (room == null)
            {
                return(NotFound(new ErrorDTO {
                    ErrorMessage = "Room does not exist"
                }));
            }

            var roomDeletedEvent = new RoomDeletedEvent(room.Id);

            using (var transaction = session.BeginTransaction())
            {
                await roomRepository.DeleteAsync(id);

                await integrationEventLogService.SaveEventAsync(roomDeletedEvent);

                transaction?.Commit();
            }

            // Publish integration event: RoomDeletedEvent
            using (var transaction = session.BeginTransaction())
            {
                eventBus.Publish(roomDeletedEvent);
                await integrationEventLogService.MarkEventAsPublishedAsync(roomDeletedEvent.EventId);

                transaction?.Commit();
            }

            return(NoContent());
        }
Ejemplo n.º 9
0
        public async Task PublishThroughEventBusAsync(IEvent evt)
        {
            await SaveEventAndBusinessDbContextChangesAsync(evt);

            await _eventBus.Publish(evt);

            await _eventLogService.MarkEventAsPublishedAsync(evt);
        }
Ejemplo n.º 10
0
        public Task PublishThroughEventBusAsync(IEvent evt)
        {
            Task[] tasks = new Task[3];
            //tasks[0] = _eventLogService.SaveEventAsync(evt, _context.Database.CurrentTransaction.GetDbTransaction());
            tasks[1] = _eventBus.Publish(evt);
            tasks[2] = _eventLogService.MarkEventAsPublishedAsync(evt);

            return(Task.WhenAll(tasks));
        }
Ejemplo n.º 11
0
        public async Task <ActionResult> UpdateProductAsync([FromBody] CatalogItem productToUpdate)
        {
            var catalogItem = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);

            if (catalogItem == null)
            {
                return(NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." }));
            }

            var oldPrice = catalogItem.Price;
            var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;

            // Update current product
            catalogItem = productToUpdate;
            _catalogContext.CatalogItems.Update(catalogItem);

            if (raiseProductPriceChangedEvent) // Save product's data and publish integration event through the Event Bus if price has changed
            {
                _logger.LogInformation(" [x] CatalogController.UpdateProduct(): Price has changed, integration event is being prepared...");

                var productPriceChangeEvent = new ProductPriceChangedIntegrationEvent(productToUpdate.Id,
                                                                                      oldPrice, productToUpdate.Price);

                var strategy = _catalogContext.Database.CreateExecutionStrategy();
                _logger.LogInformation(" [x] CatalogController.UpdateProductAsync(): Beginning new transaction to save event and commit changes.");
                await strategy.Execute(async() => {
                    using (var transaction = _catalogContext.Database.BeginTransaction())
                    {
                        await _eventLogService.SaveEventAsync(productPriceChangeEvent, transaction);
                        await _catalogContext.SaveChangesAsync();
                        transaction.Commit();
                        _logger.LogInformation(" [x] CatalogController.UpdateProductAsync(): Transaction ({0}) has been committed.", transaction.TransactionId);
                    }
                });

                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(productPriceChangeEvent.Id);

                    _eventBus.Publish(productPriceChangeEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(productPriceChangeEvent.Id);
                }

                catch (Exception e)
                {
                    _logger.LogError(e, " [x] CatalogController.UpdateProductAsync(): Fail when publishing integration event {0}.", productPriceChangeEvent.Id);
                    await _eventLogService.MarkEventAsFailedAsync(productPriceChangeEvent.Id);
                }
            }
            else // Just save the updated product because the Product's Price hasn't changed.
            {
                await _catalogContext.SaveChangesAsync();
            }

            return(Ok());
        }
Ejemplo n.º 12
0
        public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
        {
            try
            {
                await _eventLogService.MarkEventAsInProgressAsync(evt.Id);

                _eventBus.Publish(evt);
                await _eventLogService.MarkEventAsPublishedAsync(evt.Id);
            }
            catch (Exception)
            {
                await _eventLogService.MarkEventAsFailedAsync(evt.Id);
            }
        }
        public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
        {
            try {
                _logger.LogInformation("----- Publishing integration event: {IntegrationEventId_published} from {AppName} - ({@IntegrationEvent})", evt.Id, Program.AppName, evt);

                await _eventLogService.MarkEventAsInProgressAsync(evt.Id);

                _eventBus.Publish(evt);
                await _eventLogService.MarkEventAsPublishedAsync(evt.Id);
            } catch (Exception ex) {
                _logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", evt.Id, Program.AppName, evt);
                await _eventLogService.MarkEventAsFailedAsync(evt.Id);
            }
        }
Ejemplo n.º 14
0
        public async Task PublishEvent(IntegrationEvent @event)
        {
            if (_integrationEventLogService.Find(@event.Id) == null)
            {
                throw new IntegrationEventWithIdDoesNotExistsException(@event.Id);
            }

            try
            {
                _eventBus.Publish(@event);
                await _integrationEventLogService.MarkEventAsPublishedAsync(@event);
            }
            catch (FailToSendEventToRabbitException)
            {
                await _integrationEventLogService.MarkEventAsFailToPublishAsync(@event);

                throw;
            }
        }
Ejemplo n.º 15
0
        public async Task PublishThroughEventBusAsync(IntegrationEvent @event)
        {
            try
            {
                await _eventLogService.MarkEventAsInProgressAsync(@event.Id);

                await _eventBus.PublishAsync(@event);

                await _eventLogService.MarkEventAsPublishedAsync(@event.Id);

                _logger.Information($"PublishThroughEventBusAsync {@event}");
            }
            catch (Exception ex)
            {
                await _eventLogService.MarkEventAsFailedAsync(@event.Id);

                _logger.Error($"PublishThroughEventBusAsync erorr {@event}");
            }
        }
        public async Task PublishEventsThroughEventBusAsync()
        {
            var pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();

            foreach (var logEvt in pendingLogEvents)
            {
                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception)
                {
                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }
        public async Task PublishEventsAsync(Guid transactionId)
        {
            var pendingEventLogs = await _integrationEventLogService.GetPendingEventLogEntries(transactionId);

            foreach (var eventLog in pendingEventLogs)
            {
                try
                {
                    await _integrationEventLogService.MarkEventAsInProgressAsync(eventLog.EventId);

                    _eventBus.Publish(eventLog.IntegrationEvent);
                    await _integrationEventLogService.MarkEventAsPublishedAsync(eventLog.EventId);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error publishing");
                    await _integrationEventLogService.MarkEventAsFailedAsync(eventLog.EventId);
                }
            }
        }
        public async Task PublishEventsThroughEventBusAsync()
        {
            IEnumerable <IntegrationEventLogEntry> pendingLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();

            foreach (var logEvt in pendingLogEvents)
            {
                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);

                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId} from ProductService", logEvt.EventId);

                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }
        public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
        {
            _eventBus.Publish(evt);

            await _eventLogService.MarkEventAsPublishedAsync(evt);
        }
Ejemplo n.º 20
0
 public async Task  PublishThroughEventBusAsync(Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events.IntegrationEvent evt)
 {
     _eventBus.Publish(evt);
     await _eventLogService.MarkEventAsPublishedAsync(evt);
 }