gc_batch.client
Google Cloud Batch client for creating and managing Batch jobs.
This module provides the main client class for interacting with the Google Cloud Batch API. It handles job creation, listing, status monitoring, and log retrieval.
GCBatchClient
Client for managing Google Cloud Batch jobs.
This is the main entry point for interacting with Google Cloud Batch. It provides methods for creating jobs, listing jobs with filters, checking job status, and retrieving job logs.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
The BatchClientConfig with project and location settings. |
|
client |
The underlying Google Cloud Batch API client. |
|
batch_logging |
Helper for Cloud Logging operations. |
|
gcs_logging |
Helper for reading logs written to a GCS bucket. |
Example
from gc_batch import GCBatchClient, BatchClientConfig, JobRequest, BatchJobConfig
# Initialize the client
config = BatchClientConfig(project_id="my-project", location="us-central1")
client = GCBatchClient(config)
# Create a job
job_config = BatchJobConfig(machine_type="e2-standard-2", boot_disk_type="pd-balanced")
request = JobRequest(
job_name="my-analysis",
docker_image="python:3.12",
command="python /app/main.py",
config=job_config,
)
job = client.create_job(request)
print(f"Created job: {job.name}")
# List jobs by label
jobs = client.list_jobs(labels={"team": "data-science"})
# Get job status
job = client.get_job("my-analysis-1234567890")
print(f"Status: {job.status.state.name}")
# Cancel a job
client.cancel_job("projects/my-project/locations/us-central1/jobs/my-job")
Source code in src/gc_batch/client.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
batch_logging = BatchLogging(config=batch_client_config, logger=(self.logging))
instance-attribute
client = batch_v1.BatchServiceClient()
instance-attribute
config = batch_client_config
instance-attribute
gcs_logging = GCSLogReader(config=batch_client_config, logger=(self.logging))
instance-attribute
logging = logger or get_logger(name='GCBatchClient', level=log_level)
instance-attribute
__init__(batch_client_config, logger=None, log_level=logging.INFO)
Initialize the GCBatchClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_client_config
|
BatchClientConfig
|
Configuration with project_id and location settings. |
required |
Source code in src/gc_batch/client.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
cancel_job(job_name)
Cancel a running or queued job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The full job resource path (e.g., "projects/my-project/locations/us-central1/jobs/my-job"). |
required |
Example
job = client.get_job("my-job-name")
client.cancel_job(job.name) # Use the full name from the job object
Source code in src/gc_batch/client.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
create_job(job_request)
Create a new Google Cloud Batch job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_request
|
JobRequest
|
The JobRequest containing job configuration and settings. |
required |
Returns:
| Type | Description |
|---|---|
Job
|
The created GCSBatchJob object with job details. |
Raises:
| Type | Description |
|---|---|
Exception
|
If job creation fails. |
Example
config = BatchJobConfig(machine_type="e2-standard-2", boot_disk_type="pd-balanced")
request = JobRequest(
job_name="my-job",
docker_image="python:3.12",
command="python main.py",
config=config,
)
job = client.create_job(request)
print(f"Job created: {job.name}")
Source code in src/gc_batch/client.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
get_cloud_logging_url(job, severity='DEFAULT', agent_logs=False, custom_label_filters=None)
Get a URL to the Google Cloud Logging console filtered for this job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The GCSBatchJob object to generate a URL for |
required |
severity
|
str
|
Minimum severity level (e.g., "DEFAULT", "INFO", "WARNING", "ERROR") |
'DEFAULT'
|
agent_logs
|
bool
|
Whether to include agent logs (default: False) |
False
|
custom_label_filters
|
dict[str, str] | None
|
Optional mapping of label key to label value to filter the query by. If provided, overrides the job_uid filter. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
URL to the Cloud Logging console with filters applied |
Source code in src/gc_batch/client.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
get_failure_message(job)
Get concise failure information for a job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The failed Google Cloud Batch job |
required |
Returns:
| Type | Description |
|---|---|
str
|
Concise failure message with essential error details |
Source code in src/gc_batch/client.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
get_job(job_name)
Get a specific job by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The short job name (not the full resource path). |
required |
Returns:
| Type | Description |
|---|---|
Job
|
The GCSBatchJob object with full job details. |
Example
job = client.get_job("my-analysis-1234567890")
print(f"Status: {job.status.state.name}")
print(f"Created: {job.create_time}")
Source code in src/gc_batch/client.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
list_jobs(labels=None, name=None, since_time=None, status=None, page_size=100)
List Batch jobs filtered by specific labels, name, since time, and status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
dict[str, str] | None
|
Dictionary of label key-value pairs to filter by |
None
|
name
|
str | None
|
Filter by job name |
None
|
since_time
|
datetime | None
|
Filter by update time |
None
|
status
|
str | None
|
Filter by job status (RUNNING, SUCCEEDED, FAILED, QUEUED, etc.) |
None
|
page_size
|
int
|
Number of jobs to return per page (max 1000) |
100
|
Returns:
| Type | Description |
|---|---|
list[Job]
|
List of Batch jobs with matching labels, name, since time, and status |
Examples:
List jobs with specific label
jobs = client.list_jobs({"environment": "production"})
List jobs with multiple labels and name
jobs = client.list_jobs({ "team": "data-science", "project": "genomics", }, "genome-sequencing")
List jobs created in the last day
jobs = client.list_jobs(since_time=datetime.now() - timedelta(days=1))
List only failed jobs
jobs = client.list_jobs(status="FAILED")
List running jobs with specific label
jobs = client.list_jobs({"team": "research"}, status="RUNNING")
List all jobs (no filters)
jobs = client.list_jobs()
Source code in src/gc_batch/client.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
setup_gcs_volume(bucket_name, mount_path, disk_mount_path, read_only=False, billing_project=None, implicit_dirs=False)
Configure a GCS bucket as a mounted volume.
Creates a Volume configuration that mounts a Google Cloud Storage bucket path into the container, allowing the job to read/write files directly to GCS as if they were local files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_name
|
str
|
GCS bucket path (e.g., "my-bucket/data/input"). |
required |
mount_path
|
str
|
Mount path inside the container (e.g., "/mnt/input"). |
required |
disk_mount_path
|
str
|
Physical mount path on the VM. |
required |
read_only
|
bool
|
If True, pass gcsfuse's |
False
|
billing_project
|
str | None
|
Billing project for requester-pays buckets. |
None
|
implicit_dirs
|
bool
|
If True, pass |
False
|
Returns:
| Type | Description |
|---|---|
Volume
|
Tuple of (Volume, volume_config_string) where volume_config_string |
str
|
is the Docker volume mount format "host_path:container_path". |
Example
volume, config = client.setup_gcs_volume(
bucket_name="my-bucket/input-data",
mount_path="/mnt/input",
disk_mount_path="/mnt/disks/input",
billing_project="my-billing-project",
)
Source code in src/gc_batch/client.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
setup_local_ssd(config)
Configure a local SSD for high-performance storage.
Creates the configuration to attach a local SSD to the job VM for high-performance ephemeral storage. Local SSDs provide very high IOPS and low latency, ideal for temporary data processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BatchJobConfig
|
BatchJobConfig with local_ssd_size_gb, local_ssd_device_name, and local_ssd_mount_path settings. |
required |
Returns:
| Type | Description |
|---|---|
tuple[AttachedDisk, Volume, str]
|
Tuple of (AttachedDisk, Volume, volume_config_string). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If local_ssd_size_gb is None or not a multiple of 375 GB. |
Note
- Local SSD size must be a multiple of 375 GB.
- Data on local SSDs is ephemeral and lost when the VM terminates.
- Local SSDs are automatically formatted and mounted by Batch.
Example
config = BatchJobConfig(
machine_type="n2-standard-4",
boot_disk_type="pd-balanced",
local_ssd_size_gb=375,
local_ssd_mount_path="/mnt/fast",
)
attached_disk, volume, volume_config = client.setup_local_ssd(config)
Source code in src/gc_batch/client.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
setup_volume(disk_type, disk_size, device_name, mount_path)
Create an attached disk and volume configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disk_type
|
str
|
The disk type (e.g., "pd-balanced", "pd-ssd"). |
required |
disk_size
|
int
|
Disk size in GB. |
required |
device_name
|
str
|
Device name for the disk. |
required |
mount_path
|
str
|
Path where the disk will be mounted in the VM. |
required |
Returns:
| Type | Description |
|---|---|
tuple[AttachedDisk, Disk]
|
Tuple of (AttachedDisk, Disk) configurations. |
Source code in src/gc_batch/client.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |