gc_batch.utils
Utility functions for gc-batch.
This module provides helper functions for working with Google Cloud Batch jobs, including authentication utilities, time formatting, and job state checking.
format_time_for_filter(timestamp)
Convert a timestamp to RFC3339 format for Cloud Logging filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime | str | None
|
A timestamp object (e.g., from job.create_time or job.update_time) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str | None
|
RFC3339 formatted timestamp string, or None if timestamp is None |
Source code in src/gc_batch/utils.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
get_current_account()
Get the current authenticated Google Cloud account.
Tries to get the account from gcloud config first, then falls back to google.auth.default() if gcloud is not available.
Returns:
| Type | Description |
|---|---|
str | None
|
The email address of the current authenticated account, or None if |
str | None
|
authentication fails. |
Example
account = get_current_account()
if account:
print(f"Logged in as: {account}")
else:
print("Not authenticated")
Source code in src/gc_batch/utils.py
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 | |
get_current_region()
Get the GCP region of the machine gc-batch is running on.
Asks the GCE metadata server for the current VM's zone (e.g.
projects/123/zones/us-east1-b) and strips the zone suffix. In the All of
Us Researcher Workbench the notebook VM runs in the workspace's region, so
this is the region jobs must be placed in. Falls back to the
compute/region property of the active gcloud configuration when the
metadata server is unreachable (i.e. when not running on GCE).
Every command calls this when --location is not given, so it avoids
running gcloud (which takes most of a second) and reads its config files.
Returns:
| Type | Description |
|---|---|
str | None
|
The region name (e.g. "us-east1"), or None if it could not be determined. |
Source code in src/gc_batch/utils.py
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 | |
get_formatted_start_and_end_time(job, include_end_time_when_not_finished=False)
Get the formatted start and end time for a job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The GCSBatchJob object to get the start and end time for |
required |
include_end_time_when_not_finished
|
bool
|
Whether to include the end time when the job is not finished |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str | None, str | None]
|
Tuple[Optional[str], Optional[str]]: The formatted start and end time for the job - RFC3339 formatted timestamp string - RFC3339 formatted timestamp string, or None if timestamp is None - If include_end_time_when_not_finished is True and the end time is None, the end time will be the current time. - If the current time is within 20 minutes of the start time, the end time will start time + 1 hour. |
Source code in src/gc_batch/utils.py
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 | |
get_gc_batch_version()
Get the version of the gc-batch package.
Returns:
| Type | Description |
|---|---|
str
|
The version string of the gc-batch package. |
Source code in src/gc_batch/utils.py
21 22 23 24 25 26 27 28 29 | |
get_start_and_end_time(job)
Get the start and end time for a job. If the job is finished, the end time is the update time. If the job is not finished, the end time is None. The start time is the create time. Returns: Tuple[str, Optional[str]]: The start and end time for the job The start time is the create time. The end time is the update time if the job is finished, otherwise None.
Source code in src/gc_batch/utils.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
is_job_finished(job)
Check if a job has reached a terminal state.
Terminal states include SUCCEEDED, FAILED, CANCELLED, DELETION_IN_PROGRESS, and CANCELLATION_IN_PROGRESS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
Job
|
The GCSBatchJob to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the job is in a terminal state, False otherwise. |
Example
job = client.get_job("my-job")
if is_job_finished(job):
print(f"Job finished with state: {job.status.state.name}")
Source code in src/gc_batch/utils.py
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 | |
resolve_created_by_label(owner_email_env_vars)
Resolve the "created-by" label value from the current environment.
The first environment variable in owner_email_env_vars that is set
provides the value. Email-shaped values are reduced to their local part and
normalized for GCP label rules (e.g. "jane.doe@example.com" becomes
"jane-doe").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_email_env_vars
|
list[str]
|
Environment variable names to check, in order.
Typically |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label value, or "unknown" when none of the variables are set. |
Example
from gc_batch import GCBatchSettings
from gc_batch.utils import resolve_created_by_label
settings = GCBatchSettings()
print(resolve_created_by_label(settings.owner_email_env_vars))
Source code in src/gc_batch/utils.py
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 | |