Reputation: 21
Is there any standard way to get cloud task count for a queue?
I know we can loop through nextPageToken
and achieve this.
Can this be achieved with single request?
Upvotes: 1
Views: 1332
Reputation: 1203
Noting the REST API documentation here so that it's applicable to more than just Java: https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues/get
v2beta3 introduces the readMask
query param, which is described as:
Optional. Read mask is used for a more granular control over what the API returns. If the mask is not present all fields will be returned except [Queue.stats]. [Queue.stats] will be returned only if it was explicitly specified in the mask.
This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo".
If you pass readMask=stats
, the response body will look like this:
{
"stats": {
"tasksCount": "474105",
"oldestEstimatedArrivalTime": "2024-11-13T12:37:59.453271Z",
"concurrentDispatchesCount": "5",
"effectiveExecutionRate": 500
}
}
The properties are documented here https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues#queuestats
Upvotes: 0
Reputation: 1420
By looking at the documentation, QueueStats seems to be the standard way to get task count.
As mentioned in @Sameer comment, cloudtask v2beta3 has a feature to request for stats of a queue:
public long geQueueDepth(String queuePath) {
long depth=-1;
try {
FieldMask.Builder fieldBuilder = FieldMask.newBuilder();
fieldBuilder.addPaths("stats");
com.google.cloud.tasks.v2beta3.GetQueueRequest queueRequest =
com.google.cloud.tasks.v2beta3.GetQueueRequest.newBuilder().setName(queuePath).setReadMask(fieldBuilder).build();;
com.google.cloud.tasks.v2beta3.Queue queue = cloudTasksClientV3.getQueue(queueRequest);
depth=queue.getStats().getTasksCount();
}catch (Exception e){
LOGGER.error("Error while getting queue depth =>{}",e.getMessage());
}
return depth;
}
Upvotes: 2