PHP 5 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、PHP 5 アプリケーションをデプロイできなくなります。既存の PHP 5 アプリケーションは、
非推奨になる日付以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの PHP に移行することをおすすめします。
タスクの作成
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このページでは、タスクを作成して push キューに配置する方法を説明します。タスクを処理する場合は、新しいタスク オブジェクトを作成してキューに配置する必要があります。タスクを処理するサービスとハンドラを明示的に指定し、必要に応じてタスク固有のデータをハンドラに渡すこともできます。また、タスクを実行するタイミングのスケジューリング、タスクが失敗した場合の再試行回数の制限など、タスクの構成を微調整することもできます。
新しいタスクを作成する
タスクを作成してエンキューするには、PushTaskオブジェクトを作成して、add()
メソッドを呼び出します。add()
にキュー名の引数を指定すると、queue.yaml
で指定されたキューに追加できます。または、引数なしで add()
を呼び出すと、タスクがデフォルト キューに追加されます。
PushQueue を使用して複数のタスクを一括で追加することもできます。次の例では、addTasks()
メソッドを使用して、2 つの PushTask オブジェクトを PushQueue に追加します。
次のコードサンプルでは、単一のタスクを追加する方法を示しています。
次のコードサンプルでは、複数のタスクを一度に追加する方法を示しています。
PushTask
と PushQueue
を使用する場合は、これらのステートメントを PHP ファイルの先頭に追加します。
ワーカー サービスを指定する
タスクがキューから取り出される(ポップ)と、タスクはタスクキュー サービスによってワーカー サービスに送信されます。いずれのタスクにもターゲットと url があり、どのサービスとハンドラがそのタスクを実行するかがこれらによって決まります。
target
ターゲットは、どのサービスがタスク実行の HTTP リクエストを受け取るかを指定します。これは文字列であり、サービス / バージョン / インスタンスを正規形式のいずれかで指定します。よく使用される形式は次のとおりです。
service
version.service
instance.version.service
ターゲット文字列の前には、アプリのドメイン名が付加されます。タスクのターゲットを設定するには、次の 3 つの方法があります。
タスクを作成するときにターゲットを宣言します。タスクの作成時にターゲットを明示的に設定するには、PushTask オブジェクトを作成するときに、$options
配列内で header パラメータを使用します。
$task = new PushTask(
'/worker',
[],
['header' => "Host: versionHostname"]);
queue.yaml
でキューを定義するときに、target
ディレクティブを指定します(queue-blue
の定義をご覧ください)。target
が指定されているキューに追加されるタスクはすべて、そのターゲットを使用します。タスクの作成時に別のターゲットが割り当てられていても無視されます。
上記の 2 つの方法のいずれかでターゲットが指定されていない場合、タスクをキューに追加したサービスのバージョンが、そのタスクのターゲットになります。この方法でタスクをデフォルトのサービスとバージョンからキューに追加した場合、タスクの実行前にデフォルトのバージョンが変更されると、タスクは変更後のデフォルトのバージョンで実行されます。
url
url
に基づいて、ターゲット サービスのハンドラのうち 1 つが選択され、そのハンドラによってタスクが実行されます。
url
は、ターゲット サービス内のハンドラ URL パターンのうち 1 つに一致する必要があります。タスクに指定されたメソッドが GET
または PULL
である場合は、url
にクエリ パラメータを含めることができます。url
が指定されていない場合は、デフォルトの URL /_ah/queue/[QUEUE_NAME]
が使用されます。ここで [QUEUE_NAME]
はタスクのキューの名前です。
データをハンドラに渡す
データをハンドラに渡す場合、タスクの URL 内のクエリ パラメータとして渡す方法がありますが、この方法が可能なのはタスクで指定されているメソッドが GET
か PULL
の場合のみです。
PushTask
コンストラクタには query_data の位置引数が含まれています。データは通常、Key-Value ペアの辞書です。タスクのメソッドが POST
または PUT
の場合、データは HTTP リクエストのペイロードに追加されます。メソッドが GET の場合は、クエリ パラメータとして URL に追加されます。
タスクに名前を付ける
新しいタスクを作成すると、デフォルトではタスクに一意の名前が割り当てられます。ただし、name
パラメータを使用すると、タスクに独自の名前を割り当てることができます。独自のタスク名を割り当てることの利点は、名前付きのタスクでは重複が除外されることです。つまり、タスク名を使用すると、タスクが 1 回のみ追加されることを保証できます。重複の除外は、タスクが完了するか、削除されてから 9 日間続きます。
重複除外ロジックはパフォーマンスのオーバーヘッドを大幅に増加させるため、レイテンシが増加し、場合によっては名前付きタスクに伴うエラー率が高まるので注意してください。このようなコストは、タイムスタンプのように連続したタスク名が付けられている場合、大幅に増大する可能性があります。そのため、独自の名前を割り当てる場合は、コンテンツのハッシュなどの適度に分散された接頭辞をタスク名に使用することをおすすめします。
独自のタスク名を割り当てる場合は 500 文字以内にする必要があります。また、タスク名には英字の大文字と小文字、数字、アンダースコア、ハイフンを使用できます。
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","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 UTC。"],[[["\u003cp\u003eThis document details how to create and enqueue tasks within push queues, using \u003ccode\u003ePushTask\u003c/code\u003e and \u003ccode\u003ePushQueue\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eTasks can be assigned to specific worker services and handlers via a \u003ccode\u003etarget\u003c/code\u003e (service) and \u003ccode\u003eurl\u003c/code\u003e (handler), with options to set the target when constructing the task or within the queue's configuration.\u003c/p\u003e\n"],["\u003cp\u003eData can be passed to handlers as query parameters for \u003ccode\u003eGET\u003c/code\u003e or \u003ccode\u003ePULL\u003c/code\u003e methods, or within the HTTP request payload for \u003ccode\u003ePOST\u003c/code\u003e or \u003ccode\u003ePUT\u003c/code\u003e methods, using the \u003ccode\u003ePushTask\u003c/code\u003e constructor.\u003c/p\u003e\n"],["\u003cp\u003eWhile App Engine generates unique task names, you can assign custom names for de-duplication, ensuring a task is only added once within a 9-day window, although this carries a performance overhead.\u003c/p\u003e\n"],["\u003cp\u003eThis API supports first-generation runtimes and can be used for updating to second-generation runtimes, with a migration guide available for the PHP 7/8 runtime.\u003c/p\u003e\n"]]],[],null,["# Creating Tasks\n\nThis page describes how to create tasks and place them in push queues. When you\nwant to process a task, you must create a new task object and place it on a\nqueue. You can explicitly specify the service and handler that process the task,\nand optionally pass task-specific data along to the handler. You can also\nfine-tune the configuration for the task, like scheduling a time in the future\nwhen it should be executed or limiting the number of times you want the task to\nbe retried if it fails.\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\nCreating a new task\n-------------------\n\nTo create and enqueue a task, create a [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask)\nobject and call its `add()` method. You can add to a queue specified in\n`queue.yaml` by supplying a queue name argument to `add()`. Alternatively,\ncalling `add()` with no arguments will add the task to the default queue.\n\nYou can also add tasks in bulk to a queue using [PushQueue](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushQueue). In the\nfollowing example, two [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask) objects are added to a\n[PushQueue](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushQueue) using the `addTasks()` method.\n\nThe following code sample shows how to add a single task: \n\n $task = new PushTask(\n '/worker',\n ['name' =\u003e 'john doe', 'action' =\u003e 'send_reminder']);\n $task_name = $task-\u003eadd();\n\nThe following code sample shows how to add multiple tasks at once: \n\n $task1 = new PushTask('/someUrl');\n $task2 = new PushTask('/someOtherUrl');\n $queue = new PushQueue();\n $queue-\u003eaddTasks([$task1, $task2]);\n\nWhen you use `PushTask` and `PushQueue`, include these statements at the top of\nyour PHP file: \n\n use google\\appengine\\api\\taskqueue\\PushTask;\n use google\\appengine\\api\\taskqueue\\PushQueue;\n\nSpecifying the worker service\n-----------------------------\n\nWhen a task is popped off its queue, the Task Queue service sends it on\nto a worker service. Every task has a *target* and a *url*, which determine\nwhat service and handler will ultimately perform the task.\n\n### `target`\n\nThe target specifies the service that will receive the HTTP request to\nperform the task. It is a string that specifies a service/version/instance in\nany one of the [canonical forms](/appengine/docs/legacy/standard/php/how-requests-are-routed#routing_via_url). The most often-used forms are: \n\n service\n version.service\n instance.version.service\n\nThe target string is prepended to the domain name of your app. There are three\nways to set the target for a task:\n\n- Declare the target when you construct the task.\n\n\n You can set the target explicitly when creating the task by using the header\n parameter in the `$options`\n array when you construct the [PushTask](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask#__construct)\n object:\n\n $task = new PushTask(\n '/worker',\n [],\n ['header' =\u003e \"Host: versionHostname\"]);\n\n \u003cbr /\u003e\n\n- Include a `target` directive when you define a queue in the\n `queue.yaml`, as in the [definition](/appengine/docs/legacy/standard/php/taskqueue/push/creating-push-queues) of `queue-blue`.\n All tasks added to a queue with a `target` will use that target, even if\n a different target was assigned to the task at construction time.\n\n- If no target is specified according to either of the previous two methods,\n then the task's target is the version of the service that enqueues it.\n Note that if you enqueue a task from the default service and version in\n this manner, and the default version changes before the task executes, it\n will run in the new default version.\n\n### `url`\n\nThe `url` selects one of the handlers in the target service, which will\nperform the task.\n\nThe `url` should match one of the handler URL patterns in the target\nservice. The `url` can include query parameters if the method specified in the task is\n`GET` or `PULL`. If no `url` is specified the default URL\n`/_ah/queue/[QUEUE_NAME]` is used, where `[QUEUE_NAME]` is the name of\nthe task's queue.\n\nPassing data to the handler\n---------------------------\n\nYou can pass data to the handler as query parameters in the task's URL, but\nonly if the method specified in the task is `GET` or `PULL`.\n\nThe [`PushTask`](/appengine/docs/legacy/standard/php/refdocs/classes/google.appengine.api.taskqueue.PushTask) constructor has a positional argument for query_data.\nThe data is usually a dictionary of key-value pairs. If the task's method is\n`POST` or `PUT`, the data is added to the payload of the HTTP request. If the\nmethod is GET it is added to the URL as query parameters.\n\nNaming a task\n-------------\n\nWhen you create a new task, App Engine assigns the task a unique name by\ndefault. However, you can assign your own name to a task by using the `name`\nparameter. An advantage of assigning your own task names is that named tasks are\nde-duplicated, which means you can use task names to\nguarantee\nthat a task is only added once. De-duplication continues for 9 days after the\ntask is completed or deleted.\n\nNote that de-duplication logic introduces significant performance overhead,\nresulting in increased latencies and potentially increased error rates\nassociated with named tasks. These costs can be magnified significantly if task\nnames are sequential, such as with timestamps. So, if you assign your own names,\nwe recommend using a well-distributed prefix for task names, such as a hash of\nthe contents.\n\nIf you assign your own names to tasks, note that the maximum name length is 500\ncharacters, and the name can contain uppercase and lowercase letters, numbers\nunderscores, and hyphens.\n\nWhat's next\n-----------\n\n- Learn how to [create task\n handlers](/appengine/docs/legacy/standard/php/taskqueue/push/creating-handlers)."]]