javaspring-bootgoogle-cloud-platformgoogle-cloud-tasks

How to get task count in GCP cloud task


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?


Solution

  • 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;
        }