From 1c13bcf66d233c4e2cf2583771ce0760303f9c0e Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Wed, 23 Oct 2024 14:56:34 +0530 Subject: [PATCH] Check for deployment status while creating project using deployer API --- .../backend/environments/local.toml.example | 10 -- .../src/components/projects/create/Deploy.tsx | 159 +++++++++++++----- .../components/projects/create/DeployStep.tsx | 82 +-------- 3 files changed, 118 insertions(+), 133 deletions(-) diff --git a/packages/backend/environments/local.toml.example b/packages/backend/environments/local.toml.example index 2c8eca54..efebff59 100644 --- a/packages/backend/environments/local.toml.example +++ b/packages/backend/environments/local.toml.example @@ -20,16 +20,6 @@ clientId = "" clientSecret = "" -[google] - clientId = "" - clientSecret = "" - -[turnkey] - apiBaseUrl = "https://api.turnkey.com" - apiPrivateKey = "" - apiPublicKey = "" - defaultOrganizationId = "" - [registryConfig] fetchDeploymentRecordDelay = 5000 checkAuctionStatusDelay = 5000 diff --git a/packages/frontend/src/components/projects/create/Deploy.tsx b/packages/frontend/src/components/projects/create/Deploy.tsx index 9e3bfd8d..4fb25c69 100644 --- a/packages/frontend/src/components/projects/create/Deploy.tsx +++ b/packages/frontend/src/components/projects/create/Deploy.tsx @@ -1,6 +1,5 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useNavigate, useParams, useSearchParams } from 'react-router-dom'; -import { Deployment, DeploymentStatus } from 'gql-client'; import { DeployStep, DeployStatus } from './DeployStep'; import { Stopwatch, setStopWatchOffset } from '../../StopWatch'; @@ -12,6 +11,23 @@ import { useGQLClient } from 'context/GQLClientContext'; const FETCH_DEPLOYMENTS_INTERVAL = 5000; +type RequestState = + | 'SUBMITTED' + | 'DEPLOYING' + | 'DEPLOYED' + | 'REMOVED' + | 'CANCELLED' + | 'ERROR'; + +type Record = { + id: string; + createTime: string; + app: string; + lastState: RequestState; + lastUpdate: string; + logAvailable: boolean; +}; + const Deploy = () => { const client = useGQLClient(); @@ -19,7 +35,7 @@ const Deploy = () => { const projectId = searchParams.get('projectId'); const [open, setOpen] = React.useState(false); - const [deployments, setDeployments] = useState([]); + const [record, setRecord] = useState(); const handleOpen = () => setOpen(!open); @@ -30,35 +46,84 @@ const Deploy = () => { navigate(`/${orgSlug}/projects/create`); }, []); - const fetchDeployments = useCallback(async () => { + const showSteps = useMemo(() => { + if (!record) { + return true; + } + + if ( + record.lastState === 'CANCELLED' || + record.lastState === 'REMOVED' || + record.lastState === 'ERROR' + ) { + return false; + } else { + return true; + } + }, [record]); + + const showTimer = useMemo(() => { + if (!record) { + return true; + } + + if ( + record.lastState === 'CANCELLED' || + record.lastState === 'REMOVED' || + record.lastState === 'ERROR' || + record.lastState === 'DEPLOYED' + ) { + return false; + } else { + return true; + } + }, [record]); + + const fetchDeploymentRecords = useCallback(async () => { if (!projectId) { return; } const { deployments } = await client.getDeployments(projectId); - setDeployments(deployments); + const deployment = deployments[0]; + + try { + const response = await fetch( + `${deployment.deployer.deployerApiUrl}/${deployment.applicationDeploymentRequestId}`, + ); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const record: Record = await response.json(); + setRecord(record); + } catch (err: any) { + console.log('Error fetching data from deployer', err); + } }, [client, projectId]); useEffect(() => { - fetchDeployments(); + fetchDeploymentRecords(); const interval = setInterval(() => { - fetchDeployments(); + fetchDeploymentRecords(); }, FETCH_DEPLOYMENTS_INTERVAL); return () => { clearInterval(interval); }; - }, [fetchDeployments]); + }, [fetchDeploymentRecords]); useEffect(() => { - if ( - deployments.length > 0 && - deployments[0].status === DeploymentStatus.Ready - ) { + if (!record) { + return; + } + + if (record.lastState === 'DEPLOYED') { navigate(`/${orgSlug}/projects/create/success/${projectId}`); } - }, [deployments]); + }, [record]); return (
@@ -67,12 +132,14 @@ const Deploy = () => { Deployment started ... -
- - -
+ {showTimer && ( +
+ + +
+ )}
- - {/* Collapsible */} - -
- {/* Logs */} - {processLogs.map((log, key) => { - return ( -

- {log} -

- ); - })} - - {/* End of logs ref used for hiding gradient overlay */} -
- - {/* Overflow gradient overlay */} - {!hideGradientOverlay && ( -
- )} - - {/* Copy log button */} -
- -
-
-
); };