Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/ti_deps/deps/not_previously_skipped_dep.py: 25%
28 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +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.ti_deps.deps.base_ti_dep import BaseTIDep
23class NotPreviouslySkippedDep(BaseTIDep):
24 """
25 Determine if this task should be skipped.
27 Based on any of the task's direct upstream relatives have decided this task should
28 be skipped.
29 """
31 NAME = "Not Previously Skipped"
32 IGNORABLE = True
33 IS_TASK_DEP = True
35 def _get_dep_statuses(self, ti, session, dep_context):
36 from airflow.models.skipmixin import (
37 XCOM_SKIPMIXIN_FOLLOWED,
38 XCOM_SKIPMIXIN_KEY,
39 XCOM_SKIPMIXIN_SKIPPED,
40 SkipMixin,
41 )
42 from airflow.utils.state import State
44 upstream = ti.task.get_direct_relatives(upstream=True)
46 finished_tis = dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
48 finished_task_ids = {t.task_id for t in finished_tis}
50 for parent in upstream:
51 if isinstance(parent, SkipMixin):
52 if parent.task_id not in finished_task_ids:
53 # This can happen if the parent task has not yet run.
54 continue
56 prev_result = ti.xcom_pull(task_ids=parent.task_id, key=XCOM_SKIPMIXIN_KEY, session=session)
58 if prev_result is None:
59 # This can happen if the parent task has not yet run.
60 continue
62 should_skip = False
63 if (
64 XCOM_SKIPMIXIN_FOLLOWED in prev_result
65 and ti.task_id not in prev_result[XCOM_SKIPMIXIN_FOLLOWED]
66 ):
67 # Skip any tasks that are not in "followed"
68 should_skip = True
69 elif (
70 XCOM_SKIPMIXIN_SKIPPED in prev_result
71 and ti.task_id in prev_result[XCOM_SKIPMIXIN_SKIPPED]
72 ):
73 # Skip any tasks that are in "skipped"
74 should_skip = True
76 if should_skip:
77 # If the parent SkipMixin has run, and the XCom result stored indicates this
78 # ti should be skipped, set ti.state to SKIPPED and fail the rule so that the
79 # ti does not execute.
80 ti.set_state(State.SKIPPED, session)
81 yield self._failing_status(
82 reason=f"Skipping because of previous XCom result from parent task {parent.task_id}"
83 )
84 return