[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eTask handlers are request handlers that process push tasks, with the mapping from request URLs to handlers defined in the service's \u003ccode\u003eapp.yaml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eTask handlers must return an HTTP status code between 200-299 to indicate success, with any other code signifying failure and triggering a retry, and the code must be idempotent.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine sets specific HTTP headers in Task Queue requests, such as \u003ccode\u003eX-Appengine-QueueName\u003c/code\u003e, \u003ccode\u003eX-Appengine-TaskName\u003c/code\u003e, and \u003ccode\u003eX-Appengine-TaskRetryCount\u003c/code\u003e, to provide task-specific information.\u003c/p\u003e\n"],["\u003cp\u003eTo secure sensitive task handler URLs, you can restrict access to App Engine administrators by using the \u003ccode\u003elogin: admin\u003c/code\u003e element in the \u003ccode\u003eapp.yaml\u003c/code\u003e file's handler configuration.\u003c/p\u003e\n"],["\u003cp\u003eTask Queue requests are sent from the IP address 0.1.0.2, and the code for the task handler does not have to be written in the same language as the code that enqueued the task.\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| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-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/go111/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/go111/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/go111/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/go111/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/go111/config/appref#handlers-login)\nelement to the handler configuration in your `app.yaml` file.\n\nFor example: \n\n runtime: go\n api_version: go1\n\n handlers:\n - url: /worker/.*\n script: _go_app\n login: admin\n - url: /.*\n script: _go_app\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/go111/taskqueue/push/deleting-tasks-and-queues)"]]