question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add on_kill method to DataprocSubmitJobOperator

See original GitHub issue

Description

This operator should implement on_kill method using cancel_job method of DataprocHook so in case of termination we cancel running job. This option probably should be configurable (for example cancel_on_kill) because of request_id prameter in a job definition: https://googleapis.dev/python/dataproc/latest/gapic/v1/api.html#google.cloud.dataproc_v1.JobControllerClient.submit_job

I’m happy to help with system test 👍

Use case / motivation

Remove dangling jobs when operator is terminated.

Related Issues

https://github.com/apache/airflow/pull/6371 https://github.com/apache/airflow/pull/6371#issuecomment-590757917

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
turbaszekcommented, Sep 8, 2020

DataprocSubmitJobOperator has no relation with DataprocJobBaseOperator.

DataprocJobBaseOperator is used by “old” operators like DataprocSubmitPigJobOperator, DataprocSubmitHiveJobOperator etc. that are deprecated in favor of the generic operator DataprocSubmitJobOperator.

However, you are right that the logic of on_kill currently exists in old ops and may be reused in DataprocSubmitJobOperator.on_kill 😉

Thus the following code might do the job:

Looks good but let’s remove the request_id as I’m still not sure how it works

1reaction
tszerszencommented, Sep 8, 2020

In package airflow.providers.google.cloud.operators.dataproc each DataprocSubmitJobOperator inherits from DataprocJobBaseOperator.

DataprocJobBaseOperator has the following implementation of on_kill method (lines 984-992):

def on_kill(self):
    """
    Callback called when the operator is killed.
    Cancel any running job.
    """
    if self.dataproc_job_id:
        self.hook.cancel_job(
            project_id=self.project_id, job_id=self.dataproc_job_id, location=self.region
        )

@turbaszek wrote:

This operator should implement on_kill method using cancel_job method of DataprocHook so in case of termination we cancel running job.

Considering present implementation of on_kill method of DataprocJobBaseOperator isn’t it already done? The code call’s method cancel_job of DataprocHook when on_kill method is run and property dataproc_job_id is present.

If I understand this correctly, the only thing that method lacks is:

This option probably should be configurable (for example cancel_on_kill) because of request_id parameter in a job definition

Thus the following code might do the job:

def on_kill(self, cancel_on_kill=True, request_id=None):
    """
    Callback called when the operator is killed.
    Cancel any running job.

    Parameters:
        cancel_on_kill (bool): Cancel job if true, defaults to True
        request_id (job_id): Job ID to cancel, defaults to object property dataproc_job_id
    """
    if not cancel_on_kill:
        return
    job_id = self.dataproc_job_id
    if request_id:
        job_id = request_id
    if self.dataproc_job_id:
        self.hook.cancel_job(
            project_id=self.project_id, job_id=job_id, location=self.region
        )

Did I understand this issue correctly? If not I guess calling hook’s cancel_job method is not enough and I will investigate it further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to stop Dataproc Job from airflow - Stack Overflow
Is there any way, we can kill dataproc jobs directly via Airflow, if dataproc job id is provided as parameter. Google Cloud Collective....
Read more >
gcloud dataproc jobs kill | Google Cloud CLI Documentation
gcloud dataproc jobs kill job_id; POSITIONAL ARGUMENTS. Job resource - The ID of the job to kill. The arguments in this group can...
Read more >
Newest 'airflow' Questions - Stack Overflow
Airflow: How to load data from a REST API to BigQuery? I am new to Airflow here, and I am trying to write...
Read more >
How to release all COM objects when handling an...anycodings
I fixed my code by modifying these lines anycodings_memory-management of code in closeExcel method: if (application != null) { application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found