feat: recover interrupted installation preparation safely
This commit is contained in:
@@ -130,7 +130,7 @@ func importBindings(ctx context.Context, plan preflight.Plan, runner compose.Run
|
||||
|
||||
func runPreparedClient(ctx context.Context, plan preflight.Plan, runner compose.Runner, payload []byte, prepare bool) (BindingReport, error) {
|
||||
client, success, timeout := bindingClient, "bindings_ready", 15*time.Minute
|
||||
codes := []string{"bindings_ready", "authentication_required", "workspace_reconciliation_required", "binding_reconciliation_required", "container_connection_failed", "binding_import_failed"}
|
||||
codes := []string{"bindings_ready", "authentication_required", "workspace_reconciliation_required", "binding_reconciliation_required", "container_connection_failed", "binding_import_failed", "binding_operation_pending", "schema_review_required"}
|
||||
if prepare {
|
||||
client, success, timeout = readinessClient, "workspaces_prepared", 60*time.Minute
|
||||
codes = append(codes, "workspaces_prepared", "workspace_preparation_failed", "session_transport_unsupported", "schema_review_required", "schema_sync_failed", "schema_sync_pending", "preprocessing_failed", "preprocessing_blocked", "preprocessing_pending")
|
||||
|
||||
@@ -5,6 +5,25 @@ const jsonResponse = async (response, code) => {
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export async function importBinding(request, entry, revision, pause = () => new Promise(resolve => setTimeout(resolve, 2000))) {
|
||||
for (let attempt = 0; attempt < 300; attempt++) {
|
||||
const response = await request("/installation/bindings", { ...entry, revision });
|
||||
if (response.status === 409) {
|
||||
const value = await response.json();
|
||||
if (value.code !== "installation_operation_pending") throw stopped("binding_reconciliation_required");
|
||||
const databases = await jsonResponse(await request("/catalog/databases"), "binding_import_failed");
|
||||
const active = databases.find(item => item.workspaceId === entry.configuration.workspaceId)?.activeSyncRun;
|
||||
if (active?.state === "awaiting_confirmation") throw stopped("schema_review_required", active.id);
|
||||
await pause(); continue;
|
||||
}
|
||||
if (response.status === 422) throw stopped("container_connection_failed");
|
||||
const value = await jsonResponse(response, "binding_import_failed");
|
||||
if (value.connectionStatus !== "reachable" || !["applied", "unchanged"].includes(value.status)) throw stopped("binding_import_failed");
|
||||
return;
|
||||
}
|
||||
throw stopped("binding_operation_pending");
|
||||
}
|
||||
|
||||
export async function prepareWorkspace(request, workspaceId, revision, pause = () => new Promise(resolve => setTimeout(resolve, 2000))) {
|
||||
const databases = await jsonResponse(await request("/catalog/databases"), "workspace_preparation_failed");
|
||||
const database = databases.find(item => item.workspaceId === workspaceId);
|
||||
@@ -12,6 +31,11 @@ export async function prepareWorkspace(request, workspaceId, revision, pause = (
|
||||
if (database.binding.transport === "ssh_tunnel") throw stopped("session_transport_unsupported");
|
||||
const runs = await jsonResponse(await request(`/catalog/databases/${database.id}/sync-runs`), "schema_sync_failed");
|
||||
let active = runs.find(run => ["queued", "running", "awaiting_confirmation", "applying"].includes(run.state));
|
||||
const interrupted = runs.find(run => ["interrupted", "failed"].includes(run.state) && run.requestedDatabaseVersion === database.version &&
|
||||
run.scope === "all" && (database.schemaSyncedVersion !== database.version || run.phase === "memory_cleanup"));
|
||||
if (!active && interrupted) {
|
||||
active = await jsonResponse(await request(`/catalog/sync-runs/${interrupted.id}/retry`, {}), "schema_sync_failed");
|
||||
}
|
||||
const waitForSync = async run => {
|
||||
for (let attempt = 0; attempt < 900; attempt++) {
|
||||
if (run.state === "awaiting_confirmation") throw stopped("schema_review_required", run.id);
|
||||
@@ -40,6 +64,11 @@ export async function prepareWorkspace(request, workspaceId, revision, pause = (
|
||||
for (let attempt = 0; attempt < 900; attempt++) {
|
||||
if (state.state === "ready") return { workspaceId, state: "ready" };
|
||||
if (state.state === "blocked") throw stopped("preprocessing_blocked");
|
||||
if (state.state === "running" && attempt % 15 === 0) {
|
||||
const response = await request(path, { recoverInterrupted: true });
|
||||
if (response.status !== 409) await jsonResponse(response, "preprocessing_failed");
|
||||
started = true;
|
||||
} else
|
||||
if (["required", "failed"].includes(state.state) && !started) {
|
||||
started = true;
|
||||
await jsonResponse(await request(path, {}), "preprocessing_failed");
|
||||
@@ -51,7 +80,8 @@ export async function prepareWorkspace(request, workspaceId, revision, pause = (
|
||||
}
|
||||
|
||||
const readinessCodes = new Set(["workspace_preparation_failed", "workspace_reconciliation_required", "session_transport_unsupported",
|
||||
"schema_review_required", "schema_sync_failed", "schema_sync_pending", "preprocessing_failed", "preprocessing_blocked", "preprocessing_pending"]);
|
||||
"schema_review_required", "schema_sync_failed", "schema_sync_pending", "preprocessing_failed", "preprocessing_blocked", "preprocessing_pending",
|
||||
"binding_reconciliation_required", "container_connection_failed", "binding_import_failed", "binding_operation_pending"]);
|
||||
|
||||
export async function importPreparedBindings(base = "http://core:8787", mode = "bindings") {
|
||||
let cookie = "", csrf = "", origin = "", completed = 0, code = "authentication_required";
|
||||
@@ -77,6 +107,7 @@ try {
|
||||
if (!source.ok) throw Error();
|
||||
const { revision } = await source.json();
|
||||
for (const entry of prepared.databases) {
|
||||
workspaceId = entry.configuration.workspaceId;
|
||||
if (mode === "workspaces") {
|
||||
workspaceId = entry.configuration.workspaceId;
|
||||
code = "workspace_preparation_failed";
|
||||
@@ -85,12 +116,7 @@ try {
|
||||
continue;
|
||||
}
|
||||
code = "binding_import_failed";
|
||||
const response = await request("/installation/bindings", { ...entry, revision });
|
||||
if (response.status === 409) code = "binding_reconciliation_required";
|
||||
if (response.status === 422) code = "container_connection_failed";
|
||||
if (!response.ok) throw Error();
|
||||
const value = await response.json();
|
||||
if (value.connectionStatus !== "reachable" || !["applied", "unchanged"].includes(value.status)) throw Error();
|
||||
await importBinding(request, entry, revision);
|
||||
completed++;
|
||||
}
|
||||
workspaceId = undefined;
|
||||
|
||||
@@ -52,6 +52,11 @@ type journal struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (r Result) NeedsReview() bool {
|
||||
return (r.WorkspacePreparation != nil && r.WorkspacePreparation.Code == "schema_review_required") ||
|
||||
(r.BindingImport != nil && r.BindingImport.Code == "schema_review_required")
|
||||
}
|
||||
|
||||
// Check must repeat document validation and live prerequisite probes at the public CLI boundary.
|
||||
// It is mandatory; historical success in the plan never authorizes skipping current checks.
|
||||
type Check func(context.Context, preflight.Plan) error
|
||||
@@ -255,7 +260,7 @@ func Run(ctx context.Context, request Request, runner compose.Runner, check Chec
|
||||
// A journal entry alone is never proof that a container or migration still exists.
|
||||
for _, stage := range stages {
|
||||
if err := operation(stage.name, stage.run); err != nil {
|
||||
if result.WorkspacePreparation != nil && result.WorkspacePreparation.Code == "schema_review_required" {
|
||||
if result.NeedsReview() {
|
||||
return fail(stage.name, "Review the pending schema synchronization in application Administration, then repeat apply. No destructive change was confirmed by the installer.")
|
||||
}
|
||||
return fail(stage.name, err.Error())
|
||||
|
||||
Reference in New Issue
Block a user