Skip to content

Commit

Permalink
refactor ⚙: Refactoring the sendManifestFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzoronzani committed Jun 19, 2024
1 parent cddacde commit a873f77
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
23 changes: 7 additions & 16 deletions frontend/app/dashboard/new-deployment/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +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 {extractDeploymentCreated} from "@/helpers/deployment";
import {sendManifestToProvider} from "@/services/deployment";
import {sendManifestToProviderFlow} from "@/services/deployment";

const FETCH_DEPLOYMENT_PRICE_INTERVAL_MS = 30_000; // 30 seconds

Expand Down Expand Up @@ -160,21 +159,13 @@ export default function NewDeployment() {
// closeWs();
// return;

const {manifest_sorted_json, dseq} = extractDeploymentCreated(
deploymentSteps.find((el) =>
el.hasOwnProperty("DeploymentCreated")
)!
);
const {provider_url} = deploymentUpdate.update.LeaseCreated;

const manifestUrl = new URL(
`/deployment/${dseq}/manifest`,
provider_url
);
const deploymentCreatedState = deploymentSteps.find((el) =>
el.hasOwnProperty("DeploymentCreated")
)!;

await sendManifestToProvider(
manifestUrl.toString(),
manifest_sorted_json,
await sendManifestToProviderFlow(
deploymentUpdate.update,
deploymentCreatedState,
tlsCertificateData!
);

Expand Down
23 changes: 6 additions & 17 deletions frontend/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
isDeploymentFailed,
} from "@/helpers/deployment";
import {displayIcp} from "@/helpers/ui";
import {queryLeaseStatus, sendManifestToProvider} from "@/services/deployment";
import {queryLeaseStatus, sendManifestToProviderFlow} from "@/services/deployment";
import {DeploymentTier} from "@/types/deployment";
import {ChevronsUpDown} from "lucide-react";
import {useRouter} from "next/navigation";
Expand Down Expand Up @@ -126,25 +126,14 @@ export default function Dashboard() {
const checkDeploymentState = useCallback(async () => {
for (const deployment of deployments) {
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) {
try {
const cert = await loadOrCreateCertificate(backendActor!);

const deploymentCreatedState = deployment.deployment.state_history.find(([_, state]) => "DeploymentCreated" in state)!;
const {manifest_sorted_json, dseq} = extractDeploymentCreated(
deploymentCreatedState[1]
);

const {provider_url} = lastState.LeaseCreated;

const manifestUrl = new URL(
`/deployment/${dseq}/manifest`,
provider_url
);

await sendManifestToProvider(
manifestUrl.toString(),
manifest_sorted_json,
await sendManifestToProviderFlow(
lastState,
deploymentCreatedState,
cert!
);

Expand All @@ -163,7 +152,7 @@ export default function Dashboard() {
}
}
}
}, [deployments]);
}, [backendActor, deployments, loadOrCreateCertificate]);

return (
<div className="flex-1 space-y-4 p-8 pt-6">
Expand Down
32 changes: 30 additions & 2 deletions frontend/services/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { X509CertificateData } from "@/lib/certificate";
import { wait } from "@/helpers/timer";
import {X509CertificateData} from "@/lib/certificate";
import {wait} from "@/helpers/timer";
import {DeploymentState, MTlsCertificateData} from "@/declarations/backend.did";
import {extractDeploymentCreated} from "@/helpers/deployment";

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

Expand Down Expand Up @@ -74,3 +76,29 @@ export const queryLeaseStatus = async (queryLeaseUrl: string, certData: X509Cert

return await res.json();
};

export const sendManifestToProviderFlow = async (deploymentState: DeploymentState, deploymentCreatedState: DeploymentState, cert: MTlsCertificateData) => {
try {
if ("LeaseCreated" in deploymentState) {
const {manifest_sorted_json, dseq} = extractDeploymentCreated(deploymentCreatedState);

const {provider_url} = deploymentState.LeaseCreated;

const manifestUrl = new URL(
`/deployment/${dseq}/manifest`,
provider_url
);

await sendManifestToProvider(
manifestUrl.toString(),
manifest_sorted_json,
cert!
);
} else {
throw new Error("Deployment state is not in LeaseCreated state");
}
} catch (e) {
console.error("Failed to send manifest to provider", e);
throw e;
}
}

0 comments on commit a873f77

Please sign in to comment.