Ejemplo n.º 1
0
        private void OnTaskCommandProcess(CommandProcessDto obj)
        {
            _dispatcher.Current.BeginInvoke(new Action(() =>
            {
                if (_results.TryGetValue(obj.CommandResultId, out var commandResult))
                {
                    if (commandResult is CommandProcessViewModel commandProcess)
                    {
                        commandProcess.Update(obj);
                    } //if command result exists, don't do anything

                    return;
                }

                if (_executions.TryGetValue(obj.TaskExecutionId, out var taskExecutionViewModel))
                {
                    var commandProcess = new CommandProcessViewModel(obj);

                    _results.Add(commandProcess.CommandResultId, commandProcess);

                    if (taskExecutionViewModel.Results == null)
                    {
                        taskExecutionViewModel.Results = new ObservableCollection <ICommandStatusViewModel>();
                    }
                    taskExecutionViewModel.Results.Add(commandProcess);
                }
            }));
        }
Ejemplo n.º 2
0
 public CommandProcessViewModel(CommandProcessDto commandProcess)
 {
     Progress        = commandProcess.Progress;
     StatusMessage   = commandProcess.StatusMessage;
     CommandResultId = commandProcess.CommandResultId;
     TaskExecutionId = commandProcess.TaskExecutionId;
     CommandName     = commandProcess.CommandName;
 }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateCommandProcess([FromBody] CommandProcessDto commandResultDto, [FromServices] ActiveTasksManager activeTasksManager)
        {
            var clientId = User.GetClientId();

            var status = activeTasksManager.ActiveCommands.GetOrAdd(new TargetId(clientId), _ => new TasksMachineStatus());

            status.Processes.AddOrUpdate(commandResultDto.CommandResultId, commandResultDto, (id, _) => commandResultDto);

            await _hubContext.Clients.All.SendAsync(HubEventNames.TaskCommandProcess, commandResultDto);

            return(Ok());
        }
Ejemplo n.º 4
0
 public static Task ReportProgress(CommandProcessDto commandProcessDto, IRestClient restClient) =>
 CreateRequest(HttpVerb.Post, "process", commandProcessDto).Execute(restClient);
Ejemplo n.º 5
0
        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);
        }