コード例 #1
0
 def execute(self, context: 'Context'):
     hook = CloudTasksHook(
         gcp_conn_id=self.gcp_conn_id,
         impersonation_chain=self.impersonation_chain,
     )
     try:
         queue = hook.create_queue(
             location=self.location,
             task_queue=self.task_queue,
             project_id=self.project_id,
             queue_name=self.queue_name,
             retry=self.retry,
             timeout=self.timeout,
             metadata=self.metadata,
         )
     except AlreadyExists:
         if self.queue_name is None:
             raise RuntimeError("The queue name should be set here!")
         queue = hook.get_queue(
             location=self.location,
             project_id=self.project_id,
             queue_name=self.queue_name,
             retry=self.retry,
             timeout=self.timeout,
             metadata=self.metadata,
         )
     CloudTasksQueueLink.persist(
         operator_instance=self,
         context=context,
         queue_name=queue.name,
     )
     return Queue.to_dict(queue)
コード例 #2
0
 def execute(self, context: 'Context'):
     hook = CloudTasksHook(
         gcp_conn_id=self.gcp_conn_id,
         impersonation_chain=self.impersonation_chain,
     )
     queue = hook.resume_queue(
         location=self.location,
         queue_name=self.queue_name,
         project_id=self.project_id,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
     CloudTasksQueueLink.persist(
         operator_instance=self,
         context=context,
         queue_name=queue.name,
     )
     return Queue.to_dict(queue)
コード例 #3
0
 def execute(self, context: 'Context'):
     hook = CloudTasksHook(
         gcp_conn_id=self.gcp_conn_id,
         impersonation_chain=self.impersonation_chain,
     )
     task = hook.get_task(
         location=self.location,
         queue_name=self.queue_name,
         task_name=self.task_name,
         project_id=self.project_id,
         response_view=self.response_view,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
     CloudTasksQueueLink.persist(
         operator_instance=self,
         context=context,
         queue_name=task.name,
     )
     return Task.to_dict(task)
コード例 #4
0
 def execute(self, context: 'Context'):
     hook = CloudTasksHook(
         gcp_conn_id=self.gcp_conn_id,
         impersonation_chain=self.impersonation_chain,
     )
     tasks = hook.list_tasks(
         location=self.location,
         queue_name=self.queue_name,
         project_id=self.project_id,
         response_view=self.response_view,
         page_size=self.page_size,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
     CloudTasksQueueLink.persist(
         operator_instance=self,
         context=context,
         queue_name=f"projects/{self.project_id or hook.project_id}/"
         f"locations/{self.location}/queues/{self.queue_name}",
     )
     return [Task.to_dict(t) for t in tasks]
コード例 #5
0
class CloudTasksTasksListOperator(BaseOperator):
    """
    Lists the tasks in Cloud Tasks.

    .. seealso::
        For more information on how to use this operator, take a look at the guide:
        :ref:`howto/operator:CloudTasksTasksListOperator`

    :param location: The location name in which the tasks were created.
    :param queue_name: The queue's name.
    :param project_id: (Optional) The ID of the Google Cloud project that owns the Cloud Tasks.
        If set to None or missing, the default project_id from the Google Cloud connection is used.
    :param response_view: (Optional) This field specifies which subset of the Task will
        be returned.
    :param page_size: (Optional) The maximum number of resources contained in the
        underlying API response.
    :param retry: (Optional) A retry object used to retry requests.
        If None is specified, requests will not be retried.
    :param timeout: (Optional) The amount of time, in seconds, to wait for the request
        to complete. Note that if retry is specified, the timeout applies to each
        individual attempt.
    :param metadata: (Optional) Additional metadata that is provided to the method.
    :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud.
    :param impersonation_chain: Optional service account to impersonate using short-term
        credentials, or chained list of accounts required to get the access_token
        of the last account in the list, which will be impersonated in the request.
        If set as a string, the account must grant the originating account
        the Service Account Token Creator IAM role.
        If set as a sequence, the identities from the list must grant
        Service Account Token Creator IAM role to the directly preceding identity, with first
        account from the list granting this role to the originating account (templated).

    :rtype: list[google.cloud.tasks_v2.types.Task]
    """

    template_fields: Sequence[str] = (
        "location",
        "queue_name",
        "project_id",
        "gcp_conn_id",
        "impersonation_chain",
    )
    operator_extra_links = (CloudTasksQueueLink(), )

    def __init__(
        self,
        *,
        location: str,
        queue_name: str,
        project_id: Optional[str] = None,
        response_view: Optional[Task.View] = None,
        page_size: Optional[int] = None,
        retry: Union[Retry, _MethodDefault] = DEFAULT,
        timeout: Optional[float] = None,
        metadata: MetaData = (),
        gcp_conn_id: str = "google_cloud_default",
        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.location = location
        self.queue_name = queue_name
        self.project_id = project_id
        self.response_view = response_view
        self.page_size = page_size
        self.retry = retry
        self.timeout = timeout
        self.metadata = metadata
        self.gcp_conn_id = gcp_conn_id
        self.impersonation_chain = impersonation_chain

    def execute(self, context: 'Context'):
        hook = CloudTasksHook(
            gcp_conn_id=self.gcp_conn_id,
            impersonation_chain=self.impersonation_chain,
        )
        tasks = hook.list_tasks(
            location=self.location,
            queue_name=self.queue_name,
            project_id=self.project_id,
            response_view=self.response_view,
            page_size=self.page_size,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata,
        )
        CloudTasksQueueLink.persist(
            operator_instance=self,
            context=context,
            queue_name=f"projects/{self.project_id or hook.project_id}/"
            f"locations/{self.location}/queues/{self.queue_name}",
        )
        return [Task.to_dict(t) for t in tasks]
コード例 #6
0
class CloudTasksQueueCreateOperator(BaseOperator):
    """
    Creates a queue in Cloud Tasks.

    .. seealso::
        For more information on how to use this operator, take a look at the guide:
        :ref:`howto/operator:CloudTasksQueueCreateOperator`

    :param location: The location name in which the queue will be created.
    :param task_queue: The task queue to create.
        Queue's name cannot be the same as an existing queue.
        If a dict is provided, it must be of the same form as the protobuf message Queue.
    :param project_id: (Optional) The ID of the Google Cloud project that owns the Cloud Tasks.
        If set to None or missing, the default project_id from the Google Cloud connection is used.
    :param queue_name: (Optional) The queue's name.
        If provided, it will be used to construct the full queue path.
    :param retry: (Optional) A retry object used to retry requests.
        If None is specified, requests will not be retried.
    :param timeout: (Optional) The amount of time, in seconds, to wait for the request
        to complete. Note that if retry is specified, the timeout applies to each
        individual attempt.
    :param metadata: (Optional) Additional metadata that is provided to the method.
    :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud.
    :param impersonation_chain: Optional service account to impersonate using short-term
        credentials, or chained list of accounts required to get the access_token
        of the last account in the list, which will be impersonated in the request.
        If set as a string, the account must grant the originating account
        the Service Account Token Creator IAM role.
        If set as a sequence, the identities from the list must grant
        Service Account Token Creator IAM role to the directly preceding identity, with first
        account from the list granting this role to the originating account (templated).

    :rtype: google.cloud.tasks_v2.types.Queue
    """

    template_fields: Sequence[str] = (
        "task_queue",
        "project_id",
        "location",
        "queue_name",
        "gcp_conn_id",
        "impersonation_chain",
    )
    operator_extra_links = (CloudTasksQueueLink(), )

    def __init__(
        self,
        *,
        location: str,
        task_queue: Queue,
        project_id: Optional[str] = None,
        queue_name: Optional[str] = None,
        retry: Union[Retry, _MethodDefault] = DEFAULT,
        timeout: Optional[float] = None,
        metadata: MetaData = (),
        gcp_conn_id: str = "google_cloud_default",
        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.location = location
        self.task_queue = task_queue
        self.project_id = project_id
        self.queue_name = queue_name
        self.retry = retry
        self.timeout = timeout
        self.metadata = metadata
        self.gcp_conn_id = gcp_conn_id
        self.impersonation_chain = impersonation_chain

    def execute(self, context: 'Context'):
        hook = CloudTasksHook(
            gcp_conn_id=self.gcp_conn_id,
            impersonation_chain=self.impersonation_chain,
        )
        try:
            queue = hook.create_queue(
                location=self.location,
                task_queue=self.task_queue,
                project_id=self.project_id,
                queue_name=self.queue_name,
                retry=self.retry,
                timeout=self.timeout,
                metadata=self.metadata,
            )
        except AlreadyExists:
            if self.queue_name is None:
                raise RuntimeError("The queue name should be set here!")
            queue = hook.get_queue(
                location=self.location,
                project_id=self.project_id,
                queue_name=self.queue_name,
                retry=self.retry,
                timeout=self.timeout,
                metadata=self.metadata,
            )
        CloudTasksQueueLink.persist(
            operator_instance=self,
            context=context,
            queue_name=queue.name,
        )
        return Queue.to_dict(queue)