Exemplo n.º 1
0
        private async Task ExecuteCore(List <EventEntity> eventEntities)
        {
            logger.LogTrace($"Begin ExecuteCore Count:{eventEntities.Count} ");
            var events = eventEntities.Select(entity =>
            {
                var eventContentBytes = Encoding.UTF8.GetBytes(entity.Content);
                var bytesTransport    = new EventBytesTransport(entity.Name, entity.Id, eventContentBytes);
                var bytes             = bytesTransport.GetBytes();
                var targetName        = producerContainer.GetTargetName(entity.Name);
                entity.StatusName     = nameof(EventStatus.Published);
                entity.ExpiresAt      = DateTime.UtcNow.AddSeconds(options.PublishedEventsExpiredAfterSeconds);
                return(targetName, bytes);
            });

            eventEntities.ForEach(entity =>
            {
                entity.StatusName = nameof(EventStatus.Published);
                entity.ExpiresAt  = DateTime.UtcNow.AddSeconds(options.PublishedEventsExpiredAfterSeconds);
            });
            logger.LogTrace($"Begin BulkPublish {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} ");
            await producer.BulkPublish(events);

            logger.LogTrace($"Begin BulkPublish {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} ");
            if (eventEntities.Count > 10)
            {
                await eventStorage.BulkChangePublishStateAsync(eventEntities);
            }
            else
            {
                await eventStorage.ChangePublishStateAsync(eventEntities);
            }

            logger.LogTrace($"End ExecuteCore {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}  Count:{eventEntities.Count} ");
        }
        public async Task ProcessInternal()
        {
            var now = DateTime.UtcNow;

            var pendingMessages = await eventStorage.GetEventsOfNeedRetry();

            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.LogDebug($"{nameof(PendingMessageRetryProcessor)}  pendingMessages count:{pendingMessages.Count()}");
            }
            foreach (var pendingMessage in pendingMessages)
            {
                var eventContentBytes = Encoding.UTF8.GetBytes(pendingMessage.Content);
                var bytesTransport    = new EventBytesTransport(pendingMessage.Name, pendingMessage.Id, eventContentBytes);
                var bytes             = bytesTransport.GetBytes();
                if (pendingMessage.Retries > options.MaxFailedRetryCount)
                {
                    pendingMessage.StatusName = nameof(EventStatus.Failed);
                    continue;
                }
                pendingMessage.Retries++;
                var targetName = producerContainer.GetTargetName(pendingMessage.Name);
                await producer.Publish(targetName, bytes);

                pendingMessage.StatusName = nameof(EventStatus.Published);
                pendingMessage.ExpiresAt  = DateTime.UtcNow.AddSeconds(options.PublishedEventsExpiredAfterSeconds);
            }
            if (pendingMessages.Count() > 0)
            {
                if (pendingMessages.Count() > 10)
                {
                    await eventStorage.BulkChangePublishStateAsync(pendingMessages);
                }
                else
                {
                    await eventStorage.ChangePublishStateAsync(pendingMessages);
                }
            }
        }