feat(kanban): stop mixed queue on rejection
req: sync/009 req: sync/010
This commit is contained in:
@@ -19,10 +19,12 @@ let maxObservedInFlight = 0;
|
||||
let stopped = false;
|
||||
|
||||
class UploadError extends Error {
|
||||
constructor(status, retryable) {
|
||||
constructor(status, retryable, reason) {
|
||||
super(`sync upload failed with ${status}`);
|
||||
this.name = "UploadError";
|
||||
this.status = status;
|
||||
this.retryable = retryable;
|
||||
this.reason = reason;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,10 +134,17 @@ function setOnline(online) {
|
||||
root.setAttribute("data-sync-connection", online ? "online" : "offline");
|
||||
}
|
||||
|
||||
function setManualRetryAvailable(available) {
|
||||
const retry = root.querySelector("[data-sync-retry]");
|
||||
retry.disabled = !available;
|
||||
if (available) root.setAttribute("data-sync-manual-retry", "available");
|
||||
else root.removeAttribute("data-sync-manual-retry");
|
||||
}
|
||||
|
||||
function scheduleManualRetry(command, error) {
|
||||
clearTimeout(retryTimer);
|
||||
root.setAttribute("data-sync-error", error instanceof Error ? error.message : String(error));
|
||||
root.setAttribute("data-sync-manual-retry", "available");
|
||||
setManualRetryAvailable(true);
|
||||
setPhase("offline", "Sync is offline after bounded retries; the durable command remains queued. Retry now when ready.");
|
||||
root.dispatchEvent(new CustomEvent("kanban:sync-exhausted", { detail: { commandId: command.id, attempts: MAX_ATTEMPTS } }));
|
||||
}
|
||||
@@ -157,7 +166,11 @@ async function upload(command) {
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
continue;
|
||||
}
|
||||
if (!response.ok) throw new UploadError(response.status, response.status >= 500);
|
||||
if (!response.ok) {
|
||||
const problem = await response.json().catch(() => ({}));
|
||||
const reason = typeof problem.error === "string" ? problem.error : "unclassified rejection";
|
||||
throw new UploadError(response.status, response.status >= 500, reason);
|
||||
}
|
||||
return response.json();
|
||||
} catch (error) {
|
||||
if (error instanceof UploadError && !error.retryable) throw error;
|
||||
@@ -190,7 +203,7 @@ async function continuePendingWork() {
|
||||
root.setAttribute("data-sync-pending-count", String(commands.length));
|
||||
if (commands.length === 0) return;
|
||||
if (uploadsThisRun >= uploadLimit) {
|
||||
root.setAttribute("data-sync-manual-retry", "available");
|
||||
setManualRetryAvailable(true);
|
||||
setPhase("backpressured", `Upload limit ${uploadLimit} reached; ${commands.length} durable command${commands.length === 1 ? " remains" : "s remain"} queued. Retry now to continue.`);
|
||||
return;
|
||||
}
|
||||
@@ -285,8 +298,23 @@ async function synchronize(command) {
|
||||
});
|
||||
} catch (error) {
|
||||
synchronizing = false;
|
||||
if (error instanceof UploadError && !error.retryable) {
|
||||
setOnline(true);
|
||||
clearTimeout(leaseTimer);
|
||||
root.setAttribute("data-sync-error", error.message);
|
||||
root.setAttribute("data-sync-error-kind", "permanent-rejection");
|
||||
root.setAttribute("data-sync-error-status", String(error.status));
|
||||
root.setAttribute("data-sync-error-reason", error.reason);
|
||||
root.setAttribute("data-sync-rejected-command-id", command.id);
|
||||
const remaining = await pendingCommands(database);
|
||||
root.setAttribute("data-sync-pending-count", String(remaining.length));
|
||||
setManualRetryAvailable(false);
|
||||
setPhase("rejected", `Command ${command.id} was permanently rejected (${error.status}: ${error.reason}); ${remaining.length} durable command${remaining.length === 1 ? " remains" : "s remain"} queued for review.`);
|
||||
await releaseUploaderLease(database);
|
||||
root.setAttribute("data-sync-leader", "false");
|
||||
return;
|
||||
}
|
||||
setOnline(false);
|
||||
if (error instanceof UploadError && !error.retryable) throw error;
|
||||
scheduleManualRetry(command, error);
|
||||
}
|
||||
}
|
||||
@@ -320,6 +348,7 @@ async function start() {
|
||||
root.setAttribute("data-sync-in-flight", "0");
|
||||
root.setAttribute("data-sync-max-observed-in-flight", "0");
|
||||
root.setAttribute("data-sync-pending-count", String(commands.length));
|
||||
setManualRetryAvailable(false);
|
||||
if (commands.length === 0) {
|
||||
setPhase("idle", "No pending commands.");
|
||||
return;
|
||||
@@ -329,12 +358,13 @@ async function start() {
|
||||
if (!event.target.closest("[data-sync-retry]")) return;
|
||||
uploadsThisRun = 0;
|
||||
root.setAttribute("data-sync-uploaded-this-run", "0");
|
||||
root.removeAttribute("data-sync-manual-retry");
|
||||
setManualRetryAvailable(false);
|
||||
const [next] = await pendingCommands(database);
|
||||
if (next) synchronize(validatePending(next)).catch(failPermanently);
|
||||
});
|
||||
window.addEventListener("online", async () => {
|
||||
if (root.getAttribute("data-sync-phase") !== "offline") return;
|
||||
setManualRetryAvailable(false);
|
||||
const [next] = await pendingCommands(database);
|
||||
if (next) synchronize(validatePending(next)).catch(failPermanently);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user