-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Use execution_date to check for existing DagRun for TriggerDagRunOperator
#18968
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
Merged
+78
−22
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fcc8092
change DagRun find in trigger_dag to use execution_date instead of ru…
gulshngill 237b67a
updated dag_run in trigger_dagrun to find using exec_date
gulshngill ac91fe5
use self.execution
gulshngill 118f0ad
formatted code
gulshngill c02375e
added find_duplicate to dagrun, updated trigger_dag and unit tests
gulshngill 60a9734
fixed return type of find_duplicate, reformatted code
gulshngill ca0802d
reverted DagRun.find in trigger_dagrun to use run_id again instead of…
gulshngill 5d2f6a3
changed method to classmethod, improved description
gulshngill e9105bc
removed empty line
gulshngill 438802e
changed dagrun.find from staticmethod to classmethod
gulshngill 8a7dcdc
changed dagrun.find from staticmethod to classmethod
gulshngill 497d285
updated var name
gulshngill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,12 +265,13 @@ def next_dagruns_to_examine( | |
| query.limit(max_number), of=cls, session=session, **skip_locked(session=session) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| @classmethod | ||
| @provide_session | ||
| def find( | ||
| cls, | ||
| dag_id: Optional[Union[str, List[str]]] = None, | ||
| run_id: Optional[str] = None, | ||
| execution_date: Optional[datetime] = None, | ||
| execution_date: Optional[Union[datetime, List[datetime]]] = None, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR but noticed that this did not match the type in the docstring
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, why not |
||
| state: Optional[DagRunState] = None, | ||
| external_trigger: Optional[bool] = None, | ||
| no_backfills: bool = False, | ||
|
|
@@ -304,35 +305,65 @@ def find( | |
| :param execution_end_date: dag run that was executed until this date | ||
| :type execution_end_date: datetime.datetime | ||
| """ | ||
| DR = DagRun | ||
|
|
||
| qry = session.query(DR) | ||
| qry = session.query(cls) | ||
| dag_ids = [dag_id] if isinstance(dag_id, str) else dag_id | ||
| if dag_ids: | ||
| qry = qry.filter(DR.dag_id.in_(dag_ids)) | ||
| qry = qry.filter(cls.dag_id.in_(dag_ids)) | ||
| if run_id: | ||
| qry = qry.filter(DR.run_id == run_id) | ||
| qry = qry.filter(cls.run_id == run_id) | ||
| if execution_date: | ||
| if isinstance(execution_date, list): | ||
| qry = qry.filter(DR.execution_date.in_(execution_date)) | ||
| qry = qry.filter(cls.execution_date.in_(execution_date)) | ||
| else: | ||
| qry = qry.filter(DR.execution_date == execution_date) | ||
| qry = qry.filter(cls.execution_date == execution_date) | ||
| if execution_start_date and execution_end_date: | ||
| qry = qry.filter(DR.execution_date.between(execution_start_date, execution_end_date)) | ||
| qry = qry.filter(cls.execution_date.between(execution_start_date, execution_end_date)) | ||
| elif execution_start_date: | ||
| qry = qry.filter(DR.execution_date >= execution_start_date) | ||
| qry = qry.filter(cls.execution_date >= execution_start_date) | ||
| elif execution_end_date: | ||
| qry = qry.filter(DR.execution_date <= execution_end_date) | ||
| qry = qry.filter(cls.execution_date <= execution_end_date) | ||
| if state: | ||
| qry = qry.filter(DR.state == state) | ||
| qry = qry.filter(cls.state == state) | ||
| if external_trigger is not None: | ||
| qry = qry.filter(DR.external_trigger == external_trigger) | ||
| qry = qry.filter(cls.external_trigger == external_trigger) | ||
| if run_type: | ||
| qry = qry.filter(DR.run_type == run_type) | ||
| qry = qry.filter(cls.run_type == run_type) | ||
| if no_backfills: | ||
| qry = qry.filter(DR.run_type != DagRunType.BACKFILL_JOB) | ||
| qry = qry.filter(cls.run_type != DagRunType.BACKFILL_JOB) | ||
|
|
||
| return qry.order_by(cls.execution_date).all() | ||
|
|
||
| @classmethod | ||
| @provide_session | ||
| def find_duplicate( | ||
| cls, | ||
| dag_id: str, | ||
| run_id: str, | ||
| execution_date: datetime, | ||
| session: Session = None, | ||
| ) -> Optional['DagRun']: | ||
| """ | ||
| Return an existing run for the DAG with a specific run_id or execution_date. | ||
|
|
||
| return qry.order_by(DR.execution_date).all() | ||
| *None* is returned if no such DAG run is found. | ||
|
|
||
| :param dag_id: the dag_id to find duplicates for | ||
| :type dag_id: str | ||
| :param run_id: defines the run id for this dag run | ||
| :type run_id: str | ||
| :param execution_date: the execution date | ||
| :type execution_date: datetime.datetime | ||
| :param session: database session | ||
| :type session: sqlalchemy.orm.session.Session | ||
| """ | ||
| return ( | ||
| session.query(cls) | ||
| .filter( | ||
| cls.dag_id == dag_id, | ||
| or_(cls.run_id == run_id, cls.execution_date == execution_date), | ||
| ) | ||
| .one_or_none() | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def generate_run_id(run_type: DagRunType, execution_date: datetime) -> str: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uranusjr , I'm thinking while we're updating this file, should we also convert this function into a
classmethod?