public UIElement GetView(CommandResultDto commandResult, string result) { HttpResponseMessage response = null; if (commandResult.Status != null) { response = HttpResponseDeserializer.DecodeResponse(result); } using (var scope = _context.CreateChildContainer()) { var viewProviders = scope.Resolve <IEnumerable <ICommandResultViewProvider> >().OrderByDescending(x => x.Priority); var serviceProvider = scope.Resolve <IServiceProvider>(); foreach (var viewProvider in viewProviders) { try { var view = viewProvider.GetView(response, commandResult, serviceProvider); if (view != null) { return(view); } } catch (Exception e) { Logger.Error(e, "Error occurred on GetView() on {viewProviderType}", viewProvider.GetType().FullName); } } } return(null); }
private void OnTaskCommandResultCreated(CommandResultDto obj) { _dispatcher.Current.BeginInvoke(new Action(() => { if (!_executions.TryGetValue(obj.TaskExecutionId, out var execution)) { return; } if (execution.Results == null) { execution.Results = new ObservableCollection <ICommandStatusViewModel>(); } else { var existingResult = execution.Results.FirstOrDefault(x => x.CommandResultId == obj.CommandResultId); if (existingResult != null) { execution.Results.Remove(existingResult); _results.Remove(existingResult.CommandResultId); } } var result = new CommandResultViewModel(obj); _results.Add(result.CommandResultId, result); execution.Results.Add(result); })); }
Task ITaskResultStorage.CreateCommandResult(CommandResultDto commandResult) { lock (_commandResultsLock) { CommandResults.Add(commandResult); } return(Task.CompletedTask); }
public void Execute() { while (_movements.Count > 0) { var movement = _movements.Dequeue(); ProcessMovement(movement); } Result = new CommandResultDto(CommandResult.Success, string.Empty); }
public UIElement GetView(HttpResponseMessage responseMessage, CommandResultDto dto, IServiceProvider serviceProvider) { if (dto.Status != null) { return(null); } return(new ExceptionView { DataContext = new ExceptionViewModel(dto) }); }
private void AddCommandResult(CommandResultDto commandResultDto) { if (commandResultDto.Validate(new ValidationContext(commandResultDto)).Any()) { return; } if (_results.TryAdd(commandResultDto.CommandResultId, commandResultDto)) { TaskResultAdded?.Invoke(this, commandResultDto); } }
public async Task <IActionResult> CreateCommandResult([FromBody] CommandResultDto commandResultDto, [FromServices] ICreateTaskCommandResultAction action, [FromServices] ActiveTasksManager activeTasksManager) { if (activeTasksManager.ActiveCommands.TryGetValue(new TargetId(User.GetClientId()), out var status)) { status.Processes.TryRemove(commandResultDto.CommandResultId, out _); } await action.BizActionAsync(commandResultDto); return(await BizActionStatus(action, async() => { await _hubContext.Clients.All.SendAsync(HubEventNames.TaskCommandResultCreated, commandResultDto); return Ok(); })); }
public UIElement GetView(HttpResponseMessage responseMessage, CommandResultDto dto, IServiceProvider serviceProvider) { if (responseMessage.Content?.Headers.ContentType.MediaType != "maze/jsonlog") { return(null); } var log = responseMessage.Content.ReadAsStringAsync().Result; var logEvents = new List <LogEvent>(); using (var stringReader = new StringReader(log)) { var logReader = new LogEventReader(stringReader); while (logReader.TryRead(out var logEvent)) { logEvents.Add(logEvent); } } return(new LogView(logEvents)); }
public CommandResultViewModel(CommandResultDto commandResult) { CommandResult = commandResult; CommandResultId = commandResult.CommandResultId; TaskExecutionId = commandResult.TaskExecutionId; CommandName = commandResult.CommandName; Result = commandResult.Result; FinishedAt = commandResult.FinishedAt; StatusCode = commandResult.Status; if (StatusCode == null) { Status = CommandStatus.Failure; } else if (StatusCode >= 200 && StatusCode <= 299) { Status = CommandStatus.Succeeded; } else { Status = CommandStatus.Error; } }
private void ProcessMovement(char movement) { switch (movement) { case char c when c == 'R': _rover.TurnRight(); break; case char c when c == 'R': _rover.TurnLeft(); break; case char c when c == 'M': var res = _rover.TryMove(); if (res != RoverMovementResult.Success) { Result = new CommandResultDto(CommandResult.Fail, $"Movement failed. Reason: {res:G}"); } break; default: throw new ArgumentException($"Incorrect command literal {movement} at position {_movements.Count + 1}"); } }
public async Task <CommandResult> BizActionAsync(CommandResultDto inputData) { if (ValidateModelFailed(inputData)) { return(default);
public ExceptionViewModel(CommandResultDto commandResult) { ExceptionMessage = commandResult.Result; }
private async Task ExecuteCommand(TargetId id, IServiceProvider services, CommandInfo commandInfo, TasksMachineStatus status, Guid executionId, CancellationToken cancellationToken) { var executorType = _executorTypes[commandInfo]; if (executorType == null) { return; } var service = services.GetService(executorType); if (service == null) { return; } var commandName = commandInfo.GetType().Name.TrimEnd("CommandInfo", StringComparison.Ordinal); var commandResult = new CommandResultDto { CommandResultId = Guid.NewGuid(), TaskExecutionId = executionId, CommandName = commandName }; var commandProcessDto = new CommandProcessDto { CommandResultId = commandResult.CommandResultId, TaskExecutionId = executionId, CommandName = commandName }; Task UpdateStatus(CommandProcessDto arg) { commandProcessDto.StatusMessage = arg.StatusMessage; commandProcessDto.Progress = arg.Progress; return(_hubContext.Clients.All.SendAsync(HubEventNames.TaskCommandProcess, commandProcessDto, cancellationToken)); } var executionMethod = _executionMethods[executorType]; status.Processes.TryAdd(commandProcessDto.CommandResultId, commandProcessDto); try { using (var context = new DefaultTaskExecutionContext(_taskSession, _mazeTask, services, UpdateStatus)) { context.ReportProgress(null); //important to notify about the start of the operation var task = (Task <HttpResponseMessage>)executionMethod.Invoke(service, new object[] { commandInfo, id, context, cancellationToken }); var response = await task; using (var memoryStream = new MemoryStream()) { await HttpResponseSerializer.Format(response, memoryStream); commandResult.Result = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); commandResult.Status = (int)response.StatusCode; } } } catch (Exception e) { _logger.LogWarning(e, "An error occurred when executing {method}", executorType.FullName); commandResult.Result = e.ToString(); } finally { status.Processes.TryRemove(commandProcessDto.CommandResultId, out _); } commandResult.FinishedAt = DateTimeOffset.UtcNow; try { await _taskResultStorage.CreateCommandResult(commandResult); } catch (Exception e) { _logger.LogError(e, "Executing CreateCommandResult failed."); } await _hubContext.Clients.All.SendAsync(HubEventNames.TaskCommandResultCreated, commandResult); }
private void BuilderOnTaskResultAdded(object sender, CommandResultDto e) { _hubContext.Clients.All.SendAsync(HubEventNames.TaskCommandResultCreated, e); }
public Task CreateCommandResult(CommandResultDto commandResult) { var action = _serviceProvider.GetRequiredService <ICreateTaskCommandResultAction>(); return(action.BizActionAsync(commandResult)); }