|
22 | 22 |
|
23 | 23 | import com.chargebee.v4.models.timeMachine.responses.TimeMachineStartAfreshResponse; |
24 | 24 |
|
| 25 | +import com.chargebee.v4.models.timeMachine.TimeMachine; |
| 26 | +import com.chargebee.v4.models.timeMachine.TimeMachine.TimeTravelStatus; |
| 27 | +import com.chargebee.v4.exceptions.OperationFailedException; |
| 28 | +import com.chargebee.v4.exceptions.codes.BadRequestApiErrorCode; |
| 29 | +import com.chargebee.v4.internal.JsonUtil; |
| 30 | + |
25 | 31 | public final class TimeMachineService extends BaseService<TimeMachineService> { |
26 | 32 |
|
27 | 33 | private final ServiceConfig config; |
@@ -167,4 +173,70 @@ public TimeMachineStartAfreshResponse startAfresh(String timeMachineName) |
167 | 173 | Response response = startAfreshRaw(timeMachineName); |
168 | 174 | return TimeMachineStartAfreshResponse.fromJson(response.getBodyAsString(), response); |
169 | 175 | } |
| 176 | + |
| 177 | + // === Time Travel Completion Helper === |
| 178 | + |
| 179 | + /** |
| 180 | + * Waits for time travel operation to complete. Polls the API until the status changes from |
| 181 | + * IN_PROGRESS. |
| 182 | + * |
| 183 | + * @param timeMachine The TimeMachine instance to wait for |
| 184 | + * @return The updated TimeMachine with final status |
| 185 | + * @throws ChargebeeException if the API call fails |
| 186 | + * @throws InterruptedException if the thread is interrupted while waiting |
| 187 | + * @throws RuntimeException if time travel takes too long or ends in invalid state |
| 188 | + * @throws OperationFailedException if time travel fails |
| 189 | + */ |
| 190 | + public TimeMachine waitForTimeTravelCompletion(TimeMachine timeMachine) |
| 191 | + throws ChargebeeException, InterruptedException { |
| 192 | + return waitForTimeTravelCompletion(timeMachine.getName()); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Waits for time travel operation to complete. Polls the API until the status changes from |
| 197 | + * IN_PROGRESS. |
| 198 | + * |
| 199 | + * @param timeMachineName The name of the TimeMachine to wait for |
| 200 | + * @return The updated TimeMachine with final status |
| 201 | + * @throws ChargebeeException if the API call fails |
| 202 | + * @throws InterruptedException if the thread is interrupted while waiting |
| 203 | + * @throws RuntimeException if time travel takes too long or ends in invalid state |
| 204 | + * @throws OperationFailedException if time travel fails |
| 205 | + */ |
| 206 | + public TimeMachine waitForTimeTravelCompletion(String timeMachineName) |
| 207 | + throws ChargebeeException, InterruptedException { |
| 208 | + int count = 0; |
| 209 | + int sleepTime = Integer.getInteger("cb.java.time_travel.sleep.millis", 3000); |
| 210 | + TimeMachine timeMachine = retrieve(timeMachineName).getTimeMachine(); |
| 211 | + |
| 212 | + while (timeMachine.getTimeTravelStatus() == TimeTravelStatus.IN_PROGRESS) { |
| 213 | + if (count++ > 30) { |
| 214 | + throw new RuntimeException("The time travel is taking too much time"); |
| 215 | + } |
| 216 | + Thread.sleep(sleepTime); |
| 217 | + timeMachine = retrieve(timeMachineName).getTimeMachine(); |
| 218 | + } |
| 219 | + |
| 220 | + if (timeMachine.getTimeTravelStatus() == TimeTravelStatus.FAILED) { |
| 221 | + String errorJson = timeMachine.getErrorJson(); |
| 222 | + int httpStatusCode = JsonUtil.getInteger(errorJson, "http_code"); |
| 223 | + String exceptionMessage = JsonUtil.getString(errorJson, "message"); |
| 224 | + throw new OperationFailedException( |
| 225 | + httpStatusCode, |
| 226 | + "operation_failed", |
| 227 | + BadRequestApiErrorCode._UNKNOWN, |
| 228 | + exceptionMessage, |
| 229 | + errorJson, |
| 230 | + null, |
| 231 | + null); |
| 232 | + } |
| 233 | + |
| 234 | + if (timeMachine.getTimeTravelStatus() == TimeTravelStatus.NOT_ENABLED |
| 235 | + || timeMachine.getTimeTravelStatus() == TimeTravelStatus._UNKNOWN) { |
| 236 | + throw new RuntimeException( |
| 237 | + "Time travel status is in wrong state: " + timeMachine.getTimeTravelStatus()); |
| 238 | + } |
| 239 | + |
| 240 | + return timeMachine; |
| 241 | + } |
170 | 242 | } |
0 commit comments