private async Task <Unit> HandleError(PowershellFailure failure, IOperationTaskMessage command)
        {
            await _bus.Publish(OperationTaskStatusEvent.Failed(command.OperationId, command.TaskId,
                                                               new ErrorData {
                ErrorMessage = failure.Message
            })).ConfigureAwait(false);

            return(Unit.Default);
        }
Example #2
0
        public async Task Handle(CreateNewOperationTaskCommand message)
        {
            var command = JsonConvert.DeserializeObject(message.CommandData, Type.GetType(message.CommandType));

            var op = await _dbContext.Operations.FindAsync(message.OperationId).ConfigureAwait(false);

            if (op == null)
            {
                MarkAsComplete();
                return;
            }

            var task = await _dbContext.OperationTasks.FindAsync(message.TaskId).ConfigureAwait(false);

            if (task == null)
            {
                task = new OperationTask
                {
                    Id        = message.TaskId,
                    Operation = op,
                };

                _dbContext.Add(task);
            }

            task.Name = command.GetType().Name;
            Data.Tasks.Add(message.TaskId, command.GetType().AssemblyQualifiedName);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            var sendCommandAttribute = command.GetType().GetCustomAttribute <SendMessageToAttribute>();

            switch (sendCommandAttribute.Recipient)
            {
            case MessageRecipient.VMHostAgent:
            {
                switch (command)
                {
                case IMachineCommand machineCommand:
                {
                    var machine = await _dbContext.Machines.FindAsync(machineCommand.MachineId).ConfigureAwait(false);

                    if (machine == null)
                    {
                        if (command.GetType().GetCustomAttribute(typeof(MachineMayNotExistsAttribute)) != null)
                        {
                            await Handle(OperationTaskStatusEvent.Completed(message.OperationId, message.TaskId));
                        }
                        else
                        {
                            await Handle(OperationTaskStatusEvent.Failed(message.OperationId, message.TaskId,
                                                                         new ErrorData {
                                        ErrorMessage = "Machine not found"
                                    }));
                        }

                        return;
                    }

                    await _bus.Advanced.Routing.Send($"{QueueNames.VMHostAgent}.{machine.AgentName}", command)
                    .ConfigureAwait(false);

                    return;
                }

                case IHostAgentCommand agentCommand:
                    await _bus.Advanced.Routing.Send($"{QueueNames.VMHostAgent}.{agentCommand.AgentName}", command)
                    .ConfigureAwait(false);

                    return;

                default:
                    throw new InvalidDataException($"Don't know how to route operation task command of type {command.GetType()}");
                }
            }

            case MessageRecipient.Controllers:
                await _bus.Send(command);

                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #3
0
 public Task Fail(object message = null)
 {
     return(Bus.SendLocal(OperationTaskStatusEvent.Failed(Data.OperationId, Data.InitiatingTaskId, message)));
 }