airflow.providers.google.cloud.sensors.pubsub

This module contains a Google PubSub sensor.

Exceptions

PubSubMessageTransformException

Raise when messages failed to convert pubsub received format.

Classes

PubSubPullSensor

Pulls messages from a PubSub subscription and passes them through XCom.

Module Contents

exception airflow.providers.google.cloud.sensors.pubsub.PubSubMessageTransformException[source]

Bases: Exception

Raise when messages failed to convert pubsub received format.

class airflow.providers.google.cloud.sensors.pubsub.PubSubPullSensor(*, project_id, subscription, max_messages=5, return_immediately=None, ack_messages=False, gcp_conn_id='google_cloud_default', messages_callback=None, impersonation_chain=None, poke_interval=10.0, deferrable=conf.getboolean('operators', 'default_deferrable', fallback=False), **kwargs)[source]

Bases: airflow.providers.common.compat.sdk.BaseSensorOperator

Pulls messages from a PubSub subscription and passes them through XCom.

Always waits for at least one message to be returned from the subscription.

See also

For more information on how to use this operator, take a look at the guide: Pulling messages from a PubSub subscription

See also

If you don’t want to wait for at least one message to come, use the operator with return_immediately=True and deferrable=False instead: PubSubPullOperator

This sensor operator will pull up to max_messages messages from the specified PubSub subscription. When the subscription returns messages, the poke method’s criteria will be fulfilled and the messages will be returned from the operator and passed through XCom for downstream tasks.

If ack_messages is set to True, messages will be immediately acknowledged before being returned, otherwise, downstream tasks will be responsible for acknowledging them.

If you want a non-blocking task that does not wait for messages, please use PubSubPullOperator with return_immediately=True and deferrable=False instead.

project_id and subscription are templated so you can use variables in them.

Parameters:
  • project_id (str) – the Google Cloud project ID for the subscription (templated)

  • subscription (str) – the Pub/Sub subscription name. Do not include the full subscription path.

  • max_messages (int) – The maximum number of messages to retrieve per PubSub pull request

  • return_immediately (bool | None) – Defaults to True, which uses the deprecated Pub/Sub returnImmediately Pull option and can return zero messages even if there are messages in the backlog. If set to False, the system will instead wait (for a bounded amount of time) until at least one message is available, rather than returning no messages. The default will change to False in the first Google provider major release after March 31, 2027.

  • ack_messages (bool) – If True, each message will be acknowledged immediately rather than by any downstream tasks

  • gcp_conn_id (str) – The connection ID to use connecting to Google Cloud.

  • messages_callback (collections.abc.Callable[[list[google.cloud.pubsub_v1.types.ReceivedMessage], airflow.providers.common.compat.sdk.Context], Any] | None) – (Optional) Callback to process received messages. Its return value will be saved to XCom. If you are pulling large messages, you probably want to provide a custom callback. If not provided, the default implementation will convert ReceivedMessage objects into JSON-serializable dicts using google.protobuf.json_format.MessageToDict function.

  • impersonation_chain (str | collections.abc.Sequence[str] | None) – 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).

  • deferrable (bool) – Run sensor in deferrable mode

template_fields: collections.abc.Sequence[str] = ('project_id', 'subscription', 'impersonation_chain', 'gcp_conn_id')[source]
ui_color = '#ff7f50'[source]
gcp_conn_id = 'google_cloud_default'[source]
project_id[source]
subscription[source]
max_messages = 5[source]
return_immediately = None[source]
ack_messages = False[source]
messages_callback = None[source]
impersonation_chain = None[source]
deferrable[source]
poke_interval = 10.0[source]
poke(context)[source]

Override when deriving this class.

execute(context)[source]

Airflow runs this method on the worker and defers using the triggers if deferrable is True.

execute_complete(context, event)[source]

If messages_callback is provided, execute it; otherwise, return immediately with trigger event message.

Was this entry helpful?