Skip to content

Do not use native obj Jinja2 env to render error emails - #22218

Closed
andyfcx wants to merge 4 commits into
apache:mainfrom
andyfcx:fix_22152
Closed

andyfcx wants to merge 4 commits into
apache:mainfrom
andyfcx:fix_22152

Conversation

@andyfcx

@andyfcx andyfcx commented Mar 13, 2022 •

Copy link
Copy Markdown

#22152

I added a flag force_string to make sure returning will be string,
Testing code still in progress.
Need more discussion.


Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

@boring-cyborg

boring-cyborg Bot commented Mar 13, 2022

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (flake8, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it’s a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

@uranusjr uranusjr changed the title Fix 22152 Do not use native obj Jinja2 env to render error emails Mar 13, 2022
dag = self.get_dag()
if dag:
return dag.get_template_env()
return dag.get_template_env(force_string=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this False?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default as False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right 😓

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this is correct though. This function is also used when the task is being executed (in render_template_fields), and in that situation we want to use NativeEnvironment if render_template_as_native_obj is True. so this get_template_env also need to have a force_string argument, and only get_email_subject_content should set it to True.

@uranusjr

uranusjr commented Mar 13, 2022 •

Copy link
Copy Markdown
Member

(Some context for this) I’m helping @andyfcx to work out a solution to #22152.

The main problem with #22152 is that, when you set render_template_as_native_obj on a DAG, it would use a NativeEnvironment to create a Jinja2 template object, which in turn will be able to be rendered into a native object. However, the email-sending mechanism also uses the same Jinja2 environment, but this environment cannot correctly create a template to render into the email’s content (which must be a string, obviously).

So the proposed solution here is to add an additional flag on get_template_env to control what environment to return. If force_string is True, get_template_env will always create a SandboxedEnvironment (which is rendered into a string), even if render_template_as_native_obj is True. A NativeEnvironment is used only if render_template_as_native_obj is True and force_string is False.

But the implementation in the PR still needs some work. I’m also not entirely convinced this is the best solution, and any suggestions are welcomed. Also at least one test will be needed.

Comment thread airflow/models/dag.py Outdated
@ashb

ashb commented Apr 7, 2022

Copy link
Copy Markdown
Member

We've merged #22770 instead.

@ashb ashb closed this Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants