Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/ti_deps/deps/not_previously_skipped_dep.py: 24%
34 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
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.
18from __future__ import annotations
20from airflow.models.taskinstance import PAST_DEPENDS_MET
21from airflow.ti_deps.deps.base_ti_dep import BaseTIDep
24class NotPreviouslySkippedDep(BaseTIDep):
25 """
26 Determine if this task should be skipped.
28 Based on any of the task's direct upstream relatives have decided this task should
29 be skipped.
30 """
32 NAME = "Not Previously Skipped"
33 IGNORABLE = True
34 IS_TASK_DEP = True
36 def _get_dep_statuses(self, ti, session, dep_context):
37 from airflow.models.skipmixin import (
38 XCOM_SKIPMIXIN_FOLLOWED,
39 XCOM_SKIPMIXIN_KEY,
40 XCOM_SKIPMIXIN_SKIPPED,
41 SkipMixin,
42 )
43 from airflow.utils.state import State
45 upstream = ti.task.get_direct_relatives(upstream=True)
47 finished_tis = dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
49 finished_task_ids = {t.task_id for t in finished_tis}
51 for parent in upstream:
52 if isinstance(parent, SkipMixin):
53 if parent.task_id not in finished_task_ids:
54 # This can happen if the parent task has not yet run.
55 continue
57 prev_result = ti.xcom_pull(task_ids=parent.task_id, key=XCOM_SKIPMIXIN_KEY, session=session)
59 if prev_result is None:
60 # This can happen if the parent task has not yet run.
61 continue
63 should_skip = False
64 if (
65 XCOM_SKIPMIXIN_FOLLOWED in prev_result
66 and ti.task_id not in prev_result[XCOM_SKIPMIXIN_FOLLOWED]
67 ):
68 # Skip any tasks that are not in "followed"
69 should_skip = True
70 elif (
71 XCOM_SKIPMIXIN_SKIPPED in prev_result
72 and ti.task_id in prev_result[XCOM_SKIPMIXIN_SKIPPED]
73 ):
74 # Skip any tasks that are in "skipped"
75 should_skip = True
77 if should_skip:
78 # If the parent SkipMixin has run, and the XCom result stored indicates this
79 # ti should be skipped, set ti.state to SKIPPED and fail the rule so that the
80 # ti does not execute.
81 if dep_context.wait_for_past_depends_before_skipping:
82 past_depends_met = ti.xcom_pull(
83 task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
84 )
85 if not past_depends_met:
86 yield self._failing_status(
87 reason=("Task should be skipped but the the past depends are not met")
88 )
89 return
90 ti.set_state(State.SKIPPED, session)
91 yield self._failing_status(
92 reason=f"Skipping because of previous XCom result from parent task {parent.task_id}"
93 )
94 return