1 /**
2  * Copyright 2017 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 package com.android.vts.util;
15 
16 import com.google.appengine.api.taskqueue.Queue;
17 import com.google.appengine.api.taskqueue.TaskOptions;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 
23 /** TaskQueueHelper, a helper class for interacting with App Engine Task Queue API. */
24 public class TaskQueueHelper {
25     protected static final Logger logger = Logger.getLogger(TaskQueueHelper.class.getName());
26     public static final int MAX_BATCH_ADD_SIZE = 100;
27 
28     /**
29      * Add the list of tasks to the provided queue.
30      *
31      * @param queue The task queue in which to insert the tasks.
32      * @param tasks The list of tasks to add.
33      */
addToQueue(Queue queue, List<TaskOptions> tasks)34     public static void addToQueue(Queue queue, List<TaskOptions> tasks) {
35         List<TaskOptions> puts = new ArrayList<>();
36         for (TaskOptions task : tasks) {
37             puts.add(task);
38             if (puts.size() == MAX_BATCH_ADD_SIZE) {
39                 queue.addAsync(puts);
40                 puts = new ArrayList<>();
41             } else if (puts.size() > MAX_BATCH_ADD_SIZE) {
42                 logger.log(Level.SEVERE, "Too many tasks batched in the task queue API.");
43                 return;
44             }
45         }
46         if (puts.size() > 0) {
47             queue.addAsync(puts);
48         }
49     }
50 }
51