Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add Airflow variable for turning off provider DAG catchup (#1907)

Staci Mullins3a6300a6e26a
diff --git a/catalog/dags/providers/provider_dag_factory.py b/catalog/dags/providers/provider_dag_factory.py
index ebc326324..a4dec84a1 100644
--- a/catalog/dags/providers/provider_dag_factory.py
+++ b/catalog/dags/providers/provider_dag_factory.py
@@ -68,6 +68,7 @@ import re
from string import Template
from airflow import DAG
+from airflow.models import Variable
from airflow.models.mappedoperator import MappedOperator
from airflow.models.param import Param
from airflow.operators.empty import EmptyOperator
@@ -347,6 +348,12 @@ def create_provider_api_workflow_dag(conf: ProviderWorkflow):
"""
default_args = {**DAG_DEFAULT_ARGS, **(conf.default_args or {})}
+ # catchup is turned on by default for dated DAGs to allow backfilling.
+ # It can be overridden with the `CATCHUP_ENABLED` Airflow variable.
+ catchup_enabled = conf.dated and Variable.get(
+ "CATCHUP_ENABLED", default_var=True, deserialize_json=True
+ )
+
dag = DAG(
dag_id=conf.dag_id,
default_args={**default_args, "start_date": conf.start_date},
@@ -354,7 +361,7 @@ def create_provider_api_workflow_dag(conf: ProviderWorkflow):
max_active_runs=conf.max_active_runs,
start_date=conf.start_date,
schedule=conf.schedule_string,
- catchup=conf.dated, # catchup is turned on for dated DAGs to allow backfilling
+ catchup=catchup_enabled,
doc_md=conf.doc_md,
tags=[
"provider",
diff --git a/catalog/env.template b/catalog/env.template
index 204150827..73d720aca 100644
--- a/catalog/env.template
+++ b/catalog/env.template
@@ -107,6 +107,8 @@ OPENVERSE_BUCKET=openverse-storage
DATA_REFRESH_POKE_INTERVAL=5
# Number of Retries if DAG task fails to run
DEFAULT_RETRY_COUNT = 2
+# Whether to enable catchup for dated DAGs, allowing automatic backfill.
+AIRFLOW_VAR_CATCHUP_ENABLED=false
AIRFLOW_VAR_AIRFLOW_RDS_ARN=unset
AIRFLOW_VAR_AIRFLOW_RDS_SNAPSHOTS_TO_RETAIN=7
diff --git a/catalog/tests/dags/providers/test_provider_dag_factory.py b/catalog/tests/dags/providers/test_provider_dag_factory.py
index 5d00306f6..021008999 100644
--- a/catalog/tests/dags/providers/test_provider_dag_factory.py
+++ b/catalog/tests/dags/providers/test_provider_dag_factory.py
@@ -121,6 +121,27 @@ def test_create_day_partitioned_ingestion_dag_with_multi_layer_dependencies():
assert ingest5_task.upstream_task_ids == {gather1_id}
+ "catchup_var, dated, expected_result",
+ [
+ (True, True, True),
+ (False, True, False),
+ # Always False if Dag is not dated
+ (True, False, False),
+ (False, False, False),
+ ],
+)
+def test_catchup_configuration(catchup_var, dated, expected_result):
+ with mock.patch("providers.provider_dag_factory.Variable") as MockVariable:
+ MockVariable.get.side_effect = [
+ catchup_var,
+ ]
+ dag = provider_dag_factory.create_provider_api_workflow_dag(
+ ProviderWorkflow(ingester_class=MockProviderDataIngester, dated=dated)
+ )
+ assert dag.catchup == expected_result
+
+
def test_apply_configuration_overrides():
# Create a mock DAG
dag = DAG(
diff --git a/catalog/tests/utilities/dag_doc_gen/test_dag_doc_generation.py b/catalog/tests/utilities/dag_doc_gen/test_dag_doc_generation.py
index eec4398e9..b7442a90f 100644
--- a/catalog/tests/utilities/dag_doc_gen/test_dag_doc_generation.py
+++ b/catalog/tests/utilities/dag_doc_gen/test_dag_doc_generation.py
@@ -3,8 +3,12 @@ from unittest import mock
import pytest
+from catalog.tests.dags.providers.provider_api_scripts.resources.provider_data_ingester.mock_provider_data_ingester import (
+ MockProviderDataIngester,
+)
from catalog.utilities.dag_doc_gen import dag_doc_generation
from catalog.utilities.dag_doc_gen.dag_doc_generation import DagInfo
+from providers.provider_workflows import ProviderWorkflow
class DagMock(NamedTuple):
@@ -32,7 +36,6 @@ _MODULE = "catalog.utilities.dag_doc_gen.dag_doc_generation"
("# Big header", "### Big header"),
],
)
[email protected]("catchup", [True, False])
@pytest.mark.parametrize(
"tags, type_",
[
@@ -42,16 +45,44 @@ _MODULE = "catalog.utilities.dag_doc_gen.dag_doc_generation"
(["foo", "bar"], "foo"),
],
)
[email protected]("provider_workflow", ["foo_bar", None])
+ "provider_workflow, catchup, expected_dated",
+ [
+ # Dated is always equal to provider_workflow.dated
+ (
+ ProviderWorkflow(ingester_class=MockProviderDataIngester, dated=False),
+ True,
+ False,
+ ),
+ (
+ ProviderWorkflow(ingester_class=MockProviderDataIngester, dated=False),
+ False,
+ False,
+ ),
+ (
+ ProviderWorkflow(ingester_class=MockProviderDataIngester, dated=True),
+ True,
+ True,
+ ),
+ (
+ ProviderWorkflow(ingester_class=MockProviderDataIngester, dated=True),
+ False,
+ True,
+ ),
+ # If ProviderWorkflow is None, fall back to the value of 'catchup'
+ (None, True, True),
+ (None, False, False),
+ ],
+)
def test_get_dags_info(
- schedule, doc, expected_doc, catchup, tags, type_, provider_workflow
+ schedule, doc, expected_doc, tags, type_, provider_workflow, catchup, expected_dated
):
dag = DagMock(schedule_interval=schedule, doc_md=doc, catchup=catchup, tags=tags)
expected = DagInfo(
dag_id=DAG_ID,
schedule=schedule,
doc=expected_doc,
- dated=catchup,
+ dated=expected_dated,
type_=type_,
provider_workflow=provider_workflow,
)
diff --git a/catalog/utilities/dag_doc_gen/dag_doc_generation.py b/catalog/utilities/dag_doc_gen/dag_doc_generation.py
index 606d434f2..b3c71734f 100644
--- a/catalog/utilities/dag_doc_gen/dag_doc_generation.py
+++ b/catalog/utilities/dag_doc_gen/dag_doc_generation.py
@@ -124,7 +124,10 @@ def get_dags_info(dags: DagMapping) -> list[DagInfo]:
if doc:
doc = fix_headings(doc)
- dated = dag.catchup
+ provider_workflow = provider_workflows.get(dag_id)
+ dated = (
+ provider_workflow.dated if provider_workflow is not None else dag.catchup
+ )
# Infer dag type from the first available tag
type_ = dag.tags[0] if dag.tags else "other"
dags_info.append(
@@ -134,7 +137,7 @@ def get_dags_info(dags: DagMapping) -> list[DagInfo]:
doc=doc,
type_=type_,
dated=dated,
- provider_workflow=provider_workflows.get(dag_id),
+ provider_workflow=provider_workflow,
)
)