I saw limitations when sending multiple requests. In the documentation, there is no example of how to set up a request with several tasks:
$client = new CloudTasksClient();
$queueName = $client->queueName($projectId, $locationId, $queueId);
$httpRequest = new HttpRequest();
$httpRequest->setUrl($url);
$httpRequest->setHttpMethod(HttpMethod::POST);
if (isset($payload))
$httpRequest->setBody($payload);
$task = new Task();
$task->setHttpRequest($httpRequest);
$response = $client->createTask($queueName, $task);
printf('Created task %s' . PHP_EOL, $response->getName());
That way, I can only make synchronous requests within a loop.
How to send an array of requests at once? I need it to be something similar:
$client = new CloudTasksClient();
$queueName = $client->queueName($projectId, $locationId, $queueId);
for ($i = 0; $i < 100; $i++) {
$httpRequest = new HttpRequest();
$httpRequest->setUrl($url."/id/".$i);
$httpRequest->setHttpMethod(HttpMethod::POST);
if (isset($payload))
$httpRequest->setBody($payload);
$requests[$i] = $httpRequest;
}
$task = new Task();
$task->setHttpRequest($requests);
$response = $client->createTask($queueName, $task);
printf('Created task %s' . PHP_EOL, $response->getName());
However, the error is returned:
Fatal error: Uncaught Exception: Expect Google\Cloud\Tasks\V2\HttpRequest. in /***/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:197 Stack trace: #0 /***/vendor/google/cloud-tasks/src/V2/Task.php(315): Google\Protobuf\Internal\GPBUtil::checkMessage(Array, 'Google\Cloud\Ta...') #1 /***/test.php(27): Google\Cloud\Tasks\V2\Task->setHttpRequest(Array) #2 {main} thrown in /***/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 197
What is the correct and best way to send multiple tasks in the same request?