-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for templating
on_failure_callback
(#252)
Add support for templating in `on_failure_callback`. Requester is specifically looking to support templated parameters that do not need to be stored in a `.py` file. Here's an example (from the issue submitted by @matveykortsev) for reference: ``` from airflow.providers.slack.notifications.slack import send_slack_notification 'on_failure_callback': [ send_slack_notification( slack_conn_id='slack', text=""" 🔴 Task Failed. *Task*: {{ ti.task_id }} *Dag*: {{ ti.dag_id }} *Execution Time*: {{ ti.execution_date }} *Log Url*: {{ ti.log_url }} """, channel="analytics-alerts", username="Airflow", ) ], ``` Closes: #209 --------- Co-authored-by: Pankaj Singh <[email protected]>
- Loading branch information
1 parent
1c999f5
commit cce8f05
Showing
12 changed files
with
310 additions
and
16 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
Empty file.
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,16 @@ | ||
""" | ||
failure.py | ||
Create a callable that intentionally "fails". | ||
Author: Jake Roach | ||
Date: 2024-10-22 | ||
""" | ||
|
||
|
||
def succeeding_task(): | ||
print("Task has executed successfully!") | ||
|
||
|
||
def failing_task(): | ||
raise Exception("Intentionally failing this Task to trigger on_failure_callback.") |
Empty file.
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,11 @@ | ||
""" | ||
example_callbacks.py | ||
Author: Jake Roach | ||
Date: 2024-10-22 | ||
""" | ||
|
||
|
||
def output_message(context, param1, param2): | ||
print("A callback has been raised!") | ||
print(f"{param1} ---------- {param2}") |
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,17 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
# The following import is here so Airflow parses this file | ||
# from airflow import DAG | ||
import dagfactory | ||
|
||
DEFAULT_CONFIG_ROOT_DIR = "/usr/local/airflow/dags/" | ||
CONFIG_ROOT_DIR = Path(os.getenv("CONFIG_ROOT_DIR", DEFAULT_CONFIG_ROOT_DIR)) | ||
|
||
config_file = str(CONFIG_ROOT_DIR / "example_callbacks.yml") | ||
|
||
example_dag_factory = dagfactory.DagFactory(config_file) | ||
|
||
# Creating task dependencies | ||
example_dag_factory.clean_dags(globals()) | ||
example_dag_factory.generate_dags(globals()) |
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,28 @@ | ||
example_callbacks: | ||
default_args: | ||
start_date: "2024-01-01" | ||
on_failure_callback: | ||
callback: airflow.providers.slack.notifications.slack.send_slack_notification | ||
slack_conn_id: slack_conn_id | ||
text: | | ||
:red_circle: Task Failed. | ||
This task has failed and needs to be addressed. | ||
Please remediate this issue ASAP. | ||
channel: "#channel" | ||
schedule_interval: "@daily" | ||
catchup: False | ||
on_failure_callback: | ||
callback: customized.callbacks.custom_callbacks.output_message | ||
param1: param1 | ||
param2: param2 | ||
tasks: | ||
start: | ||
operator: airflow.operators.python.PythonOperator | ||
python_callable_file: $CONFIG_ROOT_DIR/customized/callables/python.py | ||
python_callable_name: succeeding_task | ||
end: | ||
operator: airflow.operators.python.PythonOperator | ||
python_callable_file: $CONFIG_ROOT_DIR/customized/callables/python.py | ||
python_callable_name: failing_task | ||
dependencies: | ||
- start |
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 +1,2 @@ | ||
# Astro Runtime includes the following pre-installed providers packages: https://www.astronomer.io/docs/astro/runtime-image-architecture#provider-packages | ||
apache-airflow-providers-slack |
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.