Skip to content

Commit 340a70b

Browse files
krzysztof-kubisRBAMOUSER\kubisk1krzysztof-kubiskrzysztof-kubis
authored
Added condition to check if it is a scheduled save or rerun (#43453)
* Aadded condition to check if it is a scheduled save or rerun * Fix key name in context of task * I added unit tests to the condition to check if it is a scheduled save or rerun --------- Co-authored-by: RBAMOUSER\kubisk1 <krzysztof.kubis@contractors.roche.com> Co-authored-by: krzysztof-kubis <krzysztof-kubis@github.com> Co-authored-by: krzysztof-kubis <krzysztof-kubis@github.com/>
1 parent 2a9bded commit 340a70b

File tree

2 files changed

+81
-1
lines changed
  • providers

2 files changed

+81
-1
lines changed

providers/src/airflow/providers/dbt/cloud/operators/dbt.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,16 @@ def execute(self, context: Context):
149149
self.run_id = non_terminal_runs[0]["id"]
150150
job_run_url = non_terminal_runs[0]["href"]
151151

152+
is_retry = context["ti"].try_number != 1
153+
152154
if not self.reuse_existing_run or not non_terminal_runs:
153155
trigger_job_response = self.hook.trigger_job_run(
154156
account_id=self.account_id,
155157
job_id=self.job_id,
156158
cause=self.trigger_reason,
157159
steps_override=self.steps_override,
158160
schema_override=self.schema_override,
159-
retry_from_failure=self.retry_from_failure,
161+
retry_from_failure=is_retry and self.retry_from_failure,
160162
additional_run_config=self.additional_run_config,
161163
)
162164
self.run_id = trigger_job_response.json()["data"]["id"]

providers/tests/dbt/cloud/operators/test_dbt.py

+78
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@
6464
),
6565
}
6666
}
67+
JOB_RUN_ERROR_RESPONSE = {
68+
"data": [
69+
{
70+
"id": RUN_ID,
71+
"href": EXPECTED_JOB_RUN_OP_EXTRA_LINK.format(
72+
account_id=ACCOUNT_ID, project_id=PROJECT_ID, run_id=RUN_ID
73+
),
74+
"status": DbtCloudJobRunStatus.ERROR.value,
75+
}
76+
]
77+
}
6778

6879

6980
def mock_response_json(response: dict):
@@ -421,6 +432,73 @@ def test_execute_retry_from_failure(self, mock_run_job, conn_id, account_id):
421432
additional_run_config=self.config["additional_run_config"],
422433
)
423434

435+
@patch.object(DbtCloudHook, "_run_and_get_response")
436+
@pytest.mark.parametrize(
437+
"conn_id, account_id",
438+
[(ACCOUNT_ID_CONN, None), (NO_ACCOUNT_ID_CONN, ACCOUNT_ID)],
439+
ids=["default_account", "explicit_account"],
440+
)
441+
def test_execute_retry_from_failure_run(self, mock_run_req, conn_id, account_id):
442+
operator = DbtCloudRunJobOperator(
443+
task_id=TASK_ID,
444+
dbt_cloud_conn_id=conn_id,
445+
account_id=account_id,
446+
trigger_reason=None,
447+
dag=self.dag,
448+
retry_from_failure=True,
449+
**self.config,
450+
)
451+
self.mock_context["ti"].try_number = 1
452+
453+
assert operator.dbt_cloud_conn_id == conn_id
454+
assert operator.job_id == self.config["job_id"]
455+
assert operator.account_id == account_id
456+
assert operator.check_interval == self.config["check_interval"]
457+
assert operator.timeout == self.config["timeout"]
458+
assert operator.retry_from_failure
459+
assert operator.steps_override == self.config["steps_override"]
460+
assert operator.schema_override == self.config["schema_override"]
461+
assert operator.additional_run_config == self.config["additional_run_config"]
462+
463+
operator.execute(context=self.mock_context)
464+
465+
mock_run_req.assert_called()
466+
467+
@patch.object(
468+
DbtCloudHook, "_run_and_get_response", return_value=mock_response_json(JOB_RUN_ERROR_RESPONSE)
469+
)
470+
@patch.object(DbtCloudHook, "retry_failed_job_run")
471+
@pytest.mark.parametrize(
472+
"conn_id, account_id",
473+
[(ACCOUNT_ID_CONN, None), (NO_ACCOUNT_ID_CONN, ACCOUNT_ID)],
474+
ids=["default_account", "explicit_account"],
475+
)
476+
def test_execute_retry_from_failure_rerun(self, mock_run_req, mock_rerun_req, conn_id, account_id):
477+
operator = DbtCloudRunJobOperator(
478+
task_id=TASK_ID,
479+
dbt_cloud_conn_id=conn_id,
480+
account_id=account_id,
481+
trigger_reason=None,
482+
dag=self.dag,
483+
retry_from_failure=True,
484+
**self.config,
485+
)
486+
self.mock_context["ti"].try_number = 2
487+
488+
assert operator.dbt_cloud_conn_id == conn_id
489+
assert operator.job_id == self.config["job_id"]
490+
assert operator.account_id == account_id
491+
assert operator.check_interval == self.config["check_interval"]
492+
assert operator.timeout == self.config["timeout"]
493+
assert operator.retry_from_failure
494+
assert operator.steps_override == self.config["steps_override"]
495+
assert operator.schema_override == self.config["schema_override"]
496+
assert operator.additional_run_config == self.config["additional_run_config"]
497+
498+
operator.execute(context=self.mock_context)
499+
500+
mock_rerun_req.assert_called_once()
501+
424502
@patch.object(DbtCloudHook, "trigger_job_run")
425503
@pytest.mark.parametrize(
426504
"conn_id, account_id",

0 commit comments

Comments
 (0)