0

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?

Alisson
  • 33
  • 9

1 Answers1

0

There seems to be no way of creating a batch of tasks at once atm. I'd recommend doing something like this:

$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);

    $task = new Task();
    $task->setHttpRequest($httpRequest);

    $response = $client->createTask($queueName, $task);
    printf('Created task %s' . PHP_EOL, $response->getName());
}

You can also refer to the docs here.

petomalina
  • 2,020
  • 2
  • 19
  • 25
  • This form is synchronous, it is not useful for me. I need multiple tasks to be sent in a single request. If I need to send 100,000 tasks, it will take a long time. – Alisson Jun 08 '20 at 11:25
  • In that case, I'd recommend submitting the form synchronously and then create a background task to create all tasks that are needed. You can do this by creating a single Cloud Task that will then spin up the background job, or using PubSub (the same way). – petomalina Jun 08 '20 at 11:27