public override async Task <ICommandResult> HandleAsync(ComponentTaskCancelCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log) { if (command is null) { throw new ArgumentNullException(nameof(command)); } if (commandQueue is null) { throw new ArgumentNullException(nameof(commandQueue)); } var commandResult = command.CreateResult(); try { commandResult.Result = (await componentTaskRepository .GetAsync(command.Payload.ComponentId, command.Payload.Id) .ConfigureAwait(Orchestration)) ?? command.Payload; if (commandResult.Result.Type != ComponentTaskType.Custom) { throw new Exception($"Component tasks of type '{commandResult.Result.TypeName}' cannot be canceled!"); } else if (commandResult.Result.TaskState.IsFinal()) { throw new Exception($"Component tasks in state '{commandResult.Result.TaskState}' cannot be canceled!"); } else { var status = await orchestrationContext .GetCommandStatusAsync(commandResult.Result, showInput : false) .ConfigureAwait(Orchestration); if (status is not null && status.RuntimeStatus.IsActive()) { await orchestrationContext .TerminateCommandAsync(commandResult.Result, $"Canceled by user {command.User.DisplayName}") .ConfigureAwait(Orchestration); } if (AzureResourceIdentifier.TryParse(commandResult.Result.ResourceId, out var resourceId)) { var containerGroup = await azureResourceService .GetResourceAsync <AzureContainerGroupResource>(resourceId.ToString()) .ConfigureAwait(false); if (containerGroup is not null) { await containerGroup .DeleteAsync(true) .ConfigureAwait(false); } } commandResult.Result.TaskState = TaskState.Canceled; commandResult.Result.Finished = DateTime.UtcNow; commandResult.Result.ResourceId = null; commandResult.Result = await componentTaskRepository .SetAsync(commandResult.Result) .ConfigureAwait(Orchestration); } commandResult.RuntimeStatus = CommandRuntimeStatus.Completed; } catch (Exception exc) { commandResult.Errors.Add(exc); } finally { await orchestrationContext .CleanupResourceLocksAsync() .ConfigureAwait(Orchestration); } return(commandResult); }