[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eA task handler processes push tasks, and its URL mapping is defined in the \u003ccode\u003eapp.yaml\u003c/code\u003e file, similar to other request handlers, allowing for flexible organization across single or multiple services.\u003c/p\u003e\n"],["\u003cp\u003eTask handlers must return an HTTP status code between 200-299 to signal success; any other code, besides 503, indicates failure and triggers a retry.\u003c/p\u003e\n"],["\u003cp\u003eTask handlers need to be idempotent because App Engine guarantees "at least once" delivery, and on rare occasions, multiple executions can occur.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine sends special headers with each task request, such as \u003ccode\u003eX-Appengine-QueueName\u003c/code\u003e, \u003ccode\u003eX-Appengine-TaskName\u003c/code\u003e, and \u003ccode\u003eX-Appengine-TaskRetryCount\u003c/code\u003e, which can be used to identify and manage the task's execution and retries.\u003c/p\u003e\n"],["\u003cp\u003eTo secure task handlers from external access, URLs can be restricted to App Engine administrators by adding \u003ccode\u003elogin: admin\u003c/code\u003e to the handler configuration in \u003ccode\u003eapp.yaml\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Creating Task Handlers\n\nThis page describes how to create a *task handler* , the code that handles a push\ntask. You must provide a request handler to process the task. The mapping from\nthe request URL to the appropriate handler is declared in your service's `app.yaml`, just\nlike any other request handler. Because you control how to map task requests to\na handler, you're free to organize your task handlers. If your application\nprocesses many different kinds of tasks, you can add all the handlers to a\nsingle service, or you can distribute them among multiple services.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| php-gen2\n|\n| /services/access). If you are updating to the App Engine PHP 7/8 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/php-differences) to learn about your migration options for legacy bundled services.\n\nWriting a push task request handler\n-----------------------------------\n\nIn the queue, the Task Queue service creates an HTTP header and sends it to an\ninstance of the worker service specified by the task's target. Task Queue requests are\nsent from the IP address `0.1.0.2`.\n\nYour handler does not need to be written in the same language that created and\nenqueued the task if the handler is in a\n[separate service](/appengine/docs/legacy/standard/php/an-overview-of-app-engine).\n\nWhen you write your handler, follow these guidelines:\n\n- The code must return an HTTP status code within the range 200--299 to\n indicate success. Any other code indicates that the task failed.\n\n | **Note:** App Engine itself returns a `503` status code when instances are overloaded or otherwise unavailable. The Task Queue service responds to this by slowing delivery from queues to handlers, to avoid making the problem worse. If you wish to trigger a retry intentionally, use any status code *other* than `2xx` or `503`.\n- Push tasks have a fixed [completion\n deadline](/appengine/docs/legacy/standard/php/taskqueue/push#the_task_deadline)\n that depends on the scaling type of the service that's running them.\n Automatic scaling services must finish before 10 minutes have elapsed.\n Manual and basic scaling services can run up to 24 hours. If your handler\n misses the deadline, the Task Queue service assumes the task failed and\n will retry it.\n\n \u003cbr /\u003e\n\n- The handler must be [idempotent](https://wikipedia.org/wiki/idempotent).\n App Engine's Task Queue API is designed to provide \"at least once\"\n delivery; that is, if a task is successfully added, App Engine will deliver\n it to a handler at least once. Note that in some rare circumstances, multiple task\n execution is possible, so your code must ensure that there are no harmful\n side-effects of repeated execution.\n\nTask Queue uses the HTTP code in the handler's response to determine if the\ntask succeeded. The response from the handler is seen only by the Task Queue service\nand only to determine if the task succeeded. The queue ignores all other fields\nin the response. Then the service discards the response. The originating app **never\nreceives** any of the data. If a task fails, the Task Queue service retries the\ntask by sending another request.\n\nUser-supplied data can be delivered to the handler in the request as a query string or as a\npayload in the request body. Inserting user data is described in\n[Creating Tasks](/appengine/docs/legacy/standard/php/taskqueue/push/creating-tasks#adding_user_data). If the request includes data, the handler must know how\nit was inserted into the request. The exact code you use to fetch the data from\nthe request depends on the particular web framework you're using.\n\nTo test a task handler, sign in as an administrator and visit the handler's URL in your browser.\n\nReading request headers\n-----------------------\n\nA push task HTTP request has special headers set by App Engine, which contain\ntask-specific information your handler can use.\n\nIf these headers are present in an external user request to your app, they are stripped\nand replaced. The sole exception is for requests from logged-in administrators\nof the application, who are allowed to set headers for testing purposes. On the other hand, headers\nare not removed when your app is running in the development server.\n\nRequests from Task Queue will always contain the following headers:\n\nIf your request handler finds any of the headers listed above, it can trust that\nthe request is a Task Queue request.\n\nIn addition, requests from Task Queue can contain the following headers:\n\nSecuring task handler URLs\n--------------------------\n\nIf a task performs sensitive operations (such as modifying data), you might want to secure the handler URL to prevent a malicious external user from calling it directly. You can prevent users from accessing task URLs by restricting access to\n[App Engine administrators](/appengine/docs/legacy/standard/php/access-control). Task requests themselves are\nissued by App Engine and can always target a restricted URL.\n\nYou can restrict a URL by adding the\n[`login: admin`](/appengine/docs/legacy/standard/php/config/appref#handlers_login)\nelement to the handler configuration in your `app.yaml` file.\n\nFor example: \n\n runtime: php55\n api_version: 1\n threadsafe: true\n\n handlers:\n - url: /tasks/process\n script: process.php\n login: admin\n\n| **Note:** While tasks can use URL paths restricted with `login: admin`, they *cannot* use URL paths restricted with `login: required` because tasks are not run as any user. The `admin` restriction is satisfied by the inclusion of the `X-Appengine-QueueName` header described in [Reading request headers](#reading_request_headers).\n\nWhat's next\n-----------\n\n- Learn how to [delete tasks](/appengine/docs/legacy/standard/php/taskqueue/push/deleting-tasks-and-queues)"]]