-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e380677
commit 813f817
Showing
6 changed files
with
136 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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
3 changes: 2 additions & 1 deletion
3
python_modules/libraries/dagster-aws/dagster_aws/pipes/clients/__init__.py
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from dagster_aws.pipes.clients.ecs import PipesECSClient | ||
from dagster_aws.pipes.clients.emr import PipesEMRClient | ||
from dagster_aws.pipes.clients.glue import PipesGlueClient | ||
from dagster_aws.pipes.clients.lambda_ import PipesLambdaClient | ||
|
||
__all__ = ["PipesGlueClient", "PipesLambdaClient", "PipesECSClient"] | ||
__all__ = ["PipesGlueClient", "PipesLambdaClient", "PipesECSClient", "PipesEMRClient"] |
103 changes: 103 additions & 0 deletions
103
python_modules/libraries/dagster-aws/dagster_aws/pipes/clients/emr.py
This file contains 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,103 @@ | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
import boto3 | ||
from dagster._annotations import experimental | ||
from dagster._core.execution.context.compute import OpExecutionContext | ||
from dagster._core.pipes.client import PipesContextInjector, PipesMessageReader | ||
from mypy_boto3_emr.type_defs import ( | ||
DescribeClusterOutputTypeDef, | ||
RunJobFlowInputRequestTypeDef, | ||
RunJobFlowOutputTypeDef, | ||
) | ||
|
||
from dagster_aws.pipes.clients.base import PipesBaseClient, class_docstring, run_docstring | ||
from dagster_aws.pipes.message_readers import PipesCloudWatchMessageReader | ||
|
||
if TYPE_CHECKING: | ||
from mypy_boto3_emr.literals import ClusterStateType | ||
|
||
|
||
AWS_SERVICE_NAME = "EMR" | ||
|
||
|
||
@experimental | ||
@class_docstring(AWS_SERVICE_NAME) | ||
class PipesEMRClient( | ||
PipesBaseClient[ | ||
RunJobFlowInputRequestTypeDef, RunJobFlowOutputTypeDef, DescribeClusterOutputTypeDef | ||
] | ||
): | ||
AWS_SERVICE_NAME = AWS_SERVICE_NAME | ||
|
||
def __init__( | ||
self, | ||
client=None, | ||
context_injector: Optional[PipesContextInjector] = None, | ||
message_reader: Optional[PipesMessageReader] = None, | ||
forward_termination: bool = True, | ||
): | ||
super().__init__( | ||
client=client or boto3.client("emr"), | ||
context_injector=context_injector, | ||
message_reader=message_reader, | ||
forward_termination=forward_termination, | ||
) | ||
|
||
@classmethod | ||
def _is_dagster_maintained(cls) -> bool: | ||
return True | ||
|
||
# is there a better way to do this? | ||
# is it possible to automatically inject the class attributes into the method docs? | ||
run = run_docstring(AWS_SERVICE_NAME, "run_job_flow")(PipesBaseClient.run) | ||
|
||
def _start( | ||
self, context: OpExecutionContext, params: "RunJobFlowInputRequestTypeDef" | ||
) -> "RunJobFlowOutputTypeDef": | ||
response = self._client.run_job_flow(**params) | ||
cluster_id = response["JobFlowId"] | ||
context.log.info( | ||
f"[pipes] {self.AWS_SERVICE_NAME} job started with cluster id {cluster_id}" | ||
) | ||
return response | ||
|
||
def _wait_for_completion( | ||
self, context: OpExecutionContext, start_response: RunJobFlowOutputTypeDef | ||
) -> "DescribeClusterOutputTypeDef": | ||
cluster_id = start_response["JobFlowId"] | ||
self._client.get_waiter("cluster_running").wait(ClusterId=cluster_id) | ||
context.log.info(f"[pipes] {self.AWS_SERVICE_NAME} job {cluster_id} running") | ||
# now wait for the job to complete | ||
self._client.get_waiter("cluster_terminated").wait(ClusterId=cluster_id) | ||
|
||
response = self._client.describe_cluster(ClusterId=cluster_id) | ||
|
||
state: ClusterStateType = response["Cluster"]["Status"]["State"] | ||
|
||
context.log.info( | ||
f"[pipes] {self.AWS_SERVICE_NAME} job {cluster_id} completed with state: {state}" | ||
) | ||
|
||
if state == "FAILED": | ||
context.log.error(f"[pipes] {self.AWS_SERVICE_NAME} job {cluster_id} failed") | ||
raise Exception(f"[pipes] {self.AWS_SERVICE_NAME} job {cluster_id} failed") | ||
|
||
return response | ||
|
||
def _read_messages(self, context: OpExecutionContext, response: DescribeClusterOutputTypeDef): | ||
if isinstance(self._message_reader, PipesCloudWatchMessageReader): | ||
# we can get cloudwatch logs from the known log group | ||
log_group, log_stream = response["Cluster"]["LogUri"].split(":", 1) # pyright: ignore (reportTypedDictNotRequiredAccess) | ||
context.log.info(f"[pipes] Reading logs from {log_group}/{log_stream}") | ||
self._message_reader.consume_cloudwatch_logs( | ||
log_group, | ||
log_stream, | ||
start_time=int( | ||
response["Cluster"]["Status"]["Timeline"]["CreationDateTime"].timestamp() * 1000 # pyright: ignore (reportTypedDictNotRequiredAccess) | ||
), | ||
) | ||
|
||
def _terminate(self, context: OpExecutionContext, start_response: RunJobFlowOutputTypeDef): | ||
cluster_id = start_response["JobFlowId"] | ||
context.log.info(f"[pipes] Terminating {self.AWS_SERVICE_NAME} job {cluster_id}") | ||
self._client.terminate_job_flows(JobFlowIds=[cluster_id]) |
This file contains 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