jj-ian commented on a change in pull request #4083: [AIRFLOW-3211] Reattach to
GCP Dataproc jobs upon Airflow restart
URL: https://github.com/apache/incubator-airflow/pull/4083#discussion_r228297977
##########
File path: airflow/contrib/hooks/gcp_dataproc_hook.py
##########
@@ -33,12 +33,82 @@ def __init__(self, dataproc_api, project_id, job,
region='global',
self.dataproc_api = dataproc_api
self.project_id = project_id
self.region = region
+
+ # Check if the job to submit is already running on the cluster.
+ # If so, don't resubmit the job.
+ try:
+ cluster_name = job['job']['placement']['clusterName']
+ except KeyError:
+ self.log.error('Job to submit is incorrectly configured.')
+ raise
+
+ jobs_on_cluster_response =
dataproc_api.projects().regions().jobs().list(
+ projectId=self.project_id,
+ region=self.region,
+ clusterName=cluster_name).execute()
+
+ UUID_LENGTH = 9
Review comment:
Thank you @fenglu-g. Just to clarify, do you mean that I should modify the
[`DataProcHook.submit`](https://github.com/apache/incubator-airflow/blob/0e8394fd23d067b7e226c011bb1825ff734219c5/airflow/contrib/hooks/gcp_dataproc_hook.py#L229)
and
[`_DataProcJob.__init__`](https://github.com/apache/incubator-airflow/blob/0e8394fd23d067b7e226c011bb1825ff734219c5/airflow/contrib/hooks/gcp_dataproc_hook.py#L31)
functions to take in the new regex param like so?
```
class _DataProcJob(LoggingMixin):
def __init__(other args..., job_dedupe_regex=None):
class DataProcHook(GoogleCloudBaseHook):
def submit(other args..., job_dedupe_regex=[by default, regex that
dedupes by matching the task ID]):
```
And then have `submit()` pass the regex along to the _DataProcJob init
[here](https://github.com/apache/incubator-airflow/blob/0e8394fd23d067b7e226c011bb1825ff734219c5/airflow/contrib/hooks/gcp_dataproc_hook.py#L230
), because the deduping happens during the init:
```
def submit(other args..., job_dedupe_regex=[by default, regex that dedupes
by matching the task ID]):
submitted = _DataProcJob(other args...,
job_dedupe_regex=job_dedupe_regex)
```
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services