Skip to content

Commit 98a9c57

Browse files
author
Dalei Li
authored
set project_id and location when canceling BigQuery job (#27521)
1 parent a691ab5 commit 98a9c57

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

airflow/providers/google/cloud/hooks/bigquery.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,9 +1418,10 @@ def cancel_job(
14181418
:param project_id: Google Cloud Project where the job is running
14191419
:param location: location the job is running
14201420
"""
1421+
project_id = project_id or self.project_id
14211422
location = location or self.location
14221423

1423-
if self.poll_job_complete(job_id=job_id):
1424+
if self.poll_job_complete(job_id=job_id, project_id=project_id, location=location):
14241425
self.log.info("No running BigQuery jobs to cancel.")
14251426
return
14261427

@@ -1434,17 +1435,18 @@ def cancel_job(
14341435
job_complete = False
14351436
while polling_attempts < max_polling_attempts and not job_complete:
14361437
polling_attempts += 1
1437-
job_complete = self.poll_job_complete(job_id)
1438+
job_complete = self.poll_job_complete(job_id=job_id, project_id=project_id, location=location)
14381439
if job_complete:
14391440
self.log.info("Job successfully canceled: %s, %s", project_id, job_id)
14401441
elif polling_attempts == max_polling_attempts:
14411442
self.log.info(
1442-
"Stopping polling due to timeout. Job with id %s "
1443+
"Stopping polling due to timeout. Job %s, %s "
14431444
"has not completed cancel and may or may not finish.",
1445+
project_id,
14441446
job_id,
14451447
)
14461448
else:
1447-
self.log.info("Waiting for canceled job with id %s to finish.", job_id)
1449+
self.log.info("Waiting for canceled job %s, %s to finish.", project_id, job_id)
14481450
time.sleep(5)
14491451

14501452
@GoogleBaseHook.fallback_to_default_project_id

tests/providers/google/cloud/hooks/test_bigquery.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ def test_cancel_queries(self, mock_client, mock_poll_job_complete):
203203
self.hook.running_job_id = running_job_id
204204
self.hook.cancel_query()
205205

206-
calls = [mock.call(job_id=running_job_id), mock.call(running_job_id)]
206+
calls = [
207+
mock.call(job_id=running_job_id, project_id=PROJECT_ID, location=None),
208+
mock.call(job_id=running_job_id, project_id=PROJECT_ID, location=None),
209+
]
207210
mock_poll_job_complete.assert_has_calls(calls)
208211
mock_client.assert_called_once_with(project_id=PROJECT_ID, location=None)
209212
mock_client.return_value.cancel_job.assert_called_once_with(job_id=running_job_id)
@@ -599,7 +602,7 @@ def test_cancel_query_jobs_to_cancel(
599602

600603
self.hook.running_job_id = JOB_ID
601604
self.hook.cancel_query()
602-
poll_job_complete.assert_called_once_with(job_id=JOB_ID)
605+
poll_job_complete.assert_called_once_with(job_id=JOB_ID, project_id=PROJECT_ID, location=None)
603606
mock_logger_info.has_call(mock.call("No running BigQuery jobs to cancel."))
604607

605608
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.get_client")

0 commit comments

Comments
 (0)