Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Retire common_api_workflows, clean up config (#296)
Madison Swain-Bowden16fd77faba7f
diff --git a/openverse_catalog/dags/common/config.py b/openverse_catalog/dags/common/config.pydeleted file mode 100644index 2b4d6a3fe..000000000--- a/openverse_catalog/dags/common/config.py+++ /dev/null@@ -1,40 +0,0 @@-import os-from datetime import datetime, timedelta---CRONTAB_STR = "crontab_str"-SCRIPT = "script"--DAG_DEFAULT_ARGS = {- "owner": "data-eng-admin",- "depends_on_past": False,- "start_date": datetime(2019, 1, 15),- "email_on_retry": False,- "retries": 3,- "retry_delay": timedelta(minutes=15),-}--# The DAG_VARIABLES dict holds variables that differentiate the dags-# between API sources. The entry for each source should have the-# following format:-# <source>: {-# 'script': os.path.join(api_script_path, <source_script>),-# 'crontab_str': <some_crontab_string>,-# }-# The `crontab_str` key is optional, and should be either a-# crontab-style string specifying when the particular script should-# be run, or a shorthand known to airflow (see-# https://airflow.apache.org/docs/stable/scheduler.html). Omitting that-# key results in a DAG that runs only when the user clicks 'play' in the-# airflow interface.--AIRFLOW_HOME = os.getenv("AIRFLOW_HOME")-API_SCRIPT_PATH = os.path.join(AIRFLOW_HOME, "dags", "provider_api_scripts")---DAG_VARIABLES = {- "thingiverse": {- SCRIPT: os.path.join(API_SCRIPT_PATH, "Thingiverse.py"),- CRONTAB_STR: "0 7 * * *",- },-}diff --git a/openverse_catalog/dags/common/dag_factory.py b/openverse_catalog/dags/common/dag_factory.pyindex 064c967aa..77f66ba89 100644--- a/openverse_catalog/dags/common/dag_factory.py+++ b/openverse_catalog/dags/common/dag_factory.py@@ -1,8 +1,6 @@import logging-from copy import deepcopyfrom datetime import datetime, timedelta-import common.config as conffrom airflow import DAGfrom airflow.models.baseoperator import cross_downstreamfrom airflow.operators.dummy import DummyOperator@@ -13,6 +11,16 @@ from airflow.utils.trigger_rule import TriggerRulelogger = logging.getLogger(__name__)+DAG_DEFAULT_ARGS = {+ "owner": "data-eng-admin",+ "depends_on_past": False,+ "start_date": datetime(2019, 1, 15),+ "email_on_retry": False,+ "retries": 3,+ "retry_delay": timedelta(minutes=15),+}++def get_dated_main_runner_operator(main_function,execution_timeout,@@ -32,7 +40,7 @@ def get_dated_main_runner_operator(def create_provider_api_workflow(dag_id,main_function,- default_args=conf.DAG_DEFAULT_ARGS,+ default_args=None,start_date=datetime(1970, 1, 1),concurrency=1,schedule_string="@daily",@@ -77,12 +85,10 @@ def create_provider_api_workflow(dagrun_timeout: datetime.timedelta giving the total amount of timea given dagrun may take."""- args = deepcopy(default_args)- args.update(start_date=start_date)- print(args)+ default_args = default_args or DAG_DEFAULT_ARGSdag = DAG(dag_id=dag_id,- default_args=args,+ default_args={**default_args, "start_date": start_date},concurrency=concurrency,max_active_runs=concurrency,dagrun_timeout=dagrun_timeout,@@ -114,7 +120,7 @@ def create_day_partitioned_ingestion_dag(reingestion_day_list_list,start_date=datetime(1970, 1, 1),concurrency=1,- default_args=conf.DAG_DEFAULT_ARGS,+ default_args=None,dagrun_timeout=timedelta(hours=23),ingestion_task_timeout=timedelta(hours=2),):@@ -191,11 +197,10 @@ def create_day_partitioned_ingestion_dag(executions of the `main_function` allowed; that is set by the`concurrency` parameter."""- args = deepcopy(default_args)- args.update(start_date=start_date)+ default_args = default_args or DAG_DEFAULT_ARGSdag = DAG(dag_id=dag_id,- default_args=args,+ default_args={**default_args, "start_date": start_date},concurrency=concurrency,max_active_runs=concurrency,dagrun_timeout=dagrun_timeout,diff --git a/openverse_catalog/dags/common_api_workflows.py b/openverse_catalog/dags/retired/common_api_workflows.pysimilarity index 100%rename from openverse_catalog/dags/common_api_workflows.pyrename to openverse_catalog/dags/retired/common_api_workflows.pydiff --git a/tests/dags/test_common_api_workflows.py b/tests/dags/test_common_api_workflows.pydeleted file mode 100644index 78b03ec59..000000000--- a/tests/dags/test_common_api_workflows.py+++ /dev/null@@ -1,125 +0,0 @@-import os-from datetime import datetime, timedelta, timezone--import common.config as conf-import common.dag_factory-import common_api_workflows as caw-from airflow import DAG-from airflow.models import DagBag, TaskInstance---SCRIPT = conf.SCRIPT-CRONTAB_STR = conf.CRONTAB_STR-FILE_DIR = os.path.abspath(os.path.dirname(__file__))---def dated(dag_date):- print(dag_date)---def test_get_dated_main_runner_handles_zero_shift(capsys):- dag = DAG(dag_id="test_dag", start_date=datetime.strptime("2019-01-01", "%Y-%m-%d"))- execution_date = datetime.strptime("2019-01-01", "%Y-%m-%d").replace(- tzinfo=timezone.utc- )- main_func = dated- with dag:- runner = common.dag_factory.get_dated_main_runner_operator(- main_func, timedelta(minutes=1)- )- ti = TaskInstance(runner, execution_date)- ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)- # main_func.assert_called_with('2019-01-01')- # Mocking main_func causes errors because Airflow JSON-encodes it,- # and MagicMock is not JSON-serializable.- captured = capsys.readouterr()- assert captured.out == "2019-01-01\n"---def test_get_dated_main_runner_handles_day_shift(capsys):- dag = DAG(dag_id="test_dag", start_date=datetime.strptime("2019-01-01", "%Y-%m-%d"))- execution_date = datetime.strptime("2019-01-01", "%Y-%m-%d").replace(- tzinfo=timezone.utc- )- main_func = dated- with dag:- runner = common.dag_factory.get_dated_main_runner_operator(- main_func, timedelta(minutes=1), day_shift=1- )- ti = TaskInstance(runner, execution_date)- ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)- # main_func.assert_called_with('2018-12-31')- # Mocking main_func causes errors because Airflow JSON-encodes it,- # and MagicMock is not JSON-serializable.- captured = capsys.readouterr()- assert captured.out == "2018-12-31\n"---def test_dags_load_with_no_errors(tmpdir):- tmp_directory = str(tmpdir)- dag_bag = DagBag(dag_folder=tmp_directory, include_examples=False)- dag_bag.process_file(os.path.join(FILE_DIR, "../../dags/common_api_workflows.py"))- assert len(dag_bag.import_errors) == 0---def test_create_dag_creates_correct_dependencies():- dag = caw.create_dag("test_source", "test_script_location", "test_dag_id")- run_id = "get_test_source_images"- run_task = dag.get_task(run_id)- assert run_task.downstream_task_ids == set()---def test_create_dag_adds_schedule_interval():- crontab_str = "0 * * * *"- dag = caw.create_dag(- "test_source", "test_script_location", "test_dag_id", crontab_str=crontab_str- )- assert dag.schedule_interval == crontab_str---def test_load_dag_conf_handles_missing_script():- source = "test_source"- dag_variables = {"test_source": {}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert script_location is None---def test_load_dag_conf_validates_script_location():- source = "test_source"- dag_variables = {"test_source": {SCRIPT: "/this/path/does/not/exist/hopefully"}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert script_location is None---def test_load_dag_conf_returns_valid_script_location():- source = "test_source"- expected_script_location = os.path.join(- os.path.join(FILE_DIR, "providers/provider_api_scripts/resources"),- "FakeSource.py",- )- dag_variables = {"test_source": {SCRIPT: expected_script_location}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert script_location == expected_script_location---def test_load_dag_conf_validates_crontab_str():- source = "test_source"- bad_crontab_str = "abc123"- dag_variables = {"test_source": {CRONTAB_STR: bad_crontab_str}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert crontab_str is None---def test_load_dag_conf_handles_missing_crontab_str():- source = "test_source"- dag_variables = {"test_source": {}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert crontab_str is None---def test_load_dag_conf_uses_proper_crontab_str():- source = "test_source"- good_crontab_str = "0 * * * *"- dag_variables = {"test_source": {CRONTAB_STR: good_crontab_str}}- script_location, dag_id, crontab_str = caw.load_dag_conf(source, dag_variables)- assert crontab_str == good_crontab_str