-
-
Notifications
You must be signed in to change notification settings - Fork 921
feat(webapp): Add support for resetting idempotency keys #2777
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
Open
mpcgrid
wants to merge
11
commits into
main
Choose a base branch
from
feat/tri-6733-reset-an-idempotencykey
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dda38b7
feat: add support for resetting idempotency keys from run detail view
mpcgrid 0210604
Merge branch 'main' into feat/tri-6733-reset-an-idempotencykey
mpcgrid 4db1c68
review fixes
mpcgrid 8425bed
Merge branch 'refs/heads/main' into feat/tri-6733-reset-an-idempotenc…
mpcgrid 1e02704
fixed code rabbit review
mpcgrid 03f13a7
Merge branch 'main' into feat/tri-6733-reset-an-idempotencykey
mpcgrid bd07781
fixed code rabbit review
mpcgrid 4463cf5
Merge remote-tracking branch 'origin/feat/tri-6733-reset-an-idempoten…
mpcgrid f232367
Merge branch 'main' into feat/tri-6733-reset-an-idempotencykey
mpcgrid f7c4854
added log for reset idempotency
mpcgrid 277f04c
Merge branch 'main' into feat/tri-6733-reset-an-idempotencykey
mpcgrid 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@trigger.dev/sdk": patch | ||
| "@trigger.dev/core": patch | ||
| --- | ||
|
|
||
| Added support for idempotency reset |
51 changes: 51 additions & 0 deletions
51
apps/webapp/app/routes/api.v1.idempotencyKeys.$key.reset.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,51 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { ServiceValidationError } from "~/v3/services/baseService.server"; | ||
| import { z } from "zod"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { ResetIdempotencyKeyService } from "~/v3/services/resetIdempotencyKey.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| key: z.string(), | ||
| }); | ||
|
|
||
| const BodySchema = z.object({ | ||
| taskIdentifier: z.string().min(1, "Task identifier is required"), | ||
| }); | ||
|
|
||
| export const { action } = createActionApiRoute( | ||
| { | ||
| params: ParamsSchema, | ||
| body: BodySchema, | ||
| allowJWT: true, | ||
| corsStrategy: "all", | ||
| authorization: { | ||
| action: "write", | ||
| resource: () => ({}), | ||
| superScopes: ["write:runs", "admin"], | ||
| }, | ||
| }, | ||
| async ({ params, body, authentication }) => { | ||
| const service = new ResetIdempotencyKeyService(); | ||
|
|
||
| try { | ||
| const result = await service.call( | ||
| params.key, | ||
| body.taskIdentifier, | ||
| authentication.environment | ||
| ); | ||
| return json(result, { status: 200 }); | ||
| } catch (error) { | ||
| if (error instanceof ServiceValidationError) { | ||
| return json({ error: error.message }, { status: error.status ?? 400 }); | ||
| } | ||
|
|
||
| logger.error("Failed to reset idempotency key via API", { | ||
| error: error instanceof Error ? { name: error.name, message: error.message, stack: error.stack } : String(error), | ||
| }); | ||
|
|
||
| return json({ error: "Internal Server Error" }, { status: 500 }); | ||
| } | ||
|
|
||
| } | ||
| ); |
127 changes: 127 additions & 0 deletions
127
...nizationSlug.projects.$projectParam.env.$envParam.runs.$runParam.idempotencyKey.reset.tsx
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,127 @@ | ||
| import { parse } from "@conform-to/zod"; | ||
| import { type ActionFunction, json } from "@remix-run/node"; | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { jsonWithErrorMessage } from "~/models/message.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
| import { requireUserId } from "~/services/session.server"; | ||
| import { ResetIdempotencyKeyService } from "~/v3/services/resetIdempotencyKey.server"; | ||
| import { v3RunParamsSchema } from "~/utils/pathBuilder"; | ||
|
|
||
| export const resetIdempotencyKeySchema = z.object({ | ||
| taskIdentifier: z.string().min(1, "Task identifier is required"), | ||
| }); | ||
|
|
||
| export const action: ActionFunction = async ({ request, params }) => { | ||
| const userId = await requireUserId(request); | ||
| const { projectParam, organizationSlug, envParam, runParam } = | ||
| v3RunParamsSchema.parse(params); | ||
|
|
||
| const formData = await request.formData(); | ||
| const submission = parse(formData, { schema: resetIdempotencyKeySchema }); | ||
|
|
||
| if (!submission.value) { | ||
| return json(submission); | ||
| } | ||
|
|
||
| try { | ||
| const { taskIdentifier } = submission.value; | ||
|
|
||
| const taskRun = await prisma.taskRun.findFirst({ | ||
| where: { | ||
| friendlyId: runParam, | ||
| project: { | ||
| slug: projectParam, | ||
| organization: { | ||
| slug: organizationSlug, | ||
| members: { | ||
| some: { | ||
| userId, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| runtimeEnvironment: { | ||
| slug: envParam, | ||
| }, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| idempotencyKey: true, | ||
| taskIdentifier: true, | ||
| runtimeEnvironmentId: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!taskRun) { | ||
| submission.error = { runParam: ["Run not found"] }; | ||
| return json(submission); | ||
| } | ||
|
|
||
| if (!taskRun.idempotencyKey) { | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| "This run does not have an idempotency key" | ||
| ); | ||
| } | ||
|
|
||
| if (taskRun.taskIdentifier !== taskIdentifier) { | ||
| submission.error = { taskIdentifier: ["Task identifier does not match this run"] }; | ||
| return json(submission); | ||
| } | ||
|
|
||
| const environment = await prisma.runtimeEnvironment.findUnique({ | ||
| where: { | ||
| id: taskRun.runtimeEnvironmentId, | ||
| }, | ||
| include: { | ||
| project: { | ||
| include: { | ||
| organization: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!environment) { | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| "Environment not found" | ||
| ); | ||
| } | ||
|
|
||
| const service = new ResetIdempotencyKeyService(); | ||
|
|
||
| await service.call(taskRun.idempotencyKey, taskIdentifier, { | ||
| ...environment, | ||
| organizationId: environment.project.organizationId, | ||
| organization: environment.project.organization, | ||
| }); | ||
|
|
||
| return json({ success: true }); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| logger.error("Failed to reset idempotency key", { | ||
| error: { | ||
| name: error.name, | ||
| message: error.message, | ||
| stack: error.stack, | ||
| }, | ||
| }); | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| `Failed to reset idempotency key: ${error.message}` | ||
| ); | ||
| } else { | ||
| logger.error("Failed to reset idempotency key", { error }); | ||
| return jsonWithErrorMessage( | ||
| submission, | ||
| request, | ||
| `Failed to reset idempotency key: ${JSON.stringify(error)}` | ||
| ); | ||
| } | ||
| } | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import type { AuthenticatedEnvironment } from "~/services/apiAuth.server"; | ||
| import { BaseService, ServiceValidationError } from "./baseService.server"; | ||
| import { logger } from "~/services/logger.server"; | ||
|
|
||
| export class ResetIdempotencyKeyService extends BaseService { | ||
| public async call( | ||
| idempotencyKey: string, | ||
| taskIdentifier: string, | ||
| authenticatedEnv: AuthenticatedEnvironment | ||
| ): Promise<{ id: string }> { | ||
| const { count } = await this._prisma.taskRun.updateMany({ | ||
| where: { | ||
| idempotencyKey, | ||
| taskIdentifier, | ||
| runtimeEnvironmentId: authenticatedEnv.id, | ||
| }, | ||
| data: { | ||
| idempotencyKey: null, | ||
| idempotencyKeyExpiresAt: null, | ||
| }, | ||
| }); | ||
|
|
||
| if (count === 0) { | ||
| throw new ServiceValidationError( | ||
| `No runs found with idempotency key: ${idempotencyKey} and task: ${taskIdentifier}`, | ||
| 404 | ||
| ); | ||
| } | ||
|
|
||
| logger.info( | ||
| `Reset idempotency key: ${idempotencyKey} for task: ${taskIdentifier} in env: ${authenticatedEnv.id}, affected ${count} run(s)` | ||
| ); | ||
|
|
||
| return { id: idempotencyKey }; | ||
| } | ||
| } | ||
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.