-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event triggers function operator (#17)
* trying something out * actually save the dag * base commit * working with event now * add example DAGs * allowing function to pick downstream DAG * Function triggers event listener working * change error message * correct file name * remove unused packages Co-authored-by: Dylan Storey <dylan.storey@astronomer.io>
- Loading branch information
1 parent
8e365bb
commit f4e3bb0
Showing
20 changed files
with
307 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
airflow_provider_kafka/operators/event_triggers_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from typing import Any, Dict, Optional, Sequence | ||
|
||
from airflow.models import BaseOperator | ||
|
||
from airflow_provider_kafka.triggers.await_message import AwaitMessageTrigger | ||
|
||
VALID_COMMIT_CADENCE = {"never", "end_of_batch", "end_of_operator"} | ||
|
||
|
||
class EventTriggersFunctionOperator(BaseOperator): | ||
"""AwaitKafkaMessageOperator An Airflow operator that defers until a specific message is published to Kafka. | ||
The behavior of the consumer for this trigger is as follows: | ||
- poll the Kafka topics for a message | ||
- if no message returned, sleep | ||
- process the message with provided callable and commit the message offset | ||
- if callable returns any data, raise a TriggerEvent with the return data | ||
- else continue to next message | ||
- return event (as default xcom or specific xcom key) | ||
:param topics: Topics (or topic regex) to use for reading from | ||
:type topics: Sequence[str] | ||
:param apply_function: The functoin to apply to messages to determine if an event occurred. As a dot | ||
notation string. | ||
:type apply_function: str | ||
:param apply_function_args: Arguments to be applied to the processing function, defaults to None | ||
:type apply_function_args: Optional[Sequence[Any]], optional | ||
:param apply_function_kwargs: Key word arguments to be applied to the processing function,, defaults to None | ||
:type apply_function_kwargs: Optional[Dict[Any, Any]], optional | ||
:param kafka_conn_id: The airflow connection storing the Kafka broker address, defaults to None | ||
:type kafka_conn_id: Optional[str], optional | ||
:param kafka_config: the config dictionary for the kafka client (additional information available on the | ||
confluent-python-kafka documentation), defaults to None | ||
:type kafka_config: Optional[Dict[Any, Any]], optional | ||
:param poll_timeout: How long the kafka consumer should wait for a message to arrive from the kafka cluster, | ||
defaults to 1 | ||
:type poll_timeout: float, optional | ||
:param poll_interval: How long the kafka consumer should sleep after reaching the end of the Kafka log, | ||
defaults to 5 | ||
:type poll_interval: float, optional | ||
:param xcom_push_key: the name of a key to push the returned message to, defaults to None | ||
:type xcom_push_key: _type_, optional | ||
""" | ||
|
||
BLUE = "#ffefeb" | ||
ui_color = BLUE | ||
|
||
template_fields = ( | ||
"topics", | ||
"apply_function", | ||
"apply_function_args", | ||
"apply_function_kwargs", | ||
) | ||
|
||
def __init__( | ||
self, | ||
topics: Sequence[str], | ||
apply_function: str, | ||
event_triggered_function: callable, | ||
apply_function_args: Optional[Sequence[Any]] = None, | ||
apply_function_kwargs: Optional[Dict[Any, Any]] = None, | ||
kafka_conn_id: Optional[str] = None, | ||
kafka_config: Optional[Dict[Any, Any]] = None, | ||
poll_timeout: float = 1, | ||
poll_interval: float = 5, | ||
**kwargs: Any, | ||
) -> None: | ||
|
||
super().__init__(**kwargs) | ||
|
||
self.topics = topics | ||
self.apply_function = apply_function | ||
self.apply_function_args = apply_function_args | ||
self.apply_function_kwargs = apply_function_kwargs | ||
self.kafka_conn_id = kafka_conn_id | ||
self.kafka_config = kafka_config | ||
self.poll_timeout = poll_timeout | ||
self.poll_interval = poll_interval | ||
self.event_triggered_function = event_triggered_function | ||
|
||
if not callable(self.event_triggered_function): | ||
raise TypeError( | ||
f"parameter event_triggered_function is expected to be of type callable, got {type(event_triggered_function)}" | ||
) | ||
|
||
def execute(self, context, event=None) -> Any: | ||
|
||
self.defer( | ||
trigger=AwaitMessageTrigger( | ||
topics=self.topics, | ||
apply_function=self.apply_function, | ||
apply_function_args=self.apply_function_args, | ||
apply_function_kwargs=self.apply_function_kwargs, | ||
kafka_conn_id=self.kafka_conn_id, | ||
kafka_config=self.kafka_config, | ||
poll_timeout=self.poll_timeout, | ||
poll_interval=self.poll_interval, | ||
), | ||
method_name="execute_complete", | ||
) | ||
|
||
return event | ||
|
||
def execute_complete(self, context, event=None): | ||
|
||
self.event_triggered_function(event, **context) | ||
|
||
self.defer( | ||
trigger=AwaitMessageTrigger( | ||
topics=self.topics, | ||
apply_function=self.apply_function, | ||
apply_function_args=self.apply_function_args, | ||
apply_function_kwargs=self.apply_function_kwargs, | ||
kafka_conn_id=self.kafka_conn_id, | ||
kafka_config=self.kafka_config, | ||
poll_timeout=self.poll_timeout, | ||
poll_interval=self.poll_interval, | ||
), | ||
method_name="execute_complete", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
ARG IMAGE_NAME="quay.io/astronomer/ap-airflow:2.2.3" | ||
ARG IMAGE_NAME="quay.io/astronomer/ap-airflow:2.4.3" | ||
FROM ${IMAGE_NAME} | ||
|
||
USER root | ||
COPY airflow_provider_kafka ${AIRFLOW_HOME}/airflow_provider_kafka | ||
COPY setup.cfg ${AIRFLOW_HOME}/airflow_provider_kafka/setup.cfg | ||
COPY setup.py ${AIRFLOW_HOME}/airflow_provider_kafka/setup.py | ||
|
||
RUN pip install ${AIRFLOW_HOME}/airflow_provider_kafka[dev] | ||
RUN pip install -e "${AIRFLOW_HOME}/airflow_provider_kafka[dev]" | ||
USER astro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.