RuntimeTaskInstance Context Does Not Contain `log_url`

by ADMIN 55 views

Introduction

Apache Airflow is a powerful workflow management platform that enables users to create, schedule, and monitor complex workflows. However, in certain scenarios, the Task Context may not contain the log_url parameter, which can cause issues with SmtpNotifier when using on_failure_callback, on_success_callback, etc. In this article, we will explore the issue, its implications, and potential solutions.

Problem Description

The Task Context does not contain the log_url parameter, which is required by SmtpNotifier in the jinja2 email template. This causes SmtpNotifier to fail when sending emails using on_failure_callback, on_success_callback, etc.

Symptoms

When creating a task with the following code:

@task()
def send_email(**context):
    ti: RuntimeTaskInstance = context['ti']

    smtp = SmtpNotifier(to=to_email, smtp_conn_id="SMTP", from_email=from_email')
    smtp.notify(context)

The error logs will display the following message:

[2025-05-08, 01:28:34] ERROR - Task failed with exception: source="task"
UndefinedError: 'airflow.sdk.execution_time.task_runner.RuntimeTaskInstance object' has no attribute 'log_url'
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 838 in run
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 1130 in _execute_task
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/operator.py", line 408 in wrapper
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/decorator.py", line 251 in execute
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/operator.py", line 408 in wrapper
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/standard/operators/python.py", line 212 in execute
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/standard/operators/python.py", line 235 in execute_callable
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/callback_runner.py", line 81 in run
File "/opt/airflow/dags/Airflow_Cleanup.py", line 98 in send_email
File "/opt/airflow/plugins/notifier/SMTP.py", line 149 in notify
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 121 in _do_render_template_fields
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 177 in render_template
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/notifier.py", line 57 in _render
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 133 in _renderFile "/home/airflow/.local/lib/python3.12/site-packages/airflow/utils/helpers.py", line 244 in render_template_to_string
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/utils/helpers.py", line 239 in render_template
File "<template>", line 37 in root
File "/home/airflow/.local/lib/python3.12/site-packages/jinja2/runtime.py", line 859 in _fail_with_undefined_error

Cause

The Task Context does not contain the log_url parameter, which is required by SmtpNotifier in the jinja2 email template.

Solution

There are two potential solutions to this issue:

  1. Update the Task Context to include log_url: The Task Context should include the log_url parameter, which can be achieved by updating the task code to include the log_url parameter.
  2. Update the jinja2 email template to remove log_url or use a different way to include log_url: The jinja2 email template can be updated to remove the log_url parameter or use a different way to include log_url.

Conclusion

In conclusion, the Task Context does not contain the log_url parameter, which can cause issues with SmtpNotifier when using on_failure_callback, on_success_callback, etc. The issue can be resolved by updating the Task Context to include log_url or updating the jinja2 email template to remove log_url or use a different way to include log_url.

Additional Information

  • Apache Airflow version: 3.0.0
  • Operating System: Linux
  • Versions of Apache Airflow Providers: apache-airflow-providers-common-compat==1.6.1
  • Deployment: Other Docker-based deployment
  • Deployment details: Custom Docker image using official Docker images as base, https://github.com/jnstockley/airflow/blob/main/Dockerfile

Are you willing to submit PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

Q&A

Q: What is the issue with the Task Context not containing log_url?

A: The Task Context does not contain the log_url parameter, which is required by SmtpNotifier in the jinja2 email template. This causes SmtpNotifier to fail when sending emails using on_failure_callback, on_success_callback, etc.

Q: What is the error message displayed when the issue occurs?

A: The error message displayed is:

[2025-05-08, 01:28:34] ERROR - Task failed with exception: source="task"
UndefinedError: 'airflow.sdk.execution_time.task_runner.RuntimeTaskInstance object' has no attribute 'log_url'
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 838 in run
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 1130 in _execute_task
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/operator.py", line 408 in wrapper
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/decorator.py", line 251 in execute
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/operator.py", line 408 in wrapper
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/standard/operators/python.py", line 212 in execute
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/standard/operators/python.py", line 235 in execute_callable
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/callback_runner.py", line 81 in run
File "/opt/airflow/dags/Airflow_Cleanup.py", line 98 in send_email
File "/opt/airflow/plugins/notifier/SMTP.py", line 149 in notify
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 121 in _do_render_template_fields
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 177 in render_template
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/bases/notifier.py", line 57 in _render
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/_internal/templater.py", line 133 in _renderFile "/home/airflow/.local/lib/python3.12/site-packages/airflow/utils/helpers.py", line 244 in render_template_to_string
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/utils/helpers.py", line 239 in render_template
File "<template>", line 37 in root
File "/home/airflow/.local/lib/python3.12/site-packages/jinja2/runtime.py", line 859 in _fail_with_undefined_error

Q: What are the potential solutions to this issue?

A: There are two solutions to this issue:

  1. Update the Task Context to include log_url: The Task Context should include the log_url parameter, which can be achieved by updating the task code to include the log_url parameter.
  2. Update the jinja2 email template to remove log_url or use a different way to include log_url: The jinja2 email template can be updated to remove the log_url parameter or use a different way to include log_url.

Q: What is the Apache Airflow version affected by this issue?

A: The Apache Airflow version affected by this issue is 3.0.0.

Q: What is the operating system affected by this issue?

A: The operating system affected by this issue is Linux.

Q: What are the versions of Apache Airflow Providers affected by this issue?

A: The versions of Apache Airflow Providers affected by this issue are apache-airflow-providers-common-compat==1.6.1.

Q: What is the deployment method affected by this issue?

A: The deployment method affected by this issue is Other Docker-based deployment.

Q: What are the deployment details affected by this issue?

A: The deployment details affected by this issue are Custom Docker image using official Docker images as base, https://github.com/jnstockley/airflow/blob/main/Dockerfile.

Q: Are you willing to submit a PR to resolve this issue?

A: [ ] Yes I am willing to submit a PR!

Q: Do you agree to follow this project's Code of Conduct?

A: [x] I agree to follow this project's Code of Conduct.