Wait for a task to finish
The
waitForTask
method is only available in thesearch
client context.
Some operations related to the Algolia index are not always instantaneous. Doing such operations with our API clients will provide a taskID
in the response body, so you can later know what is the status of your operation.
We provide a waitForTask
helper method for you to easily wait for a specific task status.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
import { algoliasearch } from 'algoliasearch';
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');
const { taskID } = await client.saveObject({
indexName: '<YOUR_INDEX_NAME>',
body: {
title: 'My Algolia Object',
},
});
// Poll the task status with defaults values
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });
// Poll the task status with your options
await client.waitForTask({
indexName: '<YOUR_INDEX_NAME>',
taskID,
// Number of maximum retries to do
maxRetries: 100,
// The time to wait between retries
timeout: (retryCount: number): number => Math.min(retryCount * 200, 5000),
});
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);
$response = $client->saveObject(
'<YOUR_INDEX_NAME>',
['title' => "My Algolia Object"],
$requestOptions
);
// Poll the task status with defaults values
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
// Poll the task status with your options
Algolia\AlgoliaSearch\Support\Helpers::retryUntil(
$client,
'getTask',
['<YOUR_INDEX_NAME>', $response['taskID'], $requestOptions],
function ($res) {
return 'published' === $res['status'];
},
100, // Number of maximum retries to do
5000, // Default timeout
// Calculation of the time to wait between retries
function ($timeout, $retryCount) {
return min($retryCount * 200, $timeout) * 1000;
}
);
import com.algolia.model.search.*;
Map<String, Object> body = new HashMap<>();
body.put("title", "My Algolia Object");
SaveObjectResponse response = client.saveObject(
"<YOUR_INDEX_NAME>",
body
);
// Poll the task status with defaults values
await client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
// Poll the task status with your options
await client.waitForTask(
"<YOUR_INDEX_NAME>",
response.getTaskID(),
// Number of maximum retries to do
100,
// The time to wait between retries
retryCount -> Math.min(retryCount * 200, 5000);
);
import com.algolia.client.api.SearchClient
import com.algolia.client.extensions.waitTask
import kotlinx.serialization.json.*
val client = SearchClient("appId", "apiKey")
val body = buildJsonObject {
put("title", "My Algolia Object")
}
val response = client.saveObject("<YOUR_INDEX_NAME>", body)
// Poll the task status with defaults values
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
// Poll the task status with your options
client.waitTask(
indexName = "<YOUR_INDEX_NAME>",
taskID = response.taskID,
maxRetries = 100,
timeout = 1.minutes,
)
var client = SearchClient(appId: '<YOUR_APP_ID>', apiKey: '<YOUR_API_KEY>');
var body = {'title', 'My Algolia Object'};
var response = await client.saveObject(indexName: '<YOUR_INDEX_NAME>', body: body);
// Poll the task status with defaults values
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
// Poll the task status with your options
await client.waitTask(
indexName: '<YOUR_INDEX_NAME>',
taskID: response.taskID,
params: WaitParams(
maxRetries: 100,
timeout: Duration(minutes: 1),
),
);
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
indexName := "<INDEX_NAME>"
appID := "<APPLICATION_ID>"
apiKey := "<API_KEY>"
searchClient, _ := search.NewClient(appID, apiKey)
response, err := searchClient.SaveObject(
searchClient.NewApiSaveObjectRequest(
indexName,
map[string]interface{}{
"name": "Tom Cruise",
},
),
)
if err != nil {
panic(err)
}
// Poll the task status with defaults values
taskResponse, err := searchClient.WaitForTask(
indexName,
response.TaskID,
nil,
nil,
nil,
)
if err != nil {
panic(err)
}
// Poll the task status with your options
initialDelay := 1000*time.Millisecond
maxDelay := 10_000*time.Millisecond
taskResponse, err := searchClient.WaitForTask(
indexName,
response.TaskID,
// Number of maximum retries to do.
search.PtrInt(100),
// The time to wait between retries, in milliseconds.
&initialDelay,
// The maximum delay to wait before making the request timeout, in milliseconds.
&maxDelay,
)
if err != nil {
panic(err)
}