Skip to content

Triggerer: Support loading triggers from zip archives #52091

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tommalt
Copy link

@tommalt tommalt commented Jun 23, 2025


Currently airflow's scheduler supports executing DAGs defined in a zip archive. However the triggerer does not yet support this functionality. We propose these changes so that triggerer can support loading/executing triggers from a specified zip archive, similar to how scheduler does it.

The sys.modules cache is cleared of relevant libraries before performing an import, so that if there are multiple zip archives which contain the same library with different versions, the correct version will be loaded from disk instead of the global sys.modules cache.

Copy link

boring-cyborg bot commented Jun 23, 2025

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 Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, 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.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    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
Copy link
Member

Is this necessary? Python supports importing from a zip file out of the box, so as long as you structure the zip file correctly, importing the trigger class normally should just work.

https://docs.python.org/3/library/zipimport.html

@tommalt
Copy link
Author

tommalt commented Jun 24, 2025

@uranusjr Yes, it is necessary. In the documentation you linked, it reads:

it [zipimport library] is automatically used by the built-in import mechanism for sys.path items that are paths to ZIP archives.

What this means is that in order for zipimport to work, you need an entry in sys.path which points to the zip archive in question. This is demonstrated in the example, which is the same thing that I am doing here. It is also what the airflow scheduler does in order to handle zip archives:

This is how the scheduler currently handles zip files, from which I derived my triggerer implementation. Note lines 493-500.

def _load_modules_from_zip(self, filepath, safe_mode):
from airflow.sdk.definitions._internal.contextmanager import DagContext
mods = []
with zipfile.ZipFile(filepath) as current_zip_file:
for zip_info in current_zip_file.infolist():
zip_path = Path(zip_info.filename)
if zip_path.suffix not in [".py", ".pyc"] or len(zip_path.parts) > 1:
continue
if zip_path.stem == "__init__":
self.log.warning("Found %s at root of %s", zip_path.name, filepath)
self.log.debug("Reading %s from %s", zip_info.filename, filepath)
if not might_contain_dag(zip_info.filename, safe_mode, current_zip_file):
# todo: create ignore list
# Don't want to spam user with skip messages
if not self.has_logged:
self.has_logged = True
self.log.info(
"File %s:%s assumed to contain no DAGs. Skipping.", filepath, zip_info.filename
)
continue
mod_name = zip_path.stem
if mod_name in sys.modules:
del sys.modules[mod_name]
DagContext.current_autoregister_module_name = mod_name
try:
sys.path.insert(0, filepath)
current_module = importlib.import_module(mod_name)
mods.append(current_module)
except Exception as e:
DagContext.autoregistered_dags.clear()
fileloc = os.path.join(filepath, zip_info.filename)
self.log.exception("Failed to import: %s", fileloc)
relative_fileloc = self._get_relative_fileloc(fileloc)
if self.dagbag_import_error_tracebacks:
self.import_errors[relative_fileloc] = traceback.format_exc(
limit=-self.dagbag_import_error_traceback_depth
)
else:
self.import_errors[relative_fileloc] = str(e)
finally:
if sys.path[0] == filepath:
del sys.path[0]
return mods

Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

Looks legit and thanks for the explanation. @uranusjr - are you happy with it ?

@potiuk
Copy link
Member

potiuk commented Jun 24, 2025

BTW. It would be really good if there are tests added - we very rarely merge changes that have no tests, because it means that the change is not tested and is prone to regression. So I retract my approval - and "require changes" for tests

Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

Needs tests

@tommalt
Copy link
Author

tommalt commented Jun 26, 2025

@potiuk Acknowledged. I will add tests. FYI I will be on vacation these next two weeks so it'll be some time before I get to it. Thanks for the feedback 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants