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 @@ -16,7 +16,6 @@
import android.util.Log;

import org.iiab.controller.kolibri.data.KolibriWishlist;
import org.iiab.controller.redesign.MapsProvisioner;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -43,8 +42,7 @@ public static boolean hasPending(Context ctx) {
}

/**
* ADFA-4954: whether a drain would proceed <em>right now</em>, without writing
* anything.
* ADFA-4954: whether a drain would proceed <em>right now</em>, without writing anything.
*
* <p>Now only {@link #drain} asks. It used to be public API for the live door, which
* checked before writing so it could refuse with "Another download is running" rather
Expand All @@ -54,27 +52,12 @@ public static boolean hasPending(Context ctx) {
* and visible ("Queued" on its row). The door banks and lets this defer; a queue is a
* better answer than a refusal, and the honest one now that something drains it.
*
* <p>The rule stays here rather than being restated at the call site — that is
* how the four {@code *Wizard} booleans went wrong.
* <p>The rule itself moved to {@link org.iiab.controller.system.data.ContentAdmission}: Books
* and ZIM carried the same one, word for word. It stays out of the call site for the reason
* this javadoc always gave — that is how the four {@code *Wizard} booleans went wrong.
*/
static boolean canDrainNow(Context ctx) {
if (org.iiab.controller.install.presentation.ModuleQueueRepository.get().isRunning()
|| MapsProvisioner.hasPending(ctx)
|| org.iiab.controller.redesign.DashboardRebuildService.isRunning()) { // ADFA-5333
Log.d(TAG, "kolibri drain blocked: proot (runrole) or dashboard-update work is in flight");
return false;
}
// ADFA-5074: unfinished work only. This read a merely registered session, so a
// download that had already completed and was waiting to be dismissed refused the
// next one with "something else is downloading" while nothing was — and the only
// way out was force-stopping the app, because these are process statics.
// Serialising exists because each stream measures free space at its own moment;
// one that has finished has already been absorbed by the disk.
if (org.iiab.controller.system.data.PendingContent.anyUnfinished(ctx)) {
Log.d(TAG, "kolibri drain blocked: a content stream still has work to do");
return false;
}
return true;
return org.iiab.controller.system.data.ContentAdmission.canStart(ctx, org.iiab.controller.system.domain.ContentType.COURSES);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,9 @@ public static boolean hasPending(Context ctx) {
/** Hand the wishlist to BooksDownloadService (requires the server to be up) and clear it.
* No-op if empty or a session is already running. */
public static void drain(Context ctx) {
// ADFA-4900: proot (runrole) and REST downloads must not run at the same time — Ansible forks
// background processes and concurrent REST work is a recipe for corruption. Defer REST while a
// module-queue (proot) job is pending or running; a later drain pass picks it up once idle.
if (org.iiab.controller.install.presentation.ModuleQueueRepository.get().isRunning()
|| MapsProvisioner.hasPending(ctx)
|| DashboardRebuildService.isRunning()) { // ADFA-5333: a live dashboard update restarts dash-node
Log.d(TAG, "books drain deferred: proot (runrole) or dashboard-update work is in flight");
return;
}
// ADFA-4954 (ADR-4954 D8): the live REST streams also serialize against each other.
// Each measures free space independently and at a different moment, so all of them can
// pass their own check and jointly fill the disk. A Kolibri channel runs to tens of GB.
// ADFA-5074: asked in one place, and about unfinished work rather than a registered
// session — see ZimProvisioner for why.
if (org.iiab.controller.system.data.PendingContent.anyUnfinished(ctx)) {
Log.d(TAG, "books drain deferred: a content stream still has work to do");
return;
}
// The deferral rule (proot in flight, another stream unfinished) is one source, shared with
// ZIM and Kolibri — see ContentAdmission for why each check is there.
if (!org.iiab.controller.system.data.ContentAdmission.canStart(ctx, org.iiab.controller.system.domain.ContentType.BOOKS)) return;
JSONArray order = BooksWishlist.all(ctx);
Log.i(TAG, "books drain: " + order.length() + " in wishlist");
List<String> ids = new ArrayList<>(), titles = new ArrayList<>(), urls = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,9 @@ public static boolean hasPending(Context ctx) {
/** Resolve the wishlist against the offline catalog and hand it to ZimDownloadService.
* No-op if empty or a session is already running. Requires the server to be up. */
public static void drain(Context ctx) {
// ADFA-4900: proot (runrole) and REST downloads must not run concurrently (Ansible forks
// background processes; concurrent REST work risks corruption). Defer ZIM while a module-queue
// (proot) job is pending or running; a later drain pass runs it once proot is idle.
if (org.iiab.controller.install.presentation.ModuleQueueRepository.get().isRunning()
|| MapsProvisioner.hasPending(ctx)
|| DashboardRebuildService.isRunning()) { // ADFA-5333: a live dashboard update restarts dash-node
Log.d(TAG, "zim drain deferred: proot (runrole) or dashboard-update work is in flight");
return;
}
// ADFA-4954 (ADR-4954 D8): the live REST streams also serialize against each other.
// Each measures free space independently and at a different moment, so all of them can
// pass their own check and jointly fill the disk. A Kolibri channel runs to tens of GB.
// ADFA-5074: asked in one place, and about unfinished work rather than a registered
// session — a finished stream has already been absorbed by the disk, and blocking on
// it refused downloads with nothing running. Also means a fourth content type is one
// line in ContentType instead of an edit here.
if (org.iiab.controller.system.data.PendingContent.anyUnfinished(ctx)) {
Log.d(TAG, "zim drain deferred: a content stream still has work to do");
return;
}
// The deferral rule (proot in flight, another stream unfinished) is one source, shared with
// Books and Kolibri — see ContentAdmission for why each check is there.
if (!org.iiab.controller.system.data.ContentAdmission.canStart(ctx, org.iiab.controller.system.domain.ContentType.ZIM)) return;
if (ZimWishlist.size(ctx) == 0) return;
final Context app = ctx.getApplicationContext();
Log.i(TAG, "zim drain: " + ZimWishlist.size(app) + " in wishlist; loading catalog");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* ============================================================================
* Name : ContentAdmission.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : The one answer to "may a REST content stream start right now?". Books, ZIM and
* Kolibri each carried an identical copy of this rule; a fourth stream would have
* carried a fourth. Behaviour is unchanged — the checks, their order and the reasons
* are the ones the three copies already applied.
* ============================================================================
*/
package org.iiab.controller.system.data;

import android.content.Context;
import android.util.Log;

import org.iiab.controller.install.presentation.ModuleQueueRepository;
import org.iiab.controller.redesign.DashboardRebuildService;
import org.iiab.controller.redesign.MapsProvisioner;
import org.iiab.controller.system.domain.ContentType;

/**
* Admission for the live REST content streams (Books, ZIM, Kolibri channels).
*
* <p>The rule lives here rather than being restated at each call site — that is how the four
* {@code *Wizard} booleans went wrong. It answers for the streams that the box downloads over REST;
* the proot operations (a module runrole, maps) have their own, different gate, and deliberately do
* not share this one: they are what this rule defers <em>to</em>.
*
* <p>Two deferrals, for different reasons:
* <ul>
* <li><b>proot</b> (ADFA-4900) — Ansible forks background processes and concurrent REST work
* risks corruption, so no REST stream may run while a runrole is pending or in flight.
* ADFA-5333 adds the live dashboard update, which restarts dash-node underneath a stream.</li>
* <li><b>the other live REST streams</b> (ADR-4954 D8) — each measures free space independently
* and at a different moment, so all of them can pass their own check and jointly fill the
* disk. A Kolibri channel runs to tens of GB, which makes it the one most likely to be the
* straw.</li>
* </ul>
*
* <p>ADFA-5074: the second check asks about <em>unfinished</em> work, not about a registered
* session. Reading a merely registered session refused the next download while nothing was running
* — a stream that had already completed and was waiting to be dismissed still said no, and the only
* way out was force-stopping the app, because these are process statics. A finished stream has
* already been absorbed by the disk. It also means a fourth content type is one line in
* {@code ContentType} instead of an edit here.
*
* <p>Deferred is not a failure: callers leave the order banked and the next pass picks it up.
*/
public final class ContentAdmission {

private static final String TAG = "K2Go-Provision";

private ContentAdmission() {}

/**
* True when a REST content stream may start now.
*
* @param stream the asking stream. A {@link ContentType} rather than its name: the type already
* owns that identity ({@code key()} is what the logs, the progress rows and the
* operations all use), and it carries the execution class that says which types
* this gate is even about — {@code MAPS} is {@code STOPPED}, so it is not a caller
* here but one of the things this rule defers to.
*/
public static boolean canStart(Context ctx, ContentType stream) {
if (ModuleQueueRepository.get().isRunning()
|| MapsProvisioner.hasPending(ctx)
|| DashboardRebuildService.isRunning()) {
Log.d(TAG, stream.key() + " drain deferred: proot (runrole) or dashboard-update work is in flight");
return false;
}
if (PendingContent.anyUnfinished(ctx)) {
Log.d(TAG, stream.key() + " drain deferred: a content stream still has work to do");
return false;
}
return true;
}
}
Loading