uranusjr commented on code in PR #61010:
URL: https://github.com/apache/airflow/pull/61010#discussion_r2730711636
##########
providers/amazon/src/airflow/providers/amazon/aws/operators/emr.py:
##########
@@ -741,49 +741,66 @@ def execute(self, context: Context) -> str | None:
self._job_flow_id = response["JobFlowId"]
self.log.info("Job flow with id %s created", self._job_flow_id)
- EmrClusterLink.persist(
- context=context,
- operator=self,
- region_name=self.hook.conn_region_name,
- aws_partition=self.hook.conn_partition,
- job_flow_id=self._job_flow_id,
- )
- if self._job_flow_id:
- EmrLogsLink.persist(
+ try:
+ EmrClusterLink.persist(
context=context,
operator=self,
region_name=self.hook.conn_region_name,
aws_partition=self.hook.conn_partition,
job_flow_id=self._job_flow_id,
- log_uri=get_log_uri(emr_client=self.hook.conn,
job_flow_id=self._job_flow_id),
)
- if self.wait_for_completion:
- waiter_name =
WAITER_POLICY_NAME_MAPPING[WaitPolicy.WAIT_FOR_COMPLETION]
-
- if self.deferrable:
- self.defer(
- trigger=EmrCreateJobFlowTrigger(
- job_flow_id=self._job_flow_id,
- aws_conn_id=self.aws_conn_id,
- waiter_delay=self.waiter_delay,
- waiter_max_attempts=self.waiter_max_attempts,
- ),
- method_name="execute_complete",
- # timeout is set to ensure that if a trigger dies, the
timeout does not restart
- # 60 seconds is added to allow the trigger to exit
gracefully (i.e. yield TriggerEvent)
- timeout=timedelta(seconds=self.waiter_max_attempts *
self.waiter_delay + 60),
- )
- else:
- self.hook.get_waiter(waiter_name).wait(
- ClusterId=self._job_flow_id,
- WaiterConfig=prune_dict(
- {
- "Delay": self.waiter_delay,
- "MaxAttempts": self.waiter_max_attempts,
- }
- ),
+ if self._job_flow_id:
+ EmrLogsLink.persist(
+ context=context,
+ operator=self,
+ region_name=self.hook.conn_region_name,
+ aws_partition=self.hook.conn_partition,
+ job_flow_id=self._job_flow_id,
+ log_uri=get_log_uri(emr_client=self.hook.conn,
job_flow_id=self._job_flow_id),
)
- return self._job_flow_id
+ if self.wait_for_completion:
+ waiter_name =
WAITER_POLICY_NAME_MAPPING[WaitPolicy.WAIT_FOR_COMPLETION]
+
+ if self.deferrable:
+ self.defer(
+ trigger=EmrCreateJobFlowTrigger(
+ job_flow_id=self._job_flow_id,
+ aws_conn_id=self.aws_conn_id,
+ waiter_delay=self.waiter_delay,
+ waiter_max_attempts=self.waiter_max_attempts,
+ ),
+ method_name="execute_complete",
+ # timeout is set to ensure that if a trigger dies, the
timeout does not restart
+ # 60 seconds is added to allow the trigger to exit
gracefully (i.e. yield TriggerEvent)
+ timeout=timedelta(seconds=self.waiter_max_attempts *
self.waiter_delay + 60),
+ )
+ else:
+ self.hook.get_waiter(waiter_name).wait(
+ ClusterId=self._job_flow_id,
+ WaiterConfig=prune_dict(
+ {
+ "Delay": self.waiter_delay,
+ "MaxAttempts": self.waiter_max_attempts,
+ }
+ ),
+ )
+ return self._job_flow_id
+
+ # Best-effort cleanup when post-creation steps fail (e.g.
IAM/permission errors).
+ except Exception:
+ if self._job_flow_id:
+ try:
+ self.log.warning(
+ "Task failed after creating EMR job flow %s;
attempting cleanup.",
+ self._job_flow_id,
+ )
Review Comment:
Put this outside the try block
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]