Programmatically cancelling a pyspark dataproc batch job

268 Views Asked by At

Using golang, I have several dataproc batch jobs running and I can access them via their Uuid by creating a client like this.

BatchClient, err := dataproc.NewBatchControllerClient(context, ...options)

If I wanted to delete a batch job, I could do it using google cloud's golang client library like this (the request body contains the Uuid of the batch)

_, err := batchClient.DeleteBatch(context, request, ...options)

However, there doesn't seem to be any way to cancel a batch that's already running programmatically. If I try to delete a batch that is already running, I rightfully get an error of FAILED_PRECONDITION

Now, I'm aware that Google cloud's SDK cli has a simple way to cancel a job like this:

gcloud dataproc batches cancel (BATCH : --region=REGION) [GCLOUD_WIDE_FLAG …]

Unfortunately, this approach is not a good fit for my application.

2

There are 2 best solutions below

0
On BEST ANSWER

The functionality for serverless job handling was added in version 2.0 of the dataproc golang client library.

To access this version, the following packages had to be updated:

    dataproc "cloud.google.com/go/dataproc/v2/apiv1"

    dataprocpb "cloud.google.com/go/dataproc/v2/apiv1/dataprocpb" 

Afterwards, the provided batch client.CancelOperation can be used to cancel a serverless batch job using the same client that's used to delete a batch job like this:

err := batchClient.CancelOperation(context, request, ...options)
0
On

Before deleting a batch resource you need to make sure that it's in the terminal state (either failed or succeeded).

To achieve this for running batch, you need to cancel it via associated long-running operation: https://cloud.google.com/dataproc-serverless/docs/reference/rest/v1/projects.locations.operations/cancel