Source code for airflow.providers.amazon.aws.sensors.sqs
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Reads and then deletes the message from SQS queue."""from__future__importannotationsfromcollections.abcimportCollection,SequencefromdatetimeimporttimedeltafromtypingimportTYPE_CHECKING,Anyfromairflow.configurationimportconffromairflow.exceptionsimportAirflowExceptionfromairflow.providers.amazon.aws.hooks.sqsimportSqsHookfromairflow.providers.amazon.aws.sensors.base_awsimportAwsBaseSensorfromairflow.providers.amazon.aws.triggers.sqsimportSqsSensorTriggerfromairflow.providers.amazon.aws.utilsimportvalidate_execute_complete_eventfromairflow.providers.amazon.aws.utils.mixinsimportaws_template_fieldsfromairflow.providers.amazon.aws.utils.sqsimportprocess_responseifTYPE_CHECKING:fromairflow.providers.amazon.aws.hooks.base_awsimportBaseAwsConnectionfromairflow.providers.amazon.aws.utils.sqsimportMessageFilteringTypefromairflow.utils.contextimportContext
[docs]classSqsSensor(AwsBaseSensor[SqsHook]):""" Get messages from an Amazon SQS queue and then delete the messages from the queue. If deletion of messages fails, an AirflowException is thrown. Otherwise, the messages are pushed through XCom with the key ``messages``. By default,the sensor performs one and only one SQS call per poke, which limits the result to a maximum of 10 messages. However, the total number of SQS API calls per poke can be controlled by num_batches param. .. seealso:: For more information on how to use this sensor, take a look at the guide: :ref:`howto/sensor:SqsSensor` :param sqs_queue: The SQS queue url (templated) :param max_messages: The maximum number of messages to retrieve for each poke (templated) :param num_batches: The number of times the sensor will call the SQS API to receive messages (default: 1) :param wait_time_seconds: The time in seconds to wait for receiving messages (default: 1 second) :param visibility_timeout: Visibility timeout, a period of time during which Amazon SQS prevents other consumers from receiving and processing the message. :param message_filtering: Specified how received messages should be filtered. Supported options are: `None` (no filtering, default), `'literal'` (message Body literal match), `'jsonpath'` (message Body filtered using a JSONPath expression), or `'jsonpath-ext'` (like `'jsonpath'`, but with an expanded query grammar). You may add further methods by overriding the relevant class methods. :param message_filtering_match_values: Optional value/s for the message filter to match on. For example, with literal matching, if a message body matches any of the specified values then it is included. For JSONPath matching, the result of the JSONPath expression is used and may match any of the specified values. :param message_filtering_config: Additional configuration to pass to the message filter. For example with JSONPath filtering you can pass a JSONPath expression string here, such as `'foo[*].baz'`. Messages with a Body which does not match are ignored. :param delete_message_on_reception: Default to `True`, the messages are deleted from the queue as soon as being consumed. Otherwise, the messages remain in the queue after consumption and should be deleted manually. :param deferrable: If True, the sensor will operate in deferrable mode. This mode requires aiobotocore module to be installed. (default: False, but can be overridden in config file by setting default_deferrable to True) :param aws_conn_id: The Airflow connection used for AWS credentials. If this is ``None`` or empty then the default boto3 behaviour is used. If running Airflow in a distributed manner and aws_conn_id is None or empty, then default boto3 configuration would be used (and must be maintained on each worker node). :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. :param verify: Whether or not to verify SSL certificates. See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html :param botocore_config: Configuration dictionary (key-values) for botocore client. See: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html """
ifself.message_filtering=="literal":ifself.message_filtering_match_valuesisNone:raiseTypeError("message_filtering_match_values must be specified for literal matching")
[docs]defexecute_complete(self,context:Context,event:dict|None=None)->None:validated_event=validate_execute_complete_event(event)ifvalidated_event["status"]!="success":raiseAirflowException(f"Trigger error: event is {validated_event}")context["ti"].xcom_push(key="messages",value=validated_event["message_batch"])
[docs]defpoll_sqs(self,sqs_conn:BaseAwsConnection)->Collection:""" Poll SQS queue to retrieve messages. :param sqs_conn: SQS connection :return: A list of messages retrieved from SQS """self.log.info("SqsSensor checking for message on queue: %s",self.sqs_queue)receive_message_kwargs={"QueueUrl":self.sqs_queue,"MaxNumberOfMessages":self.max_messages,"WaitTimeSeconds":self.wait_time_seconds,}ifself.visibility_timeoutisnotNone:receive_message_kwargs["VisibilityTimeout"]=self.visibility_timeoutresponse=sqs_conn.receive_message(**receive_message_kwargs)returnresponse
[docs]defpoke(self,context:Context):""" Check subscribed queue for messages and write them to xcom with the ``messages`` key. :param context: the context object :return: ``True`` if message is available or ``False`` """message_batch:list[Any]=[]# perform multiple SQS call to retrieve messages in seriesfor_inrange(self.num_batches):response=self.poll_sqs(sqs_conn=self.hook.conn)messages=process_response(response,self.message_filtering,self.message_filtering_match_values,self.message_filtering_config,)ifnotmessages:continuemessage_batch.extend(messages)ifself.delete_message_on_reception:self.log.info("Deleting %d messages",len(messages))entries=[{"Id":message["MessageId"],"ReceiptHandle":message["ReceiptHandle"]}formessageinmessages]response=self.hook.conn.delete_message_batch(QueueUrl=self.sqs_queue,Entries=entries)if"Successful"notinresponse:raiseAirflowException(f"Delete SQS Messages failed {response} for messages {messages}")ifmessage_batch:context["ti"].xcom_push(key="messages",value=message_batch)returnTrueelse:returnFalse