Skip to content

Commit

Permalink
refactor ⚙: Refactored the procedure applying the same logic of the p…
Browse files Browse the repository at this point in the history
…revious one
  • Loading branch information
lorenzoronzani committed Jun 19, 2024
1 parent 51fe8a4 commit 724f660
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
20 changes: 4 additions & 16 deletions frontend/app/dashboard/new-deployment/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {displayE8sAsIcp, icpToE8s} from "@/helpers/ui";
import {Spinner} from "@/components/spinner";
import {NewDeploymentForm} from "@/components/new-deployment-form";
import {transferE8sToBackend} from "@/services/backend";
import {confirmDeployment} from "@/services/deployment";
import {confirmDeployment, updateDeploymentState} from "@/services/deployment";

const FETCH_DEPLOYMENT_PRICE_INTERVAL_MS = 30_000; // 30 seconds

Expand Down Expand Up @@ -156,9 +156,6 @@ export default function NewDeployment() {

try {
if ("LeaseCreated" in deploymentUpdate.update) {
// closeWs();
// return;

const deploymentCreatedState = deploymentSteps.find((el) =>
el.hasOwnProperty("DeploymentCreated")
)!;
Expand All @@ -183,12 +180,7 @@ export default function NewDeployment() {

setDeploymentSteps((prev) => [...prev, stepFailed]);

extractOk(
await backendActor!.update_deployment_state(
deploymentUpdate.id,
stepFailed
)
);
await updateDeploymentState(backendActor!, deploymentUpdate.id, stepFailed);
} catch (e) {
console.error("Failed to update deployment:", e);
}
Expand All @@ -207,12 +199,8 @@ export default function NewDeployment() {
Active: null,
};
setDeploymentSteps((prev) => [...prev, stepActive]);
extractOk(
await backendActor!.update_deployment_state(
deploymentUpdate.id,
stepActive
)
);

await updateDeploymentState(backendActor!, deploymentUpdate.id, stepActive);

await fetchDeployments(backendActor!);

Expand Down
42 changes: 29 additions & 13 deletions frontend/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import {
isDeploymentFailed,
} from "@/helpers/deployment";
import {displayIcp} from "@/helpers/ui";
import {confirmDeployment, queryLeaseStatus} from "@/services/deployment";
import {confirmDeployment, queryLeaseStatus, updateDeploymentState} from "@/services/deployment";
import {DeploymentTier} from "@/types/deployment";
import {ChevronsUpDown} from "lucide-react";
import {useRouter} from "next/navigation";
import {useCallback, useEffect, useState} from "react";
import {extractOk} from "@/helpers/result";

export default function Dashboard() {
const router = useRouter();
Expand Down Expand Up @@ -128,6 +127,8 @@ export default function Dashboard() {
const lastState = deployment.deployment.state_history[deployment.deployment.state_history.length - 1][1];
const deploymentCreatedState = deployment.deployment.state_history.find(([_, state]) => "DeploymentCreated" in state)![1];
if ("LeaseCreated" in lastState) {
let leaseCreated = false;

try {
const cert = await loadOrCreateCertificate(backendActor!);

Expand All @@ -137,20 +138,35 @@ export default function Dashboard() {
cert!
);

const stepActive = {
Active: null,
};
leaseCreated = true;
} catch (e) {
console.error("Failed to update deployment:", e);

extractOk(
await backendActor!.update_deployment_state(
deployment.id,
stepActive
)
);
try {
const stepFailed = {
FailedOnClient: {
reason: JSON.stringify(e),
},
};
await updateDeploymentState(backendActor!, deployment.id, stepFailed);
} catch (e) {
console.error("Failed to update deployment:", e);
}
}

await fetchDeployments(backendActor!);
try {
if (leaseCreated) {

const stepActive = {
Active: null,
};

await updateDeploymentState(backendActor!, deployment.id, stepActive);

await fetchDeployments(backendActor!);
}
} catch (e) {
console.error("Failed to update deployment:", e);
console.error("Failed to complete deployment:", e);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions frontend/services/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {X509CertificateData} from "@/lib/certificate";
import {wait} from "@/helpers/timer";
import {DeploymentState, MTlsCertificateData} from "@/declarations/backend.did";
import {extractDeploymentCreated} from "@/helpers/deployment";
import {BackendActor} from "@/services/backend";
import {extractOk} from "@/helpers/result";

const PROVIDER_PROXY_URL = "https://akash-provider-proxy.omnia-network.com/";

Expand Down Expand Up @@ -103,3 +105,13 @@ export const confirmDeployment = async (deploymentState: DeploymentState, deploy
throw e;
}
}

export const updateDeploymentState = async (backendActor: BackendActor, deploymentId: string, step: DeploymentState) => {
try {
extractOk(await backendActor.update_deployment_state(deploymentId, step));
} catch (e) {
console.error("Failed to update deployment state", e);
throw e;
}

}

0 comments on commit 724f660

Please sign in to comment.