You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "manugarri (via GitHub)" <gi...@apache.org> on 2024/10/22 11:50:52 UTC
[D] How to stack multiple custom task decorators to add custom callbacks? [airflow]
GitHub user manugarri created a discussion: How to stack multiple custom task decorators to add custom callbacks?
hey folks, im in desperate need of some help. Working on 2.7.2, i implemented a wrapper on top of the @task decorator that adds callbacks to a function. The callbacks are tied to a particular product and send logs to our internal logging queue
I have something like this, and it works:
```
from airflow.decorators import task
from functools import partial
def airflow_task_with_status_logging(product: str, **kwargs):
"""
Wrapper around airflow task decorator that supports status and a product for logs.
"""
return partial(
task,
on_execute_callback=partial(send_internal_task_status_log,
status="start",
product=product),
on_retry_callback=partial(send_internal_task_status_log,
status="retry",
product=product),
on_success_callback=partial(send_internal_task_status_log,
status="success",
product=product),
on_failure_callback=partial(send_internal_task_status_log,
status="failure",
product=product),
**kwargs
)
```
So if you have a `foo` function, you can get automated logging like this:
```
@airflow_task_with_status_logging(product="product1")
def foo():
return 1
```
Now Im working to add another wrapper that implements a callback to send slack messages.
The idea is that you could add both decorators to a python function and send messages both to our message queue and to a particular slack channel.
The question I have is, how can i append to the callbacks added by the previous decorator? I tried a bunch of stuff and im getting airflow errors every time.
For example, stacking the same decorator twice like this:
```
@airflow_task_with_status_logging(product="product1")
@airflow_task_with_status_logging(product="product2")
def foo():
return 1
```
Throws an exception:
```
Traceback (most recent call last):
File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/models/dagbag.py", line 342, in parse
loader.exec_module(new_module)
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/usr/local/airflow/dags/local-test-dags/test.py", line 124, in <module>
@airflow_task_with_status_logging(product="product1")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/decorators/python.py", line 72, in python_task
return task_decorator_factory(
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/decorators/base.py", line 626, in task_decorator_factory
decorator = _TaskDecorator(
^^^^^^^^^^^^^^^
File "<attrs generated init airflow.decorators.base._TaskDecorator>", line 19, in __init__
self.__attrs_post_init__()
File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/decorators/base.py", line 338, in __attrs_post_init__
self.kwargs.setdefault("task_id", self.function.__name__)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_TaskDecorator' object has no attribute '__name__'
```
Any help would be more than welcome, im having a hard time figuring this one out :)
GitHub link: https://github.com/apache/airflow/discussions/43261
----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org