Skip to content

Commit 1ba6c86

Browse files
authored
feat(java): waitForTask APIC-478 (#521)
1 parent c993b36 commit 1ba6c86

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-3
lines changed

.github/workflows/check.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ jobs:
199199
with:
200200
type: specs
201201

202+
# Always download the utils for js, in case they changed we want to include them in the zip
202203
- name: Download JavaScript utils artifacts
203-
if: ${{ matrix.client.language == 'javascript' && steps.cache.outputs.cache-hit != 'true' }}
204+
if: ${{ matrix.client.language == 'javascript' }}
204205
uses: ./.github/actions/restore-artifacts
205206
with:
206207
type: js_utils
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.algolia.exceptions;
2+
3+
/**
4+
* Exception thrown when an error occurs during the waitForTask strategy. For example: maximum
5+
* number of retry exceeded
6+
*/
7+
public class AlgoliaRetriesExceededException extends AlgoliaRuntimeException {
8+
9+
public static final long serialVersionUID = 1L;
10+
11+
public AlgoliaRetriesExceededException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public AlgoliaRetriesExceededException(String message) {
16+
super(message);
17+
}
18+
19+
public AlgoliaRetriesExceededException(Throwable cause) {
20+
super(cause);
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.algolia.utils;
2+
3+
import com.algolia.exceptions.*;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.function.IntUnaryOperator;
7+
import java.util.function.Predicate;
8+
import java.util.function.Supplier;
9+
10+
public class TaskUtils {
11+
12+
public static final int DEFAULT_MAX_TRIAL = 50;
13+
public static final IntUnaryOperator DEFAULT_TIMEOUT = (int retries) -> {
14+
return Math.min(retries * 200, 5000);
15+
};
16+
17+
public static <TResponse> void retryUntil(
18+
Supplier<CompletableFuture<TResponse>> func,
19+
Predicate<TResponse> validate,
20+
int maxTrial,
21+
IntUnaryOperator timeout
22+
) throws AlgoliaRuntimeException {
23+
int retryCount = 0;
24+
while (retryCount < maxTrial) {
25+
try {
26+
TResponse resp = func.get().get();
27+
if (validate.test(resp)) {
28+
return;
29+
}
30+
} catch (InterruptedException | ExecutionException e) {
31+
// if the task is interrupted, just return
32+
return;
33+
}
34+
try {
35+
Thread.sleep(timeout.applyAsInt(retryCount));
36+
} catch (InterruptedException ignored) {
37+
// Restore interrupted state...
38+
Thread.currentThread().interrupt();
39+
}
40+
41+
retryCount++;
42+
}
43+
throw new AlgoliaRetriesExceededException(
44+
"The maximum number of trials exceeded. (" +
45+
(retryCount + 1) +
46+
"/" +
47+
maxTrial +
48+
")"
49+
);
50+
}
51+
}

clients/algoliasearch-client-javascript/packages/client-common/src/createRetryablePromise.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise';
22

3+
export const DEFAULT_MAX_TRIAL = 50;
4+
export const DEFAULT_TIMEOUT = (retryCount: number): number =>
5+
Math.min(retryCount * 200, 5000);
6+
37
/**
48
* Return a promise that retry a task until it meets the condition.
59
*
@@ -12,8 +16,8 @@ import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromi
1216
export function createRetryablePromise<TResponse>({
1317
func,
1418
validate,
15-
maxTrial = 10,
16-
timeout = (retryCount: number): number => Math.min(retryCount * 10, 1000),
19+
maxTrial = DEFAULT_MAX_TRIAL,
20+
timeout = DEFAULT_TIMEOUT,
1721
}: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse> {
1822
let retryCount = 0;
1923
const retry = (): Promise<TResponse> => {

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public Map<String, Object> postProcessOperationsWithModels(
5050
Utils.getClientNameKebabCase(results),
5151
additionalProperties
5252
);
53+
additionalProperties.put(
54+
"isSearchClient",
55+
Utils.getClientNameKebabCase(results).equals("search")
56+
);
5357
additionalProperties.put(
5458
"packageVersion",
5559
Utils.getClientConfigField("java", "packageVersion")

templates/java/libraries/okhttp-gson/api.mustache

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.algolia.exceptions.*;
1313
import com.algolia.utils.retry.CallType;
1414
import com.algolia.utils.retry.StatefulHost;
1515
import com.algolia.utils.RequestOptions;
16+
import java.util.function.IntUnaryOperator;
1617

1718
import java.util.EnumSet;
1819
import java.util.Random;
@@ -216,5 +217,27 @@ public class {{classname}} extends ApiClient {
216217
}
217218
{{/optionalParams.0}}
218219
{{/operation}}
220+
221+
{{#isSearchClient}}
222+
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxTrial, IntUnaryOperator timeout) {
223+
TaskUtils.retryUntil(() -> {
224+
return this.getTaskAsync(indexName, taskID, requestOptions);
225+
}, (GetTaskResponse task) -> {
226+
return task.getStatus() == TaskStatus.PUBLISHED;
227+
}, maxTrial, timeout);
228+
}
229+
230+
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
231+
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
232+
}
233+
234+
public void waitForTask(String indexName, Long taskID, int maxTrial, IntUnaryOperator timeout) {
235+
this.waitForTask(indexName, taskID, null, maxTrial, timeout);
236+
}
237+
238+
public void waitForTask(String indexName, Long taskID) {
239+
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
240+
}
241+
{{/isSearchClient}}
219242
}
220243
{{/operations}}

0 commit comments

Comments
 (0)