Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Always record provider run duration (#694)
Madison Swain-Bowden2a9647abec0c
diff --git a/openverse_catalog/dags/providers/factory_utils.py b/openverse_catalog/dags/providers/factory_utils.pyindex d57812171..a54df3714 100644--- a/openverse_catalog/dags/providers/factory_utils.py+++ b/openverse_catalog/dags/providers/factory_utils.py@@ -136,11 +136,13 @@ def pull_media_wrapper(start_time = time.perf_counter()# Not passing kwargs here because Airflow throws a bunch of stuff in there that# none of our provider scripts are expecting.- data = run_func(*args)- end_time = time.perf_counter()- # Report duration- duration = end_time - start_time- ti.xcom_push(key="duration", value=duration)+ try:+ data = run_func(*args)+ finally:+ end_time = time.perf_counter()+ # Report duration+ duration = end_time - start_time+ ti.xcom_push(key="duration", value=duration)return datadiff --git a/tests/dags/providers/test_factory_utils.py b/tests/dags/providers/test_factory_utils.pyindex 5458ccb76..e0e768f1b 100644--- a/tests/dags/providers/test_factory_utils.py+++ b/tests/dags/providers/test_factory_utils.py@@ -220,6 +220,33 @@ def test_pull_media_wrapper(internal_func_mock.assert_called_once_with(value)[email protected]("func", [fake_provider_module.main, FakeDataIngesterClass])+def test_pull_media_wrapper_always_pushes_duration(func, ti_mock, dagrun_mock):+ error_message = "Whoops!"++ def _raise_an_error(text):+ raise ValueError(text)++ with pytest.raises(ValueError, match=error_message):+ factory_utils.pull_media_wrapper(+ func,+ ["image"],+ ["file1.tsv"],+ ti_mock,+ dagrun_mock,+ args=[_raise_an_error, error_message],+ )+ # We should have one XCom push for duration+ assert ti_mock.xcom_push.call_count == 1+ push_call = ti_mock.xcom_push.mock_calls[0]+ # Check that the duration was reported+ assert push_call.kwargs["key"] == "duration"+ # Check that it was *not* None (it should always be recorded)+ duration = push_call.kwargs["value"]+ assert duration is not None+ assert duration > 0++@pytest.mark.parametrize("schedule_interval, expected",[