private Dictionary <string, Tuple <Type, List <Type> > > GetEventTypes(Type[] eventHandlerTypes, IServiceScopeFactory serviceScopeFactory) { var dictionary = new Dictionary <string, Tuple <Type, List <Type> > >(); using (var scope = serviceScopeFactory.Create()) { foreach (var eventHandlerType in eventHandlerTypes) { var eventHandler = (Queue.EventHandler)scope.ServiceProvider.GetService(eventHandlerType); eventHandler.Initialize(); foreach (var eventType in eventHandler.GetEventTypes()) { if (dictionary.ContainsKey(eventType.Name)) { dictionary[eventType.Name].Item2.Add(eventHandlerType); } else { dictionary.Add(eventType.Name, new Tuple <Type, List <Type> >(eventType, new List <Type> { eventHandlerType })); } } } } return(dictionary); }
private List <Type> GetCommandTypes(Type commandHandlerType, IServiceScopeFactory serviceScopeFactory) { using (var scope = serviceScopeFactory.Create()) { var commandHandler = (CommandHandler)scope.ServiceProvider.GetService(commandHandlerType); commandHandler.Initialize(); return(commandHandler.GetCommandTypes()); } }
private async Task <string> ExecuteJob() { var correlationId = Guid.NewGuid().ToString(); var startedAt = DateTime.UtcNow; var exception = default(Exception); ICommandInfo[] commandInfoArray; using (var scope = _serviceScopeFactory.Create()) { var commandExecutor = (ICommandExecutor)scope.ServiceProvider.GetService(typeof(ICommandExecutor)); var loggerContext = (LoggerContext)scope.ServiceProvider.GetService(typeof(ILoggerContext)); commandExecutor.SetCorrelationId(correlationId); loggerContext.SetCorrelationId(correlationId); try { var job = (IJob)scope.ServiceProvider.GetService(_jobType); await job.Execute(); } catch (Exception ex) { exception = ex; } commandInfoArray = commandExecutor.GetCommands(); } var level = exception == null ? LogEventLevel.Information : LogEventLevel.Error; var logContent = new { message = exception == null ? "Job executed successfully." : "Job executed with errors.", correlationId, jobType = _jobType.Name, startedAt, executionTime = $"{Math.Round((DateTime.UtcNow - startedAt).TotalMilliseconds)}ms", commands = commandInfoArray.Select(c => new { name = c.GetType().Name, startedAt = c.StartedAt, result = c.Result, consistentHashKey = c.ConsistentHashKey, executionTime = c.ExecutionTime }) }.ToJson(Formatting.Indented); Log.Logger.Write(level, exception, "content: {content}, correlationId: {correlationId}", logContent, correlationId); return(ScheduleJobCommand); }
public async Task Handle(Command command) { command.Started(); var exception = default(Exception); var isError = false; var output = default(object); try { await _commandExecutionStrategy.Execute(async() => { using (var scope = _serviceScopeFactory.Create()) using (var transaction = _transactionScopeFactory.Create(scope)) { var commandHandler = (CommandHandler)scope.ServiceProvider.GetService(_commandHandlerType); var loggerContext = (LoggerContext)scope.ServiceProvider.GetService(typeof(ILoggerContext)); commandHandler.Initialize(); loggerContext.SetCorrelationId(command.AsCommandInfo().CorrelationId); try { output = await commandHandler.Handle(command); if (output is IOperationResult operationResult && operationResult.IsError) { isError = true; transaction.Rollback(); } else { transaction.Commit(); } } catch { transaction.Rollback(); throw; } }
private async Task Handle(Event @event) { var startedAt = DateTime.UtcNow; var exceptionList = new List <Exception>(); var eventHandlerInfoList = new List <EventHandlerInfo>(); var correlationId = @event.Id.ToString(); try { if (!_eventHandlerDictionary.ContainsKey(@event.Type)) { await _queueDbAccessManager.AddLog(@event.Id, "Missed"); return; } var eventType = _eventHandlerDictionary[@event.Type].Item1; var eventObj = JsonConvert.DeserializeObject(@event.Body, eventType); foreach (var eventHandlerType in _eventHandlerDictionary[@event.Type].Item2) { using (var scope = _serviceScopeFactory.Create()) { var handlerStartedAt = DateTime.UtcNow; var commandExecutor = (ICommandExecutor)scope.ServiceProvider.GetService(typeof(ICommandExecutor)); var result = EventHandlerInfo.SuccessResult; commandExecutor.SetCorrelationId(correlationId); try { var eventHandler = (EventHandler)scope.ServiceProvider.GetService(eventHandlerType); var loggerContext = (LoggerContext)scope.ServiceProvider.GetService(typeof(ILoggerContext)); eventHandler.Initialize(); loggerContext.SetCorrelationId(correlationId); await eventHandler.Handle(eventObj); } catch (Exception ex) { exceptionList.Add(ex); result = EventHandlerInfo.ErrorResult; } eventHandlerInfoList.Add(new EventHandlerInfo(commandExecutor.GetCommands(), result, handlerStartedAt, eventHandlerType.Name)); } } await _queueDbAccessManager.AddLog(@event.Id, exceptionList.Count == 0? "Success" : "Error"); } catch (Exception ex) { exceptionList.Add(ex); } var aggregateException = exceptionList.Count == 0 ? null : new AggregateException(exceptionList); var level = aggregateException == null ? LogEventLevel.Information : LogEventLevel.Error; var logContent = new { message = aggregateException == null ? "Event handled successfully." : "Event handled with errors", correlationId, eventId = @event.Id, eventType = @event.Type, startedAt, consistentHashKey = @event.ConsistentHashKey, handlingTime = $"{Math.Round((DateTime.UtcNow - startedAt).TotalMilliseconds)}ms", handlers = eventHandlerInfoList.Select(i => new { name = i.Type, startedAt = i.StartedAt, result = i.Result, handlingTime = i.HandlingTime, commands = i.Commands.Select(c => new { name = c.GetType().Name, startedAt = c.StartedAt, result = c.Result, consistentHashKey = c.ConsistentHashKey, executionTime = c.ExecutionTime }) }), listener = new { cycleId = @event.Listener.CycleId, startedAt = @event.Listener.StartedAt, events = @event.Listener.Events } }.ToJson(Formatting.Indented); Log.Logger.Write(level, aggregateException, "content: {content}, correlationId: {correlationId}", logContent, correlationId); }