Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Jul 4, 2024
1 parent c74f493 commit 9f7bec3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 68 deletions.
2 changes: 0 additions & 2 deletions cli/pkg/local/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,6 @@ func (s *Server) RedeployProject(ctx context.Context, r *connect.Request[localv1
if err != nil {
return nil, err
}
} else {
// TODO: should we push change to github for redeploy on github connected project?
}

// TODO : Add other update project fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import DeployIcon from "@rilldata/web-common/components/icons/DeployIcon.svelte";
import Tooltip from "@rilldata/web-common/components/tooltip/Tooltip.svelte";
import TooltipContent from "@rilldata/web-common/components/tooltip/TooltipContent.svelte";
import { createDeployer } from "@rilldata/web-common/features/project/deploy";
import { waitUntil } from "@rilldata/web-common/lib/waitUtils";
import { ProjectDeployer } from "@rilldata/web-common/features/project/deploy";
import { behaviourEvent } from "@rilldata/web-common/metrics/initMetrics";
import {
createLocalServiceDeployValidation,
createLocalServiceRedeploy,
} from "@rilldata/web-common/runtime-client/local-service";
import { createLocalServiceDeployValidation } from "@rilldata/web-common/runtime-client/local-service";
import { get } from "svelte/store";
import { Button } from "../../../components/button";
export let type: "primary" | "secondary" = "primary";
Expand All @@ -28,41 +25,37 @@
},
});
$: isDeployed = !!$deployValidation.data?.deployedProjectId;
const deployer = new ProjectDeployer();
const deployerStatus = deployer.getStatus();
const deploying = deployer.deploying;
let open = false;
function onShowDeploy() {
if (isDeployed) {
return onDeploy();
if (deployer.isDeployed) {
return deployer.deploy();
}
open = true;
void behaviourEvent?.fireDeployIntentEvent();
}
let deploying = false;
const deploy = createDeployer();
const redeploy = createLocalServiceRedeploy();
$: isLoading = $deploy.isLoading || $redeploy.isLoading;
async function onDeploy() {
deploying = true;
await waitUntil(() => !$deployValidation.isLoading);
if (!(await $deploy.mutateAsync($deployValidation.data!))) return;
deploying = false;
if (!(await deployer.validate())) return;
await deployer.deploy();
open = false;
}
function handleVisibilityChange() {
if (document.visibilityState !== "visible" || !deploying) return;
void onDeploy();
if (document.visibilityState !== "visible" || !get(deploying)) return;
void deployer.validate();
}
$: console.log($deployerStatus);
</script>

<svelte:window on:visibilitychange={handleVisibilityChange} />

<Tooltip distance={8}>
<Button on:click={onShowDeploy} {type} loading={$deployValidation.isLoading}>
<Button on:click={onShowDeploy} {type} loading={$deployerStatus.isLoading}>
{isDeployed ? "Redeploy" : "Deploy"}
</Button>
<TooltipContent slot="tooltip-content">
Expand Down Expand Up @@ -90,7 +83,7 @@
<Button
type="primary"
on:click={onDeploy}
loading={isLoading || deploying}
loading={$deployerStatus.isLoading || $deploying}
>
Continue
</Button>
Expand Down
130 changes: 87 additions & 43 deletions web-common/src/features/project/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,101 @@
import type { ConnectError } from "@connectrpc/connect";
import { createMutation, CreateMutationOptions } from "@rilldata/svelte-query";
import { ConnectError } from "@connectrpc/connect";
import { waitUntil } from "@rilldata/web-common/lib/waitUtils";
import { DeployValidationResponse } from "@rilldata/web-common/proto/gen/rill/local/v1/api_pb";
import {
localServiceDeploy,
localServiceRedeploy,
createLocalServiceDeploy,
createLocalServiceDeployValidation,
createLocalServiceRedeploy,
} from "@rilldata/web-common/runtime-client/local-service";
import { derived, get, writable } from "svelte/store";

export function createDeployer(options?: {
mutation?: CreateMutationOptions<
Awaited<ReturnType<typeof deploy>>,
ConnectError,
DeployValidationResponse,
unknown
>;
}) {
const { mutation: mutationOptions } = options ?? {};
return createMutation<
Awaited<ReturnType<typeof deploy>>,
unknown,
DeployValidationResponse,
unknown
>(deploy, mutationOptions);
}
export class ProjectDeployer {
public readonly validation: ReturnType<
typeof createLocalServiceDeployValidation
> = createLocalServiceDeployValidation({
query: {
refetchOnWindowFocus: true,
},
});
public readonly deploying = writable(false);

private readonly deployMutation = createLocalServiceDeploy();
private readonly redeployMutation = createLocalServiceRedeploy();

async function deploy(deployValidation: DeployValidationResponse) {
if (!deployValidation.isAuthenticated) {
window.open(`${deployValidation.loginUrl}`, "__target");
return false;
public getStatus() {
return derived(
[this.validation, this.deployMutation, this.redeployMutation],
([validation, deployMutation, redeployMutation]) => {
if (
validation.isFetching ||
deployMutation.isLoading ||
redeployMutation.isLoading
) {
return {
isLoading: true,
error: undefined,
};
}

return {
isLoading: false,
error:
(validation.error as ConnectError)?.message ??
(deployMutation.error as ConnectError)?.message ??
(redeployMutation.error as ConnectError)?.message,
};
},
);
}

if (deployValidation.isGithubRepo && !deployValidation.isGithubConnected) {
// if the project is a github repo and not connected to github then redirect to grant access
window.open(`${deployValidation.githubGrantAccessUrl}`, "__target");
return false;
public get isDeployed() {
const validation = get(this.validation).data as DeployValidationResponse;
return !!validation?.deployedProjectId;
}

if (deployValidation.deployedProjectId) {
const resp = await localServiceRedeploy({
projectId: deployValidation.deployedProjectId,
reupload: !deployValidation.isGithubRepo,
});
if (resp.frontendUrl) {
window.open(resp.frontendUrl, "__target");
public async validate() {
this.deploying.set(false);
let validation = get(this.validation).data as DeployValidationResponse;
if (validation?.deployedProjectId) {
return true;
}

await waitUntil(() => !get(this.validation).isFetching);
validation = get(this.validation).data as DeployValidationResponse;

if (!validation.isAuthenticated) {
window.open(`${validation.loginUrl}`, "__target");
this.deploying.set(true);
return false;
}
} else {
const resp = await localServiceDeploy({
projectName: deployValidation.localProjectName,
org: deployValidation.rillUserOrgs[0],
upload: !deployValidation.isGithubRepo,
});
if (resp.frontendUrl) {

if (validation.isGithubRepo && !validation.isGithubConnected) {
// if the project is a github repo and not connected to github then redirect to grant access
window.open(`${validation.githubGrantAccessUrl}`, "__target");
this.deploying.set(true);
return false;
}

return true;
}

public async deploy() {
// safeguard around deploy
if (!(await this.validate())) return;

const validation = get(this.validation).data as DeployValidationResponse;
if (validation.deployedProjectId) {
const resp = await get(this.redeployMutation).mutateAsync({
projectId: validation.deployedProjectId,
reupload: !validation.isGithubRepo,
});
window.open(resp.frontendUrl, "__target");
} else {
const resp = await get(this.deployMutation).mutateAsync({
projectName: validation.localProjectName,
org: validation.rillUserOrgs[0],
upload: !validation.isGithubRepo,
});
window.open(resp.frontendUrl, "__target");
}
}
return true;
}

0 comments on commit 9f7bec3

Please sign in to comment.