Skip to content

Commit ec57cf4

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Implement method to get number of pending jobs.
PiperOrigin-RevId: 716684266
1 parent 1d1b3f0 commit ec57cf4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/unit/vertexai/test_batch_prediction.py

+6
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,9 @@ def tes_list_batch_prediction_jobs(self, list_batch_prediction_jobs_mock):
723723
list_batch_prediction_jobs_mock.assert_called_once_with(
724724
request={"parent": _TEST_PARENT}
725725
)
726+
727+
def test_num_pending_jobs(self, list_batch_prediction_jobs_mock):
728+
num_pending_jobs = batch_prediction.BatchPredictionJob.num_pending_jobs()
729+
730+
assert num_pending_jobs == 1
731+
list_batch_prediction_jobs_mock.assert_called_once()

vertexai/batch_prediction/_batch_prediction.py

+22
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,28 @@ def _is_genai_model(cls, model_name: str) -> bool:
328328

329329
return False
330330

331+
@classmethod
332+
def num_pending_jobs(cls) -> int:
333+
"""Returns the number of pending batch prediction jobs.
334+
335+
The pending jobs are those defined in _JOB_PENDING_STATES from
336+
google/cloud/aiplatform/jobs.py
337+
e.g. JOB_STATE_QUEUED, JOB_STATE_PENDING, JOB_STATE_RUNNING,
338+
JOB_STATE_CANCELLING, JOB_STATE_UPDATING.
339+
It will be used to manage the number of concurrent batch that is limited
340+
according to
341+
https://cloud.google.com/vertex-ai/generative-ai/docs/quotas#concurrent-batch-requests
342+
"""
343+
return len(
344+
cls._list(
345+
cls_filter=lambda gca_resource: cls._is_genai_model(gca_resource.model),
346+
filter=" OR ".join(
347+
f'state="{pending_state.name}"'
348+
for pending_state in jobs._JOB_PENDING_STATES
349+
),
350+
)
351+
)
352+
331353
@classmethod
332354
def _complete_bq_uri(cls, uri: Optional[str] = None):
333355
"""Completes a BigQuery uri to a BigQuery table uri."""

0 commit comments

Comments
 (0)