Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
* Still driven by the foreground InstallService so polling continues
* across UI/config changes.
*
* Contract (static/dashboard/routes.ts + sockets/jobs.ts):
* POST /api/kiwix/download {ids:[...]} -> { id, ... }
* GET /api/kiwix/jobs/:id -> { phase, percent,
* speed(bytes/s),
* detail, error }
* POST /api/kiwix/jobs/:id/cancel -> { ok:true }
* POST /api/kiwix/jobs/:id/pause|resume -> { ok:true } (ADFA-4893)
* phase is one of: queued|downloading|indexing|processing|paused|done|error|canceled.
* Contract (static/dashboard/routes.ts + sockets/jobs.ts), per content <type>
* ("kiwix", "books", ...) — ADFA-4893 made this client type-parametric:
* POST /api/<type>/download <body> -> { id, ... }
* GET /api/<type>/jobs/:id -> { phase, percent, speed(bytes/s),
* detail, error, retryAttempt, retryTotal }
* POST /api/<type>/jobs/:id/{cancel,pause,resume} -> { ok:true }
* The poll is interpreted by percent + retry state, not the exact phase name, so kiwix
* (downloading/indexing) and books (processing) can share one client.
* ============================================================================
*/
package org.iiab.controller.content;
Expand Down Expand Up @@ -58,7 +58,7 @@ default void onPaused(int percent) {}
default void onReconnecting(int attempt, int total) {}
}

private static final String BASE = BoxEndpoints.API + "/kiwix";
private final String base; // ADFA-4893: per-type API base (/api/kiwix, /api/books, ...)
private static final long POLL_MS = 1000L;
private static final int MAX_POLL_ERRORS = 10; // tolerate ~10s of transient network blips
// ADFA-4893: the START (download) POST is before any server job exists, so the server's own
Expand All @@ -75,19 +75,32 @@ default void onReconnecting(int attempt, int total) {}

private final Runnable pollTask = () -> AppExecutors.get().io().execute(this::pollOnce);

/** Start (download + index) one ZIM on the running server and poll to completion. */
/** ZIM/kiwix by default (back-compat with existing callers). */
public RestContentClient() { this("kiwix"); }

/** ADFA-4893: shared client for any content type ("kiwix", "books", ...) -> /api/&lt;type&gt;. */
public RestContentClient(@NonNull String type) { this.base = BoxEndpoints.API + "/" + type; }

/** Start (download + index) one ZIM and poll to completion. Thin wrapper over {@link #start}. */
public void addZim(@NonNull String zimFilename, @NonNull Listener l) {
this.listener = l;
final JSONObject body;
try { body = new JSONObject().put("ids", new JSONArray().put(zimFilename)); }
catch (Exception e) { fail("couldn't build the request"); return; }
start(body, l);
}

/** ADFA-4893: generic start — POST the type-specific body to /download, then poll to completion.
* Kiwix passes {ids:[...]}, books {items:[{id,title,url}]}; this client is body-agnostic. */
public void start(@NonNull JSONObject body, @NonNull Listener l) {
this.listener = l;
AppExecutors.get().io().execute(() -> {
final JSONObject body;
try { body = new JSONObject().put("ids", new JSONArray().put(zimFilename)); }
catch (Exception e) { fail("couldn't build the request"); return; }
// ADFA-4893: retry ONLY the kickoff POST (connectivity blip / server still warming) a few
// times. An empty id is a server refusal, not a blip, so it fails without retrying. Once the
// job exists, the server owns reconnection (visible), not this loop.
for (int attempt = 1; !finished; attempt++) {
try {
JSONObject resp = httpJson("POST", BASE + "/download", body);
JSONObject resp = httpJson("POST", base + "/download", body);
String id = resp.optString("id", "");
if (id.isEmpty()) { fail("content service did not start the job"); return; }
jobId = id;
Expand All @@ -105,7 +118,7 @@ public void addZim(@NonNull String zimFilename, @NonNull Listener l) {
private void pollOnce() {
if (finished) return;
try {
JSONObject j = httpJson("GET", BASE + "/jobs/" + jobId, null);
JSONObject j = httpJson("GET", base + "/jobs/" + jobId, null);
pollErrors = 0;
String phase = j.optString("phase", "");
int percent = j.optInt("percent", -1);
Expand All @@ -117,37 +130,32 @@ private void pollOnce() {

if (detail != null && !detail.isEmpty()) deliver(() -> listener.onLog(detail));

// Terminal phases first.
switch (phase) {
case "downloading":
case "done": done(); return;
case "error": fail(error != null ? error : "download failed"); return;
case "canceled": fail("canceled"); return;
case "paused": {
// ADFA-4893: not terminal — surface it and keep polling so resume/progress is seen.
final int pp = percent;
deliver(() -> listener.onPaused(pp));
break;
}
default: {
// ADFA-4893: active phase — interpret by percent + retry, NOT by the phase name, so
// kiwix ("downloading"/"indexing") and books ("processing" with an item %) both work.
if (retryAttempt > 0) {
final int a = retryAttempt, t = retryTotal;
deliver(() -> listener.onReconnecting(a, t));
} else if (percent >= 0) {
final int p = percent; final String rate = formatRate(speed);
deliver(() -> listener.onProgress(p, rate));
} else if ("indexing".equals(phase) || "processing".equals(phase)) {
if (!indexing) { indexing = true; deliver(() -> listener.onIndexing()); }
}
break;
case "indexing":
case "processing":
if (!indexing) { indexing = true; deliver(() -> listener.onIndexing()); }
break;
case "done":
done();
return;
case "error":
fail(error != null ? error : "download failed");
return;
case "canceled":
fail("canceled");
return;
case "paused": {
// ADFA-4893: not terminal — surface it and keep polling so resume/progress is seen.
final int pp = percent;
deliver(() -> listener.onPaused(pp));
// else queued / no-percent-yet: keep polling.
break;
}
default:
break; // queued / unknown: keep polling
}
main.postDelayed(pollTask, POLL_MS);
} catch (Exception e) {
Expand All @@ -162,7 +170,7 @@ public void cancel() {
final String id = jobId;
if (id != null && !id.isEmpty()) {
AppExecutors.get().io().execute(() -> {
try { httpJson("POST", BASE + "/jobs/" + id + "/cancel", null); } catch (Exception ignore) { /* best effort */ }
try { httpJson("POST", base + "/jobs/" + id + "/cancel", null); } catch (Exception ignore) { /* best effort */ }
});
}
teardown();
Expand All @@ -173,7 +181,7 @@ public void pause() {
final String id = jobId;
if (id != null && !id.isEmpty()) {
AppExecutors.get().io().execute(() -> {
try { httpJson("POST", BASE + "/jobs/" + id + "/pause", null); } catch (Exception ignore) { /* best effort */ }
try { httpJson("POST", base + "/jobs/" + id + "/pause", null); } catch (Exception ignore) { /* best effort */ }
});
}
}
Expand All @@ -183,7 +191,7 @@ public void resume() {
final String id = jobId;
if (id != null && !id.isEmpty()) {
AppExecutors.get().io().execute(() -> {
try { httpJson("POST", BASE + "/jobs/" + id + "/resume", null); } catch (Exception ignore) { /* best effort */ }
try { httpJson("POST", base + "/jobs/" + id + "/resume", null); } catch (Exception ignore) { /* best effort */ }
});
}
}
Expand Down
Loading
Loading