Skip to content

Commit

Permalink
feat: add util for retrying an api command automatically after delay (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Mar 27, 2024
1 parent 5701e3d commit fb9cffc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bigboxer23</groupId>
<artifactId>utils</artifactId>
<version>2.0.26</version>
<version>2.0.27</version>

<name>utils</name>
<!-- FIXME change it to the project's website -->
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/bigboxer23/utils/command/RetryingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bigboxer23.utils.command;

import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Run a command and retry it once after a pause */
public class RetryingCommand {
private static final Logger logger = LoggerFactory.getLogger(RetryingCommand.class);

public static <T> T execute(Command<T> command, String identifier) throws IOException {
try {
logger.info("Starting command " + identifier);
return command.execute();
} catch (IOException e) {
logger.error("error running command, attempting to retry " + identifier, e);
try {
Thread.sleep(5 * 1000); // 5 sec
return command.execute();
} catch (IOException e2) {
logger.error("error retrying command " + identifier, e2);
throw e2;
} catch (InterruptedException e2) {
logger.error("error retrying command " + identifier, e2);
}
}
return null;
}
}

0 comments on commit fb9cffc

Please sign in to comment.