Repository navigation
Add wrapper for submitting AWS Batch jobs #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import boto3 | ||
| import os | ||
|
|
||
| from botocore.exceptions import ClientError | ||
| from nypl_py_utils.functions.log_helper import create_log | ||
|
|
||
|
|
||
| class BatchClient: | ||
| """Client for interacting with AWS Batch""" | ||
|
|
||
| def __init__(self): | ||
| self.logger = create_log("batch_client") | ||
|
|
||
| try: | ||
| self.batch_client = boto3.client( | ||
| "batch", region_name=os.environ.get("AWS_REGION", "us-east-1") | ||
| ) | ||
| except ClientError as e: | ||
| self.logger.error( | ||
| "Could not create Batch client: {err}".format(err=e) | ||
| ) | ||
| raise BatchClientError( | ||
| "Could not create Batch client: {err}".format(err=e) | ||
| ) from None | ||
|
|
||
| def close(self): | ||
| self.batch_client.close() | ||
|
|
||
| def submit_job( | ||
| self, job_name, job_definition, queue_name=None, env_vars={} | ||
| ): | ||
| """ | ||
| Submits a job to be run by AWS Batch. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| job_name: str | ||
| The name of the job. This is the developer's choice and is NOT the | ||
| same as the job_definition, which must correspond to a known value. | ||
| job_definition: str | ||
| Name of the existing, registered job definition in AWS to run | ||
| queue_name: str, optional | ||
| Name of the job queue to use. If None, defaults to the | ||
| job_definition string. | ||
| env_vars: dict, optional | ||
| Environment variables to use when running the job | ||
| """ | ||
| formatted_env = [{"name": k, "value": v} for k, v in env_vars.items()] | ||
| if queue_name is None: | ||
| queue_name = job_definition | ||
|
|
||
| self.logger.info(f"Submitting {job_definition} as {job_name}") | ||
| try: | ||
| if formatted_env: | ||
| self.batch_client.submit_job( | ||
| jobName=job_name, | ||
| jobQueue=queue_name, | ||
| jobDefinition=job_definition, | ||
| containerOverrides={"environment": formatted_env}, | ||
| ) | ||
| else: | ||
| self.batch_client.submit_job( | ||
| jobName=job_name, | ||
| jobQueue=queue_name, | ||
| jobDefinition=job_definition, | ||
| ) | ||
| except ClientError as e: | ||
| self.logger.error(f"Could not run {job_definition} job: {str(e)}") | ||
| raise BatchClientError( | ||
| f"Could not run {job_definition} job: {str(e)}" | ||
| ) from None | ||
|
|
||
|
|
||
| class BatchClientError(Exception): | ||
| def __init__(self, message=None): | ||
| self.message = message | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import pytest | ||
|
|
||
| from botocore.exceptions import ClientError | ||
| from nypl_py_utils.classes.batch_client import BatchClient, BatchClientError | ||
|
|
||
|
|
||
| class TestBatchClient: | ||
|
|
||
| @pytest.fixture | ||
| def test_instance(self, mocker): | ||
| mocker.patch('boto3.client') | ||
| return BatchClient() | ||
|
|
||
| def test_submit_job_defaults(self, test_instance): | ||
| test_instance.submit_job("job_name", "job_def") | ||
| test_instance.batch_client.submit_job.assert_called_once_with( | ||
| jobName="job_name", | ||
| jobQueue="job_def", | ||
| jobDefinition="job_def" | ||
| ) | ||
|
|
||
| def test_submit_job_overrides(self, test_instance): | ||
| test_instance.submit_job( | ||
| "job_name", "job_def", | ||
| queue_name="queue_name", | ||
| env_vars={"var1": "val1", "var2": 2} | ||
| ) | ||
| test_instance.batch_client.submit_job.assert_called_once_with( | ||
| jobName="job_name", | ||
| jobQueue="queue_name", | ||
| jobDefinition="job_def", | ||
| containerOverrides={"environment": [ | ||
| {"name": "var1", "value": "val1"}, | ||
| {"name": "var2", "value": 2} | ||
| ]} | ||
| ) | ||
|
|
||
| def test_submit_job_error(self, test_instance): | ||
| test_instance.batch_client.submit_job.side_effect = \ | ||
| ClientError({}, 'SubmitJob') | ||
| with pytest.raises(BatchClientError): | ||
| test_instance.submit_job("job_name", "job_def") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.