airflow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] oelesinsc24 commented on a change in pull request #4068: [AIRFLOW-2310]: Add AWS Glue Job Compatibility to Airflow
Date Mon, 22 Oct 2018 19:30:03 GMT
oelesinsc24 commented on a change in pull request #4068: [AIRFLOW-2310]: Add AWS Glue Job Compatibility
to Airflow
URL: https://github.com/apache/incubator-airflow/pull/4068#discussion_r227036055
 
 

 ##########
 File path: airflow/contrib/hooks/aws_glue_job_hook.py
 ##########
 @@ -0,0 +1,130 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+from airflow.exceptions import AirflowException
+from airflow.contrib.hooks.aws_hook import AwsHook
+import time
+
+
+class AwsGlueJobHook(AwsHook):
+    """
+    Interact with AWS Glue - create job, trigger, crawler
+
+    :param job_name: unique job name per AWS account
+    :type str
+    :param desc: job description
+    :type str
+    :param region_name: aws region name (example: us-east-1)
+    :type region_name: str
+    """
+
+    def __init__(self,
+                 job_name=None,
+                 desc=None,
+                 aws_conn_id='aws_default',
+                 region_name=None, *args, **kwargs):
+        self.job_name = job_name
+        self.desc = desc
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        super(AwsGlueJobHook, self).__init__(*args, **kwargs)
+
+    def get_conn(self):
+        conn = self.get_client_type('glue', self.region_name)
+        return conn
+
+    def list_jobs(self):
+        conn = self.get_conn()
+        return conn.get_jobs()
+
+    def initialize_job(self, script_arguments=None):
+        """
+        Initializes connection with AWS Glue
+        to run job
+        :return:
+        """
+        glue_client = self.get_conn()
+
+        try:
+            job_response = self.get_glue_job()
+            job_name = job_response['Name']
+            job_run = glue_client.start_job_run(
+                JobName=job_name,
+                Arguments=script_arguments
+            )
+            return self.job_completion(job_name, job_run['JobRunId'])
+        except Exception as general_error:
+            raise AirflowException(
+                'Failed to run aws glue job, error: {error}'.format(
+                    error=str(general_error)
+                )
+            )
+
+    def job_completion(self, job_name=None, run_id=None):
+        """
+        :param job_name:
+        :param run_id:
+        :return:
+        """
+        glue_client = self.get_conn()
+        job_status = glue_client.get_job_run(
+            JobName=job_name,
+            RunId=run_id,
+            PredecessorsIncluded=True
+        )
+        job_run_state = job_status['JobRun']['JobRunState']
+        failed = job_run_state == 'FAILED'
+        stopped = job_run_state == 'STOPPED'
+        completed = job_run_state == 'SUCCEEDED'
+
+        while True:
+            if failed or stopped or completed:
+                self.log.info("Exiting Job {} Run State: {}"
+                              .format(run_id, job_run_state))
+                return {'JobRunState': job_run_state, 'JobRunId': run_id}
+            else:
+                self.log.info("Polling for AWS Glue Job {} current run state"
+                              .format(job_name))
+                time.sleep(6)
 
 Review comment:
   @ashb, run job and poll job for completion are two separate methods already.
   
   @Fokko, 6 seconds was a random number having looked at sleep time in some existing implementations.
e.g: 
   
   1. BigQuery `5 seconds`
   2. Google Dataproc `5 seconds` (airflow/contrib/hooks/gcp_dataproc_hook.py#L70)
   3. Google Dataproc `10 seconds` (another section: airflow/contrib/hooks/gcp_dataproc_hook.py#L164)
   
   However, this can be changed. In addition, from my experience with AWS Glue, 6 seconds
is enough to poll for job status

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message