public static void waitForTaskToFinish()

in tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/MigrationUtils.java [376:423]


    public static void waitForTaskToFinish(CloseableHttpClient httpClient, String esAddress, String taskId, MigrationContext migrationContext) throws IOException {
        while (true) {
            final JSONObject status = new JSONObject(
                    HttpUtils.executeGetRequest(httpClient, esAddress + "/_tasks/" + taskId,
                            null));
            if (status.has("error")) {
                final JSONObject error = status.getJSONObject("error");
                throw new IOException("Task error: " + error.getString("type") + " - " + error.getString("reason"));
            }
            if (status.has("completed") && status.getBoolean("completed")) {
                if (migrationContext != null) {
                    migrationContext.printMessage("Task is completed");
                } else {
                    LOGGER.info("Task is completed");
                }
                if (status.has("response")) {
                    final JSONObject response = status.getJSONObject("response");
                    printResponseDetail(response, migrationContext);
                    if (response.has("failures")) {
                        final JSONArray failures = response.getJSONArray("failures");
                        if (!failures.isEmpty()) {
                            for (int i = 0; i < failures.length(); i++) {
                                JSONObject failure = failures.getJSONObject(i);
                                JSONObject cause = failure.getJSONObject("cause");
                                if (migrationContext != null) {
                                    migrationContext.printMessage("Cause of failure: " + cause.toString());
                                } else {
                                    LOGGER.error("Cause of failure: {}", cause.toString());
                                }
                            }
                            throw new IOException("Task completed with failures, check previous log for details");
                        }
                    }
                }
                break;
            }
            if (migrationContext != null) {
                migrationContext.printMessage("Waiting for Task " + taskId + " to complete");
            } else {
                LOGGER.info("Waiting for Task {} to complete", taskId);
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }