def stop_training_job()

in custom_resource/sagemaker_training_job.py [0:0]


def stop_training_job(training_job_name):
    try:
        training_job = sm.describe_training_job(TrainingJobName=training_job_name)
        status = training_job["TrainingJobStatus"]
        if status == "InProgress":
            logger.info("Stopping InProgress training job: %s", training_job_name)
            sm.stop_training_job(TrainingJobName=training_job_name)
            return False
        else:
            logger.info("Training job status: %s, nothing to stop", status)
            return True
    except ClientError as e:
        # NOTE: This doesn't return "ResourceNotFound" code, so need to catch
        if (
            e.response["Error"]["Code"] == "ValidationException"
            and "resource not found" in e.response["Error"]["Message"]
        ):
            logger.info("Resource not found, nothing to stop")
            return True
        else:
            logger.error("Unexpected error while trying to stop training job")
            raise e