This page demonstrates how to create an App Engine task handler, the
worker code that handles an App Engine task. The Cloud Tasks
queue sends HTTP requests to your task handler. Upon successful completion of
processing, the handler must send an HTTP status code between 200
and 299
back to the queue. Any other value indicates the task has failed and the queue
retries the task.
App Engine Task Queue requests are sent from the IP address 0.1.0.2
.
Also refer to the
IP range for requests sent to the App Engine environment.
C#
Go
Java
Node.js
PHP
Python
Ruby
Timeouts
App Engine tasks have specific timeouts that depend on the scaling type of the service that's running them.
For worker services running in the standard environment:
- Automatic scaling: task processing must finish in 10 minutes.
- Manual and basic scaling: requests can run up to 24 hours.
For worker services running in the flex environment: all types have a 60 minute timeout.
If your handler misses the deadline, the queue assumes the task failed and retries it.
Reading App Engine task request headers
Requests sent to your App Engine handler by a Cloud Tasks queue have special headers, which contain task-specific information your handler might want to use.
These headers are set internally. If any of these headers are present in an external user request to your app, they are replaced by the internal ones — except for requests from logged in administrators of the application, who are allowed to set headers for testing purposes.
App Engine task requests always contain the following headers:
Header | Description |
---|---|
X-AppEngine-QueueName |
The name of the queue. |
X-AppEngine-TaskName |
The "short" name of the task, or, if no name was specified at creation, a unique system-generated id. This is the my-task-id value in the complete task name; for example, task_name = projects/my-project-id/locations/my-location/queues/my-queue-id/tasks/my-task-id . |
X-AppEngine-TaskRetryCount |
The number of times that the task has been retried. For the first attempt, this value is 0 . This number includes attempts where the task failed due to a lack of available instances and never reached the execution phase. |
X-AppEngine-TaskExecutionCount |
The number of times that the task has executed and received a response from the handler. Since Cloud Tasks deletes the task once a successful response has been received, all previous handler responses are failures. This number does not include failures due to a lack of available instances. Note that X-AppEngine-TaskExecutionCount can be equal to X-AppEngine-TaskRetryCount if it is updated before an execution is attempted. |
X-AppEngine-TaskETA |
The schedule time of the task, specified in seconds since January 1st 1970. |
If your request handler finds any of the headers listed previously, it can assume that the request is a Cloud Tasks request.
In addition, requests from Cloud Tasks might contain the following headers:
Header | Description |
---|---|
X-AppEngine-TaskPreviousResponse |
The HTTP response code from the previous retry. |
X-AppEngine-TaskRetryReason |
The reason for retrying the task. |
X-AppEngine-FailFast |
Indicates that a task fails immediately if an existing instance is not available. |
Target routing
In App Engine tasks, both the queue and the task handler run within the same Google Cloud project. Traffic is encrypted during transport and never leaves Google datacenters. You cannot explicitly set the protocol (for example, HTTP or HTTPS). The request to the handler, however, will appear to have used the HTTP protocol.
Tasks can be dispatched to secure task handlers, unsecure task handlers, and in
supported runtimes, URIs restricted with
login: admin
.
Because tasks are not run as any user, they cannot be dispatched to URIs
restricted with login: required
.
Task dispatches also don't follow redirects.
What's next
- Learn more about tasks in the RPC API reference.
- Learn more about tasks in the REST API reference.