Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文(简体) - 中文(繁體)
Pull queues allow you to design your own system to consume App Engine tasks. The task consumer can be part of your App Engine app (such as a backend) or a system outside of App Engine (using the Task Queue REST API). The task consumer leases a specific number of tasks for a specific duration, then processes and deletes them before the lease ends.
Using pull queues requires your application to handle some functions that are automated in push queues:
Pull queues require a specific configuration in queue.xml
. For more information, please see Defining Pull Queues on the Task Queue configuration page.
The following sections describe the process of enqueuing, leasing, and deleting tasks using pull queues.
Pull queues allow a task consumer to process tasks outside of App Engine's default task processing system. If the task consumer is a part of your App Engine app, you can manipulate tasks using simple API calls from the com.google.appengine.api.taskqueue package. Task consumers outside of App Engine can pull tasks using the Task Queue REST API.
The process works like this:
You can use pull queues within the App Engine environment using simple API calls to add tasks to a pull queue, lease them, and delete them after processing.
Before you begin, make sure to configure the pull queue in queue.xml
.
To add tasks to a pull queue, simply get the queue using the queue name defined in queue.xml
, and use TaskOptions.Method.PULL. The following example enqueues tasks in a pull queue named pull-queue
:
Queue q = QueueFactory.getQueue("pull-queue"); q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) .payload("hello world"));
Once you have added tasks to a pull queue, you can lease one or more tasks using leaseTasks(). There may be a short delay before tasks recently added using add() become available via leaseTasks(). When you request a lease, you specify the number of tasks to lease (up to a maximum of 1,000 tasks) and the duration of the lease in seconds (up to a maximum of one week). The lease duration needs to be long enough to ensure that the slowest task will have time to finish before the lease period expires. You can extend a task lease using modifyTaskLease()
Leasing a task makes it unavailable for processing by another worker, and it remains unavailable until the lease expires. If you lease an individual task, the API selects the task from the front of the queue. If no such task is available, an empty list is returned.
Note: leaseTasks() operates only on pull queues. If you attempt to lease tasks added in a push queue, App Engine throws an exception. You can change a push queue to a pull queue by changing its definition in queue.xml
. Please see Defining Pull Queues for more information. The following code sample leases 100 tasks from the queue pull-queue
for one hour:
Queue q = QueueFactory.getQueue("pull-queue"); List<TaskHandle> tasks = q.leaseTasks(3600, TimeUnit.SECONDS, 100);
In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing them, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.
You can delete an individual task or a list of tasks using deleteTask(). You must know the name of a task in order to delete it. If you are deleting tasks from a pull queue, you can find task names in the Task object returned by leaseTasks(). If you are deleting tasks from a push queue, you need to know the task name through some other means (for example, if you created the task explicitly).
The following code sample demonstrates how to deletes a task named foo
from the queue named pull-queue
:
Queue q = QueueFactory.getQueue("pull-queue"); q.deleteTask("foo");
You can use App Engine Backends as workers to lease and process pull queue tasks. Backends allow you to process more work without having to worry about request deadlines and other restrictions normally imposed by App Engine. Using backends with pull queues gives you processing efficiencies by allowing you to batch task processing using leases.
For more information about using backends, check out the Backends documentation.
If you need to use pull queues from outside App Engine, you must use the Task Queue REST API. The REST API is a Google web service accessible at a globally-unique URI of the form:
https://www.googleapis.com/taskqueue/v1beta1/projects/taskqueues
Google provides the following client libraries that you can use to call the Task Queue methods remotely:
In the table below, the first column shows each library's stage of development; note that some are still in early stages. The second column links to the main page for each library.
For libraries that have samples for the Google Task Queue, the third column in the table below links to them directly. If a library's samples page does not yet include a sample for this API, you can still use that library -- simply adapt one of the existing samples as needed.
Client library | Public repository | All client library samples |
---|---|---|
Google APIs Client Library for .NET (beta) | google-api-dotnet-client/ | .NET samples |
Google APIs Client Library for Go (alpha) | google-api-go-client/ | Go samples |
Google API Libraries for Google Web Toolkit (alpha) | gwt-google-apis/ | GWT samples |
Google APIs Client Library for Java (beta) | google-api-java-client/ | Java samples |
Google APIs Client Library for JavaScript (alpha) | google-api-javascript-client/ | JavaScript samples td> |
Google APIs Client Library for Objective C (alpha) | google-api-objectivec-client/ | Objective-C samples |
Google APIs Client Library for PHP (beta) | google-api-php-client/ | PHP samples |
Google APIs Client Library for Python (beta) | google-api-python-client/ | Python samples |
Google APIs Client Library for Ruby (alpha) | google-api-ruby-client/ | Ruby samples |
The REST API uses OAuth as the authorization mechanism. When you configure your pull queue, make sure that your queue.xml
file supplies
the email addresses of the users that can access the queue using the REST API.
The OAuth scope for all methods is https://www.googleapis.com/auth/taskqueue
.
This section demonstrates the use of the REST API in an application called gtaskqueue. The gtaskqueue tool allows you to interact with the REST API via the command line and also provides an example of how to lease and delete tasks. The sections below show the basics of using the Client Library with relevant code snippets from the gtaskqueue tool.
Most of the functionality of the Client Library is encapsulated in an HttpTransport object. This object stores your authentication header and the default headers to be used with every request. Once the authentication is complete (see Auth.java in the sample code for how OAuth is handled), we set up an HttpTransport instance that will be used for all requests. From Util.java:
import com.google.api.client.googleapis.GoogleHeaders; import com.google.api.client.googleapis.GoogleUtils; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; ... public static final HttpTransport TRANSPORT = newTransport(); ... static HttpTransport newTransport() { HttpTransport result = new NetHttpTransport(); GoogleUtils.useMethodOverride(result); GoogleHeaders headers = new GoogleHeaders(); headers.setApplicationName("Google-TaskQueueSample/1.0"); result.defaultHeaders = headers; return result; } ...
The HttpTransport instance can now be used to make requests to the REST API. For example, here is the code from TaskQueue.java that calls TaskQueue.Get and displays information about the queue:
public void get(String projectName, String taskQueueName, boolean getStats) throws IOException { HttpRequest request = Util.TRANSPORT.buildGetRequest(); request.url = TaskQueueUrl.forTaskQueueServiceQueues(projectName, taskQueueName, getStats); return request.execute().parseAs(TaskQueue.class); }
The following sections describe the two most common functions used with the Task Queue API, allowing you to lease and delete tasks.
The Java library provides methods that invoke the Tasks.lease method in the REST API. When you create a lease, you need to specify the number of tasks to lease (up to a maximum of 1,000 tasks); the API returns the specified number of tasks in order of the oldest task ETA.
You also need to specify the the duration of the lease in seconds (up to a maximum of one week). The lease must be long enough so you can finish all the leased tasks, yet short enough so that tasks can be made available for leasing if your consumer crashes. Similarly, if you lease too many tasks at once and your client crashes, a large number of tasks will become unavailable until the lease expires.
The following code from the gtaskqueue sample (in TaskQueueSample.java) shows how to lease tasks:
private static void leaseAndDeleteTask() throws IOException { Task task = new Task(); ListleasedTasks = task.lease(projectName, taskQueueName, leaseSecs, numTasks); if (leasedTasks.size() == 0) { System.out.println("No tasks to lease and hence exiting"); } for (Task leasedTask : leasedTasks) { leasedTask.executeTask(); leasedTask.delete(); } }
This code enables the command-line tool to lease a specified number of tasks for a set duration:
mvn -q exec:java -Dexec.args="appengtaskpuller 30 100"
When run, this command-line tool constructs the following URI call to the REST API:
POST https://www.googleapis.com/taskqueue/v1beta1/projects/taskqueues/appengtaskpuller/tasks/lease?alt=json&lease_secs=30&numTasks=100
This request returns an array of 100 tasks with the following JSON structure:
{ "kind": "taskqueues#tasks", "items": [ { "kind": "taskqueues#task", "id": string, "queueName": string, "payloadBase64": string, "enqueueTimestamp": number, "leaseTimestamp": number } ... ] }
After processing each task, you need to delete it, as described in the following section.
In general, once a worker completes a task, it needs to delete the task from the queue. If you see tasks remaining in a queue after a worker finishes processing, it is likely that the worker failed; in this case, the tasks need to be processed by another worker.
You can delete an individual task or a list of tasks using the REST method Task.delete. You must know the name of a task in order to delete it. You can get the task name from the id field of the Task object returned by Task.lease.
Call delete if you have finished a task, even if you have exceeded the lease time. Tasks should be idempotent, so even if a task lease expires and another client leases the task, performing the same task twice should not cause an error.
Note: When you delete a task, it immediately becomes invisible to queries, but its name remains in the system for up to three days. During this time, attempting to create another task with the same name will result in an "item exists" error. The system offers no method to determine if deleted task names are still in the system. To avoid these issues, we recommend that you let App Engine generate the task name automatically.
Returning to our gtaskqueue example, the following code snippet (from TaskQueueSample.java) deletes tasks from a queue:
private static void leaseAndDeleteTask() throws IOException { Task task = new Task(); List<Task> leasedTasks = task.lease(projectName, taskQueueName, leaseSecs, numTasks); if (leasedTasks.size() == 0) { System.out.println("No tasks to lease and hence exiting"); } for (Task leasedTask : leasedTasks) { leasedTask.executeTask(); leasedTask.delete(); } }
The delete method implementation can be seen in Task.java:
public void delete() throws HttpResponseException, IOException { HttpRequest request = Util.TRANSPORT.buildDeleteRequest(); String projectName = getProjectFromQueueName(); String queueName = getQueueFromQueueName(); if (projectName.isEmpty() || queueName.isEmpty()) { System.out.println("Error parsing full queue name:" + this.queueName + " Hence unable to delete task" + this.id); return; } request.url = TaskQueueUrl.forTaskQueueServiceTasks(projectName, queueName); request.url.pathParts.add(this.id); try { request.execute(); } catch (HttpResponseException hre) { System.out.println("Error deleting task: " + this.id); throw hre; } }
If the delete command is successful, the REST API returns an HTTP 200 response. If deletion fails, the API returns an HTTP failure code.
Enqueuing a task counts counts toward the following quotas:
Leasing a task counts toward the following quotas:
The Task Queue Stored Task Bytes quota is configurable in queue.xml
by setting <total-storage-limit>. This quota counts towards your Stored Data (billable) quota.
In addition to quotas, the following limits apply to the use of pull queues:
Limit | Amount |
---|---|
Task Object Size | 1MB |
Number of Active Queues (Not Including the Default Queue) | 10 for free apps 100 for billed apps |
Maximum Number of Tasks That Can Be Added in a Batch | 100 tasks |
Maximum Number of Tasks That Can Be Added in a Transaction | 5 tasks |
Maximum Number of Tasks That You Can Lease in a Single leaseTasks() Operation | 1000 tasks |
Maximum Payload Size when Leasing a Batch of Tasks | 32MB 1MB when using the REST API |