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); } }
public async Task Execute() { if (_contentParser == null) { await Task.Run(() => _context.Response.StatusCode = HttpStatusCode.BadRequest); return; } var content = await _contentParser.Parse <TContent>(_context.Request.Body); await _requestController.Execute( content ); }
public Result <IGraph <IArtifact, Dependency> > Create(IEnumerable <Template> templates) { var graph = new Graph <IArtifact, Dependency>(); var nodeDict = new Dictionary <string, INode <IArtifact> >(); foreach (var template in templates) { foreach (var variant in template.Variants) { var lines = _contentParser.Parse(template.Lines, variant.Variables).ToImmutableList(); var imageId = "unknown"; var tags = new List <string>(); var platform = string.Empty; var references = new List <Reference>(); var components = new List <string>(); var repositories = new List <string>(); var comments = new List <string>(); var dockerfileLines = new List <Line>(); var weight = 0; foreach (var line in lines) { var isMetadata = false; if (line.Type == LineType.Comment) { isMetadata = TrySetByPrefix(line.Text, CommentPrefix, value => comments.Add(value.Trim())) || TrySetByPrefix(line.Text, IdPrefix, value => imageId = value) || TrySetByPrefix(line.Text, TagPrefix, value => tags.Add(value)) || TrySetByPrefix(line.Text, PlatformPrefix, value => platform = value) || TrySetByPrefix(line.Text, BasedOnPrefix, value => { var match = ReferenceRegex.Match(value); if (match.Success) { var weightValue = 0; if (int.TryParse(match.Groups["weight"].Value, out var refWeightValue)) { weightValue = refWeightValue; } references.Add(new Reference(match.Groups["reference"].Value, new Weight(weightValue))); } }) || TrySetByPrefix(line.Text, ComponentsPrefix, value => components.Add(value)) || TrySetByPrefix(line.Text, RepoPrefix, value => repositories.Add(value)) || TrySetByPrefix(line.Text, WeightPrefix, value => { if (int.TryParse(value, out var weightValue)) { weight = weightValue; } }); } if (!isMetadata) { dockerfileLines.Add(line); } } var dockerfile = new Dockerfile(_pathService.Normalize(variant.BuildPath), imageId, platform, tags, components, repositories, comments, references, new Weight(weight), dockerfileLines); if (graph.TryAddNode(new Image(dockerfile), out var dockerImageNode)) { foreach (var tag in tags) { nodeDict[$"{imageId}:{tag}"] = dockerImageNode; } if (graph.TryAddNode(new GeneratedDockerfile(_pathService.Normalize(Path.Combine(dockerfile.Path, "Dockerfile")), dockerfile.Lines), out var dockerfileNode)) { graph.TryAddLink(dockerImageNode, GenerateDependency, dockerfileNode, out _); } } } } var imageNodes = from node in graph.Nodes let image = node.Value as Image where image != null select new { node, image }; // Add references foreach (var from in imageNodes.ToList()) { foreach (var reference in from.image.File.References) { if (nodeDict.TryGetValue(reference.RepoTag, out var toNode)) { graph.TryAddLink(from.node, new Dependency(DependencyType.Build), toNode, out _); } else { if (graph.TryAddNode(reference, out var referenceNode)) { nodeDict[reference.RepoTag] = referenceNode; } graph.TryAddLink(from.node, new Dependency(DependencyType.Pull), referenceNode, out _); } } } return(new Result <IGraph <IArtifact, Dependency> >(graph)); }
public Map Convert() { string fileContent = _fileReader.Read(); return(_contentParser.Parse(fileContent)); }