Retries an action every ms, for 250ms, until it finally works or fails. Makes FS operations more reliable.
(File input, Throwing.Function<File, T> function)
| 116 | * Makes FS operations more reliable. |
| 117 | */ |
| 118 | private static <T> T retry(File input, Throwing.Function<File, T> function) { |
| 119 | long start = System.currentTimeMillis(); |
| 120 | Throwable lastException; |
| 121 | do { |
| 122 | try { |
| 123 | return function.apply(input); |
| 124 | } catch (Throwable e) { |
| 125 | lastException = e; |
| 126 | Errors.suppress().run(() -> Thread.sleep(1)); |
| 127 | } |
| 128 | } while (System.currentTimeMillis() - start < MS_RETRY); |
| 129 | throw Errors.asRuntime(lastException); |
| 130 | } |
| 131 | |
| 132 | /** Returns true if the given directory exists, and waits up to 500ms for the directory to exist. */ |
| 133 | public static boolean dirExists(File dir) { |
no test coverage detected