Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions airflow/providers/amazon/aws/hooks/sagemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,8 @@ def check_status(
non_terminal_states = self.non_terminal_states

sec = 0
running = True

while running:
while True:
Comment on lines -678 to +677

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to do this change otherwise python would complain that the response variable might be uninitialized in the return clause.

time.sleep(check_interval)
sec += check_interval

Expand All @@ -688,19 +687,16 @@ def check_status(
except ClientError:
raise AirflowException("AWS request failed, check logs for more info")

if status in non_terminal_states:
running = True
elif status in self.failed_states:
if status in self.failed_states:
raise AirflowException(f"SageMaker job failed because {response['FailureReason']}")
else:
running = False
elif status not in non_terminal_states:
break

if max_ingestion_time and sec > max_ingestion_time:
# ensure that the job gets killed if the max ingestion time is exceeded
raise AirflowException(f"SageMaker job took more than {max_ingestion_time} seconds")

self.log.info("SageMaker Job completed")
response = describe_function(job_name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is basically no latency between the last call of the loop (l.682) and this one, and moreover the status reached is supposed to be a terminal state, so there is no need to repeat the call here.

return response

def check_training_status_with_log(
Expand Down
9 changes: 4 additions & 5 deletions tests/providers/amazon/aws/hooks/test_sagemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,15 @@ def test_training_ends_with_wait(self, mock_client, mock_check_training):
DESCRIBE_TRAINING_INPROGRESS_RETURN,
DESCRIBE_TRAINING_STOPPING_RETURN,
DESCRIBE_TRAINING_COMPLETED_RETURN,
DESCRIBE_TRAINING_COMPLETED_RETURN,
],
}
mock_session.configure_mock(**attrs)
mock_client.return_value = mock_session
hook = SageMakerHook(aws_conn_id="sagemaker_test_conn_id_1")
hook.create_training_job(
create_training_params, wait_for_completion=True, print_log=False, check_interval=1
create_training_params, wait_for_completion=True, print_log=False, check_interval=0
)
assert mock_session.describe_training_job.call_count == 4
assert mock_session.describe_training_job.call_count == 3

@mock.patch.object(SageMakerHook, "check_training_config")
@mock.patch.object(SageMakerHook, "get_conn")
Expand All @@ -360,7 +359,7 @@ def test_training_throws_error_when_failed_with_wait(self, mock_client, mock_che
create_training_params,
wait_for_completion=True,
print_log=False,
check_interval=1,
check_interval=0,
)
assert mock_session.describe_training_job.call_count == 3

Expand Down Expand Up @@ -616,7 +615,7 @@ def test_training_with_logs(self, mock_describe, mock_client, mock_log_client, m
mock_log_client.return_value = mock_log_session
hook = SageMakerHook(aws_conn_id="sagemaker_test_conn_id_1")
hook.create_training_job(
create_training_params, wait_for_completion=True, print_log=True, check_interval=1
create_training_params, wait_for_completion=True, print_log=True, check_interval=0
)
assert mock_describe.call_count == 3
assert mock_session.describe_training_job.call_count == 1
Expand Down