public async Task CreateAsync(IEvent @event, CancellationToken cancellationToken = default)
        {
            var outboxEventDomainEntity = _domainEntityFactory.CreateInstance <IOutboxEventDomainEntity>();
            await outboxEventDomainEntity.NewAsync(cancellationToken);

            outboxEventDomainEntity.SetEvent(@event);
            outboxEventDomainEntity.Lock();
            await outboxEventDomainEntity.CreateAsync(cancellationToken);
        }
Example #2
0
        public async Task CreateAsync(IMessage message, CancellationToken cancellationToken = default)
        {
            var outboxMessageDomainEntity = _domainEntityFactory.CreateInstance <IOutboxMessageDomainEntity>();
            await outboxMessageDomainEntity.NewAsync(cancellationToken);

            outboxMessageDomainEntity.SetMessage(message);
            outboxMessageDomainEntity.Lock();
            await outboxMessageDomainEntity.CreateAsync(cancellationToken);
        }
        public async Task PublishAsync(Guid id, CancellationToken cancellationToken = default)
        {
            _logger.LogInformation("Publishing OutboxEvent entity with id '{id}'", id);

            var outboxEventDomainEntity = _domainEntityFactory.CreateInstance <IOutboxEventDomainEntity>();
            await outboxEventDomainEntity.GetAsync(id, cancellationToken);

            _logger.LogInformation("Publishing event of type '{type}'", outboxEventDomainEntity.Type);

            bool isPublished = false;

            try
            {
                var @event = outboxEventDomainEntity.GetEvent();
                await _eventPublisher.PublishAsync(@event, cancellationToken);

                _logger.LogInformation("Published event of type '{type}'", outboxEventDomainEntity.Type);
                isPublished = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to publish event of type '{type}' (ID: {id})", outboxEventDomainEntity.Type, outboxEventDomainEntity.EntityId);
                await UnLockAsync(outboxEventDomainEntity, cancellationToken);
            }

            if (isPublished)
            {
                try
                {
                    await UnlockAndMarkAsPublishedAsync(outboxEventDomainEntity, cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.LogCritical(ex, "Failed to mark published event of type '{type}' as published (ID: {id})", outboxEventDomainEntity.Type, outboxEventDomainEntity.EntityId);
                }
            }
        }
Example #4
0
        public async Task SendAsync(Guid id, CancellationToken cancellationToken = default)
        {
            _logger.LogInformation("Sending OutboxMessage entity with id '{id}'", id);

            var outboxMessageDomainEntity = _domainEntityFactory.CreateInstance <IOutboxMessageDomainEntity>();
            await outboxMessageDomainEntity.GetAsync(id, cancellationToken);

            _logger.LogInformation("Sending message of type '{type}'", outboxMessageDomainEntity.Type);

            bool isSent = false;

            try
            {
                var message = outboxMessageDomainEntity.GetMessage();
                await _messageSender.SendAsync(message, cancellationToken);

                _logger.LogInformation("Sent message of type '{type}'", outboxMessageDomainEntity.Type);
                isSent = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to send message of type '{type}'  (ID: {id})", outboxMessageDomainEntity.Type, outboxMessageDomainEntity.EntityId);
                await UnLockAsync(outboxMessageDomainEntity, cancellationToken);
            }

            if (isSent)
            {
                try
                {
                    await UnlockAndMarkAsSentAsync(outboxMessageDomainEntity, cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.LogCritical(ex, "Failed to mark sent message of type '{type}' as sent (ID: {id})", outboxMessageDomainEntity.Type, outboxMessageDomainEntity.EntityId);
                }
            }
        }