예제 #1
0
        public async Task RunAsync(params string[] operationIds)
        {
            foreach (string operationId in operationIds)
            {
                WorkerOperation operation = await this.repository.GetAsync(operationId);

                var operationHandlers = this.handlers.Where(h => h.OperationEntity == operation.Entity).ToList();

                await Task.WhenAll(operationHandlers.Select(handler => handler.HandleAsync(operation)));
            }
        }
예제 #2
0
        public async Task HandleAsync(WorkerOperation operation)
        {
            IContentParser <IEnumerable <TDto> > parser = this.contentParser.FirstOrDefault(p => p.ContentType == operation.ContentType);

            if (parser == null)
            {
                //TODO handler null parser
            }

            TDto[] dtos = parser.Parse(operation.Content).ToArray();

            var failureReasons = new List <string>();

            for (int i = 0; i < dtos.Length; i++)
            {
                TDto dto = dtos[i];

                if (!IsValid(dto, out string reason))
                {
                    failureReasons.Add($"Validation error processing row {i + 1}. {reason}.");
                    continue;
                }

                try
                {
                    var enrichers = new List <Task>();

                    foreach (IEnricher <TDto> enricher in this.enrichers)
                    {
                        enrichers.Add(enricher.EnrichAsync(dto));
                    }

                    await Task.WhenAll(enrichers);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, $"Failed to process row {i + 1} while running enricher.",
                                         new { operation.Id, operation.JobId, operation.ContentType, operation.Entity }, dto);

                    failureReasons.Add($"Failed to process row {i + 1}.");
                }

                await this.repository.CreateAsync(CreateDomainEntity(dto, operation.Body));
            }

            if (failureReasons.Count > 0)
            {
                await this.workerOperationStatusRepository.CreateAsync(operation, failureReasons);
            }
        }