Skip to content

cancel_task async

cancel_task(
    agent_context_id: AgentContextIdentifier,
    task_id: TaskId,
    *,
    config_overrides: Optional[AsyncContextClientConfig] = None,
) -> CancelTaskResponse

Cancels a task that is PENDING or IN_PROGRESS. The task moves to CANCELLING, and then to CANCELLED when it stops. AWS Context keeps the change sets that the task already created. Canceling a CANCELLED task returns it unchanged. Canceling a CANCELLING task returns it unchanged, unless the task's work has already stopped, in which case the cancel completes and the task is returned as CANCELLED. Canceling a COMPLETED or FAILED task fails with ConflictException.

Parameters:

Raises:

  • AccessDeniedException –

    You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.

  • InternalServerException –

    The request failed because of an internal error. Retry the request.

  • ThrottlingException –

    The request was denied because it exceeded a request rate quota. Wait, then retry the request.

  • ValidationException –

    The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.

  • ConflictException –

    The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.

  • ResourceNotFoundException –

    The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

Cancel a running task The following example cancels a verified queries import task that is in progress, which moves it to CANCELLING.

>>> await client.cancel_task(agent_context_id='ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', task_id='task-2c7f5e9a1d3b4a68b4e2f0c9d8a7e153')
Cancel a task that has already completed, which fails with ConflictException
The following example tries to cancel a task that is COMPLETED, which fails with ConflictException.
>>> await client.cancel_task(agent_context_id='ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', task_id='task-6e2b9f1c4a7d4e38b5c0a9f2d7e1b364')
Source code in docs_src/capo_context/_services/async_context.py
async def cancel_task(
    self,
    agent_context_id: "capo_context.types.agent_context_identifier.AgentContextIdentifier",
    task_id: "capo_context.types.task_id.TaskId",
    *,
    config_overrides: Optional[AsyncContextClientConfig] = None,
) -> "capo_context.types.cancel_task_response.CancelTaskResponse":
    """<p>Cancels a task that is <code>PENDING</code> or <code>IN_PROGRESS</code>. The task moves to <code>CANCELLING</code>, and then to <code>CANCELLED</code> when it stops. AWS Context keeps the change sets that the task already created. Canceling a <code>CANCELLED</code> task returns it unchanged. Canceling a <code>CANCELLING</code> task returns it unchanged, unless the task's work has already stopped, in which case the cancel completes and the task is returned as <code>CANCELLED</code>. Canceling a <code>COMPLETED</code> or <code>FAILED</code> task fails with <code>ConflictException</code>.</p>

    Args:
        agent_context_id: <p>The agent context, as an ID or ARN.</p>
        task_id: <p>The ID of the task to cancel.</p>

    Raises:
        capo_context.errors.access_denied_exception.AccessDeniedException: <p>You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.</p>
        capo_context.errors.internal_server_exception.InternalServerException: <p>The request failed because of an internal error. Retry the request.</p>
        capo_context.errors.throttling_exception.ThrottlingException: <p>The request was denied because it exceeded a request rate quota. Wait, then retry the request.</p>
        capo_context.errors.validation_exception.ValidationException: <p>The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.</p>
        capo_context.errors.conflict_exception.ConflictException: <p>The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.</p>
        capo_context.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.</p>
        capo_context.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Cancel a running task
        The following example cancels a verified queries import task that is in progress, which moves it to CANCELLING.

        >>> await client.cancel_task(agent_context_id='ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', task_id='task-2c7f5e9a1d3b4a68b4e2f0c9d8a7e153')
        Cancel a task that has already completed, which fails with ConflictException
        The following example tries to cancel a task that is COMPLETED, which fails with ConflictException.

        >>> await client.cancel_task(agent_context_id='ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', task_id='task-6e2b9f1c4a7d4e38b5c0a9f2d7e1b364')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_context.types.cancel_task_request.CancelTaskRequest]",
    ) -> AsyncOperationResponse[
        "capo_context.types.cancel_task_response.CancelTaskResponse"
    ]:
        import capo_context._operations.context.cancel_task

        (
            output,
            http_response,
        ) = await capo_context._operations.context.cancel_task.async_cancel_task(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_output(
        output: "capo_context.types.cancel_task_response.CancelTaskResponse",
    ) -> dict[str, Any]:
        import capo_context.types.cancel_task_response

        return capo_context.types.cancel_task_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("CancelTask", redact_output=_redact_output),
    )
    input_: capo_context.types.cancel_task_request.CancelTaskRequest = {
        "agent_context_id": agent_context_id,
        "task_id": task_id,
    }

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output