public virtual async Task Process(TEntity msg, TState state, string name, Func <IServiceProvider, Exception, TEntity, TState, string, Task> errorHandler)
        {
            var list = _handlers.ToList();
            QueueHandlerContext <TEntity, TState> context = new QueueHandlerContext <TEntity, TState>()
            {
                QueueName = name,
                Message   = msg,
                State     = state
            };

            foreach (var h in list)
            {
                try
                {
                    await _retry.Retry(async() =>
                    {
                        using (var scope = _serviceProvider.CreateScope())
                        {
                            context.ServiceProvider = scope.ServiceProvider;
                            var handler             = scope.ServiceProvider.GetRequiredService(h.GetType()) as IQueueHandler <TEntity, TState>;
                            await handler.Process(context);
                        }
                    }, 3, 100, true);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "处理队列消息失败");
                    if (errorHandler != null)
                    {
                        using (var scope = _serviceProvider.CreateScope())
                        {
                            await errorHandler(_serviceProvider, e, msg, state, name);
                        }
                    }
                }

                if (context.Stoped)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
 public virtual Task Process(QueueHandlerContext <TEntity, TState> context)
 {
     return(Task.CompletedTask);
 }