Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Robust Support for Callbacks at Task and TaskGroup Level #322

Merged
merged 32 commits into from
Jan 9, 2025

Conversation

jroach-astronomer
Copy link
Member

@jroach-astronomer jroach-astronomer commented Dec 17, 2024

Pull request to address #253, which aims to provide enhanced support for callback types at the DAG, default_args, Task and TaskGroup level. There are now four different ways that callbacks can be defined via dag-factory. Below, you'll also see an example of each.

  1. By passing a string that points to a callable.
  2. By passing a file path and file name that points to a callable, similar to above.
  3. By providing a string that points to a callable, but with some parameters being provided at runtime.
  4. Using callbacks from providers (like Slack).

Passing a string that points to a callable

...
  task_groups:
    task_group_1:
      default_args:
        on_success_callback: print_hello.print_hello_from_callback
      dependencies: [task_1, task_2]
...

Passing a file path and file name that points to a callable

...
    task_2:
      operator: airflow.operators.bash_operator.BashOperator
      bash_command: "echo 2"
      on_success_callback_name: print_hello_from_callback
      on_success_callback_file: $CONFIG_ROOT_DIR/print_hello.py
      dependencies: [start]
...

By providing a string that points to a callable, but with some parameters being provided at runtime

...
  on_failure_callback:
    callback: customized.callbacks.custom_callbacks.output_message
    param1: param1
    param2: param2
...

Using callbacks from providers

...
    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"
...

Unit-tests have been added and updated appropriately.

@jroach-astronomer jroach-astronomer requested a review from a team as a code owner December 17, 2024 04:21
@codecov-commenter
Copy link

codecov-commenter commented Dec 18, 2024

Codecov Report

Attention: Patch coverage is 93.10345% with 2 lines in your changes missing coverage. Please review.

Project coverage is 93.69%. Comparing base (442c824) to head (317b35e).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
dagfactory/dagbuilder.py 93.10% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #322   +/-   ##
=======================================
  Coverage   93.68%   93.69%           
=======================================
  Files          10       10           
  Lines         792      777   -15     
=======================================
- Hits          742      728   -14     
+ Misses         50       49    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jroach-astronomer
Copy link
Member Author

@pankajastro, do you mind reviewing this, open to feedback?

cc: @tatiana

@tatiana tatiana added this to the DAG Factory 0.22.0 milestone Dec 30, 2024
@pankajkoti pankajkoti self-requested a review January 2, 2025 09:50
@pankajkoti
Copy link
Contributor

pankajkoti commented Jan 6, 2025

@jroach-astronomer please let me know once you'd like me to re-review/test the pull request. Could you please also update the PR description after the changes especially with the example for on_success_callback: $CONFIG_ROOT_DIR.print_hello.print_hello_from_callback

There seem to be a couple of conflicts, would be nice if you could resolve them too. We're planning on releasing DAG Factory on 10th Jan, so would be nice if we could get this change in by then.

@jroach-astronomer
Copy link
Member Author

@pankajkoti, resolved MC's, updated PR docs.

@jroach-astronomer jroach-astronomer changed the title Issue 253 Add Robust Support for Callbacks at Task and TaskGroup Level Jan 8, 2025
Copy link
Contributor

@pankajkoti pankajkoti left a comment

Choose a reason for hiding this comment

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

This looks fantastic! Thank you for all the refactoring and for addressing my earlier questions and comments. I’ve shared an in-line thought in #322 (comment) about potentially dropping support for the file+name approach of specifying callbacks. It would be great to revisit this idea later, and we can plan to work on it in the next release since it’s outside the scope of this PR.

dag_params["default_args"], "on_success_callback_file"
):
# Then, check at the DAG-level for a file path and name
if utils.check_dict_key(dag_params, f"{callback_type}_name") and utils.check_dict_key(
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m wondering if we should continue supporting the {callback_type}_name + {callback_type}_file approach for callbacks. Are there scenarios where simply specifying {callback_type} might be insufficient or lack necessary support?

Unifying the approach to a single, straightforward method seems more maintainable and less confusing for users. Additionally, what happens if a user specifies both on_success_callback and on_success_callback_name + on_success_callback_file? Currently, it appears that on_success_callback_name + on_success_callback_file will override the on_success_callback, which isn’t transparent to the end user and could cause confusion.

While this isn’t part of your changes and has been a pre-existing issue, it might be worth discussing whether we could drop support for the name+file approach altogether or at least an error when users try to provide callbacks using both approaches. What do you think?

@pankajkoti pankajkoti merged commit 39c813e into astronomer:main Jan 9, 2025
68 checks passed
@pankajkoti pankajkoti mentioned this pull request Jan 10, 2025
tatiana pushed a commit that referenced this pull request Jan 10, 2025
### Added

- Propagate provided dag_display_name to built dag by @pankajkoti in
#326
- Add incipient documentation tooling by @tatiana in #328
- Support loading `default_args` from shared `defaults.yml` by
@pankajastro in #330
- Add security policy by @tatiana in #339
- Add Robust Support for Callbacks at Task and TaskGroup Level by
@@jroach-astronomer in #322
- Support `ExternalTaskSensor` `execution_date_fn` and `execution_delta`
by @tatiana in #354
- Refactor and add support for schedule conditions in DAG configuration
by @ErickSeo in #320

### Fixed

- Handle gracefully exceptions during telemetry collection by @tatiana
in #335
- Adjust `markdownlint` configuration to enforce 4-space indentation for
proper `mkdocs` rendering by @pankajkoti in #345

### Docs

- Create initial documentation index by @tatiana in #325
- Use absolute URLs for failing links in docs/index.md by @pankajkoti in
#331
- Add quick start docs by @pankajastro in #324
- Add docs comparing Python and YAML-based DAGs by @tatiana in #327
- Add docs about project contributors and their roles by @tatiana in
#341
- Add documentation to support developers by @tatiana in #343
- Add docs for configuring workflows, environment variables and defaults
by @pankajkoti in #338
- Add code of conduct for contributors and DAG factory community by
@tatiana in #340
- Document Dynamic Task Mapping feature by @pankajkoti in #344
- Fix warning message 404 in code_of_conduct docs by @pankajastro in
#346
- Update theme for documentation by @pankajastro in #348
- Fix markdownlint errors and some rendering improvements by
@pankajastro in #356
- Reword content in documentation by @yanmastin-astro in #336

### Others

- Improve integration tests scripts by @tatiana in #316
- Add Markdown pre-commit checks by @tatiana in #329
- Remove Airflow <> 2.0.0 check by @pankajastro in #334
- Reduce telemetry timeout from 5 to 1 second by @tatiana in #337
- Add GH action job to deploy docs by @pankajastro in #342
- Enable Depandabot to scan outdated Github Actions dependencies by
@tatiana in #347
- Improve docs deploy job by @pankajastro in #352
- Unify how we build dagfactory by @tatiana in #353
- Fix running make docker run when previous versions were run locally by
@tatiana in #362
- Install `jq` in `dev` container by @pankajastro in #363
- Dependabot GitHub actions version upgrades in #349, #350, #351


Closes: #306
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.

4 participants