-
Notifications
You must be signed in to change notification settings - Fork 21
feat(java): waitForTask APIC-478 #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...oliasearch-core/src/main/java/com/algolia/exceptions/AlgoliaRetriesExceededException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.algolia.exceptions; | ||
|
||
/** | ||
* Exception thrown when an error occurs during the waitForTask strategy. For example: maximum | ||
* number of retry exceeded | ||
*/ | ||
public class AlgoliaRetriesExceededException extends AlgoliaRuntimeException { | ||
|
||
public static final long serialVersionUID = 1L; | ||
|
||
public AlgoliaRetriesExceededException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public AlgoliaRetriesExceededException(String message) { | ||
super(message); | ||
} | ||
|
||
public AlgoliaRetriesExceededException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...liasearch-client-java-2/algoliasearch-core/src/main/java/com/algolia/utils/TaskUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.algolia.utils; | ||
|
||
import com.algolia.exceptions.*; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.IntUnaryOperator; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
public class TaskUtils { | ||
|
||
public static <TResponse> void retryUntil( | ||
Supplier<CompletableFuture<TResponse>> func, | ||
Predicate<TResponse> validate, | ||
int maxTrial, | ||
IntUnaryOperator timeout | ||
) throws AlgoliaRuntimeException { | ||
int retryCount = 0; | ||
while (retryCount < maxTrial) { | ||
try { | ||
TResponse resp = func.get().get(); | ||
System.out.println(resp); | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (validate.test(resp)) { | ||
return; | ||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
// if the task is interrupted, just return | ||
return; | ||
} | ||
try { | ||
System.out.println("sleeping for " + timeout.applyAsInt(retryCount)); | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Thread.sleep(timeout.applyAsInt(retryCount)); | ||
} catch (InterruptedException ignored) { | ||
// Restore interrupted state... | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
retryCount++; | ||
} | ||
throw new AlgoliaRetriesExceededException( | ||
"The maximum number of trials exceeded. (" + | ||
(retryCount + 1) + | ||
"/" + | ||
maxTrial + | ||
")" | ||
); | ||
} | ||
|
||
public static <TResponse> void retryUntil( | ||
Supplier<CompletableFuture<TResponse>> func, | ||
Predicate<TResponse> validate | ||
) { | ||
retryUntil( | ||
func, | ||
validate, | ||
50, | ||
(int retries) -> { | ||
return Math.min(retries * 200, 5000); | ||
} | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,5 +216,19 @@ public class {{classname}} extends ApiClient { | |
} | ||
{{/optionalParams.0}} | ||
{{/operation}} | ||
|
||
{{#isSearchClient}} | ||
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we allow option overriding like we did for JS? This way people don't have to re-implement the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep I can add a variant |
||
TaskUtils.retryUntil(() -> { | ||
return this.getTaskAsync(indexName, taskID, requestOptions); | ||
}, (GetTaskResponse task) -> { | ||
return task.getStatus() == TaskStatus.PUBLISHED; | ||
}); | ||
} | ||
|
||
public void waitForTask(String indexName, Long taskID) { | ||
this.waitForTask(indexName, taskID, null); | ||
} | ||
{{/isSearchClient}} | ||
} | ||
{{/operations}} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe add the defaults at the class level? So we don't have to dig if we want to change it somedays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did the same as for js, do you want me to change it there too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep a constant at the top of the file maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry you mean putting the default in the search client or just at the top of this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the top of this file, the default value options (same for JS maybe?)