@@ -305,6 +305,7 @@ def run(
305
305
reserved_ip_ranges : Optional [List [str ]] = None ,
306
306
sync : Optional [bool ] = True ,
307
307
create_request_timeout : Optional [float ] = None ,
308
+ enable_preflight_validations : Optional [bool ] = False ,
308
309
) -> None :
309
310
"""Run this configured PipelineJob and monitor the job until completion.
310
311
@@ -325,6 +326,8 @@ def run(
325
326
Optional. Whether to execute this method synchronously. If False, this method will unblock and it will be executed in a concurrent Future.
326
327
create_request_timeout (float):
327
328
Optional. The timeout for the create request in seconds.
329
+ enable_preflight_validations (bool):
330
+ Optional. Whether to enable preflight validations for the PipelineJob.
328
331
"""
329
332
network = network or initializer .global_config .network
330
333
@@ -334,6 +337,7 @@ def run(
334
337
reserved_ip_ranges = reserved_ip_ranges ,
335
338
sync = sync ,
336
339
create_request_timeout = create_request_timeout ,
340
+ enable_preflight_validations = enable_preflight_validations ,
337
341
)
338
342
339
343
@base .optional_sync ()
@@ -344,6 +348,7 @@ def _run(
344
348
reserved_ip_ranges : Optional [List [str ]] = None ,
345
349
sync : Optional [bool ] = True ,
346
350
create_request_timeout : Optional [float ] = None ,
351
+ enable_preflight_validations : Optional [bool ] = False ,
347
352
) -> None :
348
353
"""Helper method to ensure network synchronization and to run
349
354
the configured PipelineJob and monitor the job until completion.
@@ -363,12 +368,15 @@ def _run(
363
368
Optional. Whether to execute this method synchronously. If False, this method will unblock and it will be executed in a concurrent Future.
364
369
create_request_timeout (float):
365
370
Optional. The timeout for the create request in seconds.
371
+ enable_preflight_validations (bool):
372
+ Optional. Whether to enable preflight validations for the PipelineJob.
366
373
"""
367
374
self .submit (
368
375
service_account = service_account ,
369
376
network = network ,
370
377
reserved_ip_ranges = reserved_ip_ranges ,
371
378
create_request_timeout = create_request_timeout ,
379
+ enable_preflight_validations = enable_preflight_validations ,
372
380
)
373
381
374
382
self ._block_until_complete ()
@@ -402,6 +410,7 @@ def submit(
402
410
create_request_timeout : Optional [float ] = None ,
403
411
* ,
404
412
experiment : Optional [Union [str , experiment_resources .Experiment ]] = None ,
413
+ enable_preflight_validations : Optional [bool ] = False ,
405
414
) -> None :
406
415
"""Run this configured PipelineJob.
407
416
@@ -432,6 +441,8 @@ def submit(
432
441
433
442
Pipeline parameters will be associated as parameters to the
434
443
current Experiment Run.
444
+ enable_preflight_validations (bool):
445
+ Optional. Whether to enable preflight validations for the PipelineJob.
435
446
"""
436
447
network = network or initializer .global_config .network
437
448
service_account = service_account or initializer .global_config .service_account
@@ -471,12 +482,47 @@ def submit(
471
482
472
483
_LOGGER .log_create_with_lro (self .__class__ )
473
484
474
- self ._gca_resource = self .api_client .create_pipeline_job (
475
- parent = self ._parent ,
476
- pipeline_job = self ._gca_resource ,
477
- pipeline_job_id = self .job_id ,
478
- timeout = create_request_timeout ,
479
- )
485
+ if enable_preflight_validations :
486
+ self ._gca_resource .preflight_validations = True
487
+
488
+ def extract_error_messages (error_string ):
489
+ """
490
+ Extracts error messages from a string containing structured errors.
491
+
492
+ Args:
493
+ error_string: The string containing the error data.
494
+
495
+ Returns:
496
+ A list of formatted error messages.
497
+ """
498
+
499
+ message_pattern = (
500
+ r"CreatePipelineJobApiErrorDetail\"\n.*message=(.*),\ cause=null"
501
+ )
502
+
503
+ matches = re .findall (message_pattern , error_string )
504
+
505
+ formatted_errors = [
506
+ f"{ i + 1 } . { message } " for i , message in enumerate (matches )
507
+ ]
508
+
509
+ return formatted_errors
510
+
511
+ try :
512
+ self ._gca_resource = self .api_client .create_pipeline_job (
513
+ parent = self ._parent ,
514
+ pipeline_job = self ._gca_resource ,
515
+ pipeline_job_id = self .job_id ,
516
+ timeout = create_request_timeout ,
517
+ )
518
+ except Exception as e :
519
+ preflight_validations_error_messages = extract_error_messages (str (e ))
520
+ if preflight_validations_error_messages :
521
+ raise Exception (
522
+ "PipelineJob Preflight validations failed with the following errors:\n "
523
+ + "\n " .join (preflight_validations_error_messages )
524
+ ) from e
525
+ raise
480
526
481
527
_LOGGER .log_create_complete_with_getter (
482
528
self .__class__ , self ._gca_resource , "pipeline_job"
0 commit comments