-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: separate deployment initialize and start steps #2522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
965cd22
Enable setting the initial status on deployment creation
myftija 77a979f
Expose endpoint to start deployments
myftija 3e5277d
Extend build timeout on deployment start
myftija 0f6b0f1
Use separate timeout value for queued deployments
myftija a3fd1c2
Add startedAt to the deployment schema
myftija 84fbd77
Show the new startedAt instead of createdAt in the dashboard
myftija 7041dc3
Show github user tag also in the deployment details page
myftija 31a6644
Show `pending` deployment status as `queued` in the dashboard
myftija 977f023
Apply some good 🐰 suggestions
myftija bbc4b16
Add missing return
myftija File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
apps/webapp/app/routes/api.v1.deployments.$deploymentId.start.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; | ||
| import { StartDeploymentRequestBody } from "@trigger.dev/core/v3"; | ||
| import { z } from "zod"; | ||
| import { authenticateRequest } from "~/services/apiAuth.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { DeploymentService } from "~/v3/services/deployment.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| deploymentId: z.string(), | ||
| }); | ||
|
|
||
| export async function action({ request, params }: ActionFunctionArgs) { | ||
| if (request.method.toUpperCase() !== "POST") { | ||
| return json({ error: "Method Not Allowed" }, { status: 405 }); | ||
| } | ||
|
|
||
| const parsedParams = ParamsSchema.safeParse(params); | ||
|
|
||
| if (!parsedParams.success) { | ||
| return json({ error: "Invalid params" }, { status: 400 }); | ||
| } | ||
|
|
||
| const authenticationResult = await authenticateRequest(request, { | ||
| apiKey: true, | ||
| organizationAccessToken: false, | ||
| personalAccessToken: false, | ||
| }); | ||
|
|
||
| if (!authenticationResult || !authenticationResult.result.ok) { | ||
| logger.info("Invalid or missing api key", { url: request.url }); | ||
| return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
| } | ||
|
|
||
| const { environment: authenticatedEnv } = authenticationResult.result; | ||
| const { deploymentId } = parsedParams.data; | ||
|
|
||
| const rawBody = await request.json(); | ||
| const body = StartDeploymentRequestBody.safeParse(rawBody); | ||
|
|
||
| if (!body.success) { | ||
| return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 }); | ||
| } | ||
|
|
||
| const deploymentService = new DeploymentService(); | ||
|
|
||
| await deploymentService | ||
| .startDeployment(authenticatedEnv, deploymentId, { | ||
| contentHash: body.data.contentHash, | ||
| git: body.data.gitMeta, | ||
| runtime: body.data.runtime, | ||
| }) | ||
| .match( | ||
| () => { | ||
| return json(null, { status: 204 }); | ||
| }, | ||
| (error) => { | ||
| switch (error.type) { | ||
| case "failed_to_extend_deployment_timeout": | ||
| return json(null, { status: 204 }); // ignore these errors for now | ||
| case "deployment_not_found": | ||
| return json({ error: "Deployment not found" }, { status: 404 }); | ||
| case "deployment_not_pending": | ||
| return json({ error: "Deployment is not pending" }, { status: 409 }); | ||
| case "other": | ||
| default: | ||
| error.type satisfies "other"; | ||
| return json({ error: "Internal server error" }, { status: 500 }); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { type AuthenticatedEnvironment } from "~/services/apiAuth.server"; | ||
| import { BaseService } from "./baseService.server"; | ||
| import { errAsync, fromPromise, okAsync } from "neverthrow"; | ||
| import { type WorkerDeploymentStatus, type WorkerDeployment } from "@trigger.dev/database"; | ||
| import { logger, type GitMeta } from "@trigger.dev/core/v3"; | ||
| import { TimeoutDeploymentService } from "./timeoutDeployment.server"; | ||
| import { env } from "~/env.server"; | ||
|
|
||
| export class DeploymentService extends BaseService { | ||
| public startDeployment( | ||
| authenticatedEnv: AuthenticatedEnvironment, | ||
| friendlyId: string, | ||
| updates: Partial<Pick<WorkerDeployment, "contentHash" | "runtime"> & { git: GitMeta }> | ||
| ) { | ||
| const getDeployment = () => | ||
| fromPromise( | ||
| this._prisma.workerDeployment.findFirst({ | ||
| where: { | ||
| friendlyId, | ||
| environmentId: authenticatedEnv.id, | ||
| }, | ||
| select: { | ||
| status: true, | ||
| id: true, | ||
| }, | ||
| }), | ||
| (error) => ({ | ||
| type: "other" as const, | ||
| cause: error, | ||
| }) | ||
| ).andThen((deployment) => { | ||
| if (!deployment) { | ||
| return errAsync({ type: "deployment_not_found" as const }); | ||
| } | ||
| return okAsync(deployment); | ||
| }); | ||
|
|
||
| const validateDeployment = (deployment: Pick<WorkerDeployment, "id" | "status">) => { | ||
| if (deployment.status !== "PENDING") { | ||
| logger.warn("Attempted starting deployment that is not in PENDING status", { | ||
| deployment, | ||
| }); | ||
| return errAsync({ type: "deployment_not_pending" as const }); | ||
| } | ||
|
|
||
| return okAsync(deployment); | ||
| }; | ||
|
|
||
| const updateDeployment = (deployment: Pick<WorkerDeployment, "id">) => | ||
| fromPromise( | ||
| this._prisma.workerDeployment.updateMany({ | ||
| where: { id: deployment.id, status: "PENDING" }, // status could've changed in the meantime, we're not locking the row | ||
| data: { ...updates, status: "BUILDING", startedAt: new Date() }, | ||
| }), | ||
| (error) => ({ | ||
| type: "other" as const, | ||
| cause: error, | ||
| }) | ||
| ).andThen((result) => { | ||
| if (result.count === 0) { | ||
| return errAsync({ type: "deployment_not_pending" as const }); | ||
| } | ||
| return okAsync({ id: deployment.id }); | ||
| }); | ||
|
|
||
| const extendTimeout = (deployment: Pick<WorkerDeployment, "id">) => | ||
| fromPromise( | ||
| TimeoutDeploymentService.enqueue( | ||
| deployment.id, | ||
| "BUILDING" satisfies WorkerDeploymentStatus, | ||
| "Building timed out", | ||
| new Date(Date.now() + env.DEPLOY_TIMEOUT_MS) | ||
| ), | ||
| (error) => ({ | ||
| type: "failed_to_extend_deployment_timeout" as const, | ||
| cause: error, | ||
| }) | ||
| ).map(() => undefined); | ||
|
|
||
|
myftija marked this conversation as resolved.
|
||
| return getDeployment() | ||
| .andThen(validateDeployment) | ||
| .andThen(updateDeployment) | ||
| .andThen(extendTimeout); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...tabase/prisma/migrations/20250918122438_add_started_at_to_deployment_schema/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE "public"."WorkerDeployment" ADD COLUMN "startedAt" TIMESTAMP(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.