public async Task <ResponseMessage <VmTicket> > Handle(VmOperationRequestCommand request, CancellationToken cancellationToken) { var result = new ResponseMessage <VmTicket>(); try { var ticket = request.VmTicket; if (!Enum.TryParse(ticket.Operation, out VmOperatioType vmOperatioType) || vmOperatioType == VmOperatioType.Unknown) { result.Message = string.IsNullOrWhiteSpace(ticket.Operation) ? $"{nameof(ticket.Operation)} is missing" : $"Unsupported type of {nameof(ticket.Operation)} {ticket.Operation}"; return(result); } //validate sub name and tags var canReboot = await _vmSdkService.IsRebootAllowed(ticket.SubcriptionId, ticket.ResorceGroup, ticket.VmName).ConfigureAwait(false); if (!canReboot.success) { result.Success = false; result.Message = canReboot.errorMessage; return(result); } //save _repository.Add(ticket, _userId); if (!await _repository.SaveChangesAsync()) { result.Success = false; result.Message = "Failed to save to DB"; return(result); } await _vmTicketHistoryRepository.Create(ticket); //send message to bus BusMessageBase message = new VmOperationRequestMessage { Vm = ticket.VmName, UserId = _userId, TicketId = ticket.Id }.CreateBaseMessage(); try { if (request.WaitForActionToComplete == false) { await _messageBus.PublishMessageTopic(message, _serviceBusConfig.VmOperationRequesTopic); result.ReturnedObject = ticket; result.Success = true; return(result); } } catch (Exception ex) { _logger.LogError($"Failed to PublishMessageTopic {ex?.Message}", message); } //failover if message is not sent to bus, or want respond if (request.WaitForActionToComplete || result.Success == false) { var dto = message as VmOperationRequestMessage; var vmOperationExecuteCommand = new VmOperationExecuteCommand() { VmOperationRequestMessage = dto }; var response = await _mediator.Send(vmOperationExecuteCommand); //update result result = response; return(result); } result.ReturnedObject = ticket; } catch (Exception ex) { //this shouldn't stop the API from doing else so this can be logged _logger.LogError($"{nameof(VmOperationRequestCommandHandler)} failed due to: {ex.Message}"); } return(result); }
public async Task <ResponseMessage <VmTicket> > Handle(VmOperationExecuteCommand request, CancellationToken cancellationToken) { var result = new ResponseMessage <VmTicket>(); try { var ticket = await _repository.GetId(request.VmOperationRequestMessage.TicketId); if (ticket == null) { throw new Core.NotFoundException($"{nameof(VmOperationExecuteCommand)}", request.VmOperationRequestMessage.TicketId); } if (!Enum.TryParse(ticket.Operation, out Entities.Enums.VmOperatioType vmOperatioType) || vmOperatioType == Entities.Enums.VmOperatioType.Unknown) { result.Message = string.IsNullOrWhiteSpace(ticket.Operation) ? $"{nameof(ticket.Operation)} is missing" : $"Unsupported type of {nameof(ticket.Operation)} {ticket.Operation}"; return(result); } ticket.Status = Status.Processing.ToString(); ticket.VmState = $"{ticket.Operation} in Progress"; if (await _repository.UpdateAsync(ticket, _userId) == null) { return(RetunFailed(result)); } await _vmTicketHistoryRepository.Create(ticket, nameof(VmOperationExecuteCommand)); if (vmOperatioType == Entities.Enums.VmOperatioType.Restart) { var reb = await _vmSdkService.RestartVmAndWaitForConfirmation(ticket.SubcriptionId, ticket.ResorceGroup, ticket.VmName).ConfigureAwait(false); if (reb.success) { ticket.Status = Status.Completed.ToString(); ticket.VmState = reb.status; } else { ticket.Status = Status.Failed.ToString(); ticket.VmState = reb.errorMessage; } } else if (vmOperatioType == Entities.Enums.VmOperatioType.Start) { var reb = await _vmSdkService.StartVmAndWaitForConfirmation(ticket.SubcriptionId, ticket.ResorceGroup, ticket.VmName).ConfigureAwait(false); if (reb.success) { ticket.Status = Status.Completed.ToString(); ticket.VmState = reb.status; } else { ticket.Status = Status.Failed.ToString(); ticket.VmState = reb.errorMessage; } } else if (vmOperatioType == Entities.Enums.VmOperatioType.Stop) { var reb = await _vmSdkService.StopVmAndWaitForConfirmation(ticket.SubcriptionId, ticket.ResorceGroup, ticket.VmName).ConfigureAwait(false); if (reb.success) { ticket.Status = Status.Completed.ToString(); ticket.VmState = reb.status; } else { ticket.Status = Status.Failed.ToString(); ticket.VmState = reb.errorMessage; } } if (await _repository.UpdateAsync(ticket, _userId) == null) { return(RetunFailed(result)); } await _vmTicketHistoryRepository.Create(ticket, nameof(VmOperationExecuteCommand)); result.Success = true; result.ReturnedObject = ticket; } catch (Exception ex) { //this shouldn't stop the API from doing else so this can be logged _logger.LogError($"{nameof(VmOperationExecuteCommandHandler)} failed due to: {ex.Message}"); } return(result);