dagpickle.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. from typing import TYPE_CHECKING
  20. import dill
  21. from sqlalchemy import BigInteger, Column, Integer, PickleType
  22. from airflow.models.base import Base
  23. from airflow.utils import timezone
  24. from airflow.utils.sqlalchemy import UtcDateTime
  25. if TYPE_CHECKING:
  26. from airflow.models.dag import DAG
  27. class DagPickle(Base):
  28. """
  29. Represents a version of a DAG and becomes a source of truth for a BackfillJob execution.
  30. Dags can originate from different places (user repos, main repo, ...) and also get executed
  31. in different places (different executors). A pickle is a native python serialized object,
  32. and in this case gets stored in the database for the duration of the job.
  33. The executors pick up the DagPickle id and read the dag definition from the database.
  34. """
  35. id = Column(Integer, primary_key=True)
  36. pickle = Column(PickleType(pickler=dill))
  37. created_dttm = Column(UtcDateTime, default=timezone.utcnow)
  38. pickle_hash = Column(BigInteger)
  39. __tablename__ = "dag_pickle"
  40. def __init__(self, dag: DAG) -> None:
  41. self.dag_id = dag.dag_id
  42. if hasattr(dag, "template_env"):
  43. dag.template_env = None # type: ignore[attr-defined]
  44. self.pickle_hash = hash(dag)
  45. self.pickle = dag