@@ -2055,6 +2055,9 @@ class DataprocCreateBatchOperator(BaseOperator):
20552055 If set as a sequence, the identities from the list must grant
20562056 Service Account Token Creator IAM role to the directly preceding identity, with first
20572057 account from the list granting this role to the originating account (templated).
2058+ :param asynchronous: Flag to return after creating batch to the Dataproc API.
2059+ This is useful for creating long-running batch and
2060+ waiting on them asynchronously using the DataprocBatchSensor
20582061 """
20592062
20602063 template_fields : Sequence [str ] = (
@@ -2080,6 +2083,7 @@ def __init__(
20802083 gcp_conn_id : str = "google_cloud_default" ,
20812084 impersonation_chain : str | Sequence [str ] | None = None ,
20822085 result_retry : Retry | _MethodDefault = DEFAULT ,
2086+ asynchronous : bool = False ,
20832087 ** kwargs ,
20842088 ):
20852089 super ().__init__ (** kwargs )
@@ -2095,6 +2099,7 @@ def __init__(
20952099 self .gcp_conn_id = gcp_conn_id
20962100 self .impersonation_chain = impersonation_chain
20972101 self .operation : operation .Operation | None = None
2102+ self .asynchronous = asynchronous
20982103
20992104 def execute (self , context : Context ):
21002105 hook = DataprocHook (gcp_conn_id = self .gcp_conn_id , impersonation_chain = self .impersonation_chain )
@@ -2114,10 +2119,13 @@ def execute(self, context: Context):
21142119 )
21152120 if self .operation is None :
21162121 raise RuntimeError ("The operation should be set here!" )
2117- result = hook .wait_for_operation (
2118- timeout = self .timeout , result_retry = self .result_retry , operation = self .operation
2119- )
2120- self .log .info ("Batch %s created" , self .batch_id )
2122+ if not self .asynchronous :
2123+ result = hook .wait_for_operation (
2124+ timeout = self .timeout , result_retry = self .result_retry , operation = self .operation
2125+ )
2126+ self .log .info ("Batch %s created" , self .batch_id )
2127+ else :
2128+ return self .operation .operation .name
21212129 except AlreadyExists :
21222130 self .log .info ("Batch with given id already exists" )
21232131 if self .batch_id is None :
@@ -2130,7 +2138,6 @@ def execute(self, context: Context):
21302138 timeout = self .timeout ,
21312139 metadata = self .metadata ,
21322140 )
2133-
21342141 # The existing batch may be a number of states other than 'SUCCEEDED'
21352142 if result .state != Batch .State .SUCCEEDED :
21362143 if result .state == Batch .State .FAILED or result .state == Batch .State .CANCELLED :
@@ -2355,3 +2362,59 @@ def execute(self, context: Context):
23552362 )
23562363 DataprocListLink .persist (context = context , task_instance = self , url = DATAPROC_BATCHES_LINK )
23572364 return [Batch .to_dict (result ) for result in results ]
2365+
2366+
2367+ class DataprocCancelOperationOperator (BaseOperator ):
2368+ """
2369+ Cancel the batch workload resource.
2370+
2371+ :param operation_name: Required. The name of the operation resource to be cancelled.
2372+ :param region: Required. The Cloud Dataproc region in which to handle the request.
2373+ :param project_id: Optional. The ID of the Google Cloud project that the cluster belongs to.
2374+ :param retry: A retry object used to retry requests. If ``None`` is specified, requests will not be
2375+ retried.
2376+ :param timeout: The amount of time, in seconds, to wait for the request to complete. Note that if
2377+ ``retry`` is specified, the timeout applies to each individual attempt.
2378+ :param metadata: Additional metadata that is provided to the method.
2379+ :param gcp_conn_id: The connection ID to use connecting to Google Cloud.
2380+ :param impersonation_chain: Optional service account to impersonate using short-term
2381+ credentials, or chained list of accounts required to get the access_token
2382+ of the last account in the list, which will be impersonated in the request.
2383+ If set as a string, the account must grant the originating account
2384+ the Service Account Token Creator IAM role.
2385+ If set as a sequence, the identities from the list must grant
2386+ Service Account Token Creator IAM role to the directly preceding identity, with first
2387+ account from the list granting this role to the originating account (templated).
2388+ """
2389+
2390+ template_fields : Sequence [str ] = ("operation_name" , "region" , "project_id" , "impersonation_chain" )
2391+
2392+ def __init__ (
2393+ self ,
2394+ * ,
2395+ operation_name : str ,
2396+ region : str ,
2397+ project_id : str | None = None ,
2398+ retry : Retry | _MethodDefault = DEFAULT ,
2399+ timeout : float | None = None ,
2400+ metadata : Sequence [tuple [str , str ]] = (),
2401+ gcp_conn_id : str = "google_cloud_default" ,
2402+ impersonation_chain : str | Sequence [str ] | None = None ,
2403+ ** kwargs ,
2404+ ):
2405+ super ().__init__ (** kwargs )
2406+ self .operation_name = operation_name
2407+ self .region = region
2408+ self .project_id = project_id
2409+ self .retry = retry
2410+ self .timeout = timeout
2411+ self .metadata = metadata
2412+ self .gcp_conn_id = gcp_conn_id
2413+ self .impersonation_chain = impersonation_chain
2414+
2415+ def execute (self , context : Context ):
2416+ hook = DataprocHook (gcp_conn_id = self .gcp_conn_id , impersonation_chain = self .impersonation_chain )
2417+
2418+ self .log .info ("Canceling operation: %s" , self .operation_name )
2419+ hook .get_operations_client (region = self .region ).cancel_operation (name = self .operation_name )
2420+ self .log .info ("Operation canceled." )
0 commit comments