Skip to content

Commit b3de0cf

Browse files
committed
Cancel run added to the SDK
1 parent 9e2b07b commit b3de0cf

File tree

8 files changed

+142
-8
lines changed

8 files changed

+142
-8
lines changed

.changeset/tiny-doors-type.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Added cancelRun to the SDK

apps/webapp/app/routes/api.v2.runs.$runParam.cancel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
2525
const parsed = ParamsSchema.safeParse(params);
2626

2727
if (!parsed.success) {
28-
return json({ error: "Invalid or Missing runId" }, { status: 400 });
28+
return json({ error: "Invalid or Missing run id" }, { status: 400 });
2929
}
3030

3131
const { runParam } = parsed.data;

docs/v3-openapi.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,101 @@
106106
}
107107
]
108108
}
109+
},
110+
"/api/v1/runs/{run_id}/cancel": {
111+
"post": {
112+
"description": "Cancels a run.",
113+
"parameters": [
114+
{
115+
"in": "path",
116+
"name": "run_id",
117+
"required": true,
118+
"schema": {
119+
"type": "string"
120+
},
121+
"description": "The ID of an existing run. When you trigger a run you will get an id in the response."
122+
}
123+
],
124+
"responses": {
125+
"200": {
126+
"description": "Successful request",
127+
"content": {
128+
"application/json": {
129+
"schema": {
130+
"type": "object",
131+
"properties": {
132+
"message": {
133+
"type": "string",
134+
"description": "Confirmation message that the run was canceled."
135+
}
136+
}
137+
}
138+
}
139+
}
140+
},
141+
"400": {
142+
"description": "Invalid request",
143+
"content": {
144+
"application/json": {
145+
"schema": {
146+
"type": "object",
147+
"properties": {
148+
"error": {
149+
"type": "string",
150+
"enum": ["Invalid or missing run ID", "Failed to create new run"]
151+
}
152+
}
153+
}
154+
}
155+
}
156+
},
157+
"401": {
158+
"description": "Unauthorized request",
159+
"content": {
160+
"application/json": {
161+
"schema": {
162+
"type": "object",
163+
"properties": {
164+
"error": {
165+
"type": "string",
166+
"enum": ["Invalid or Missing API key"]
167+
}
168+
}
169+
}
170+
}
171+
}
172+
},
173+
"404": {
174+
"description": "Resource not found",
175+
"content": {
176+
"application/json": {
177+
"schema": {
178+
"type": "object",
179+
"properties": {
180+
"error": {
181+
"type": "string",
182+
"enum": ["Run not found"]
183+
}
184+
}
185+
}
186+
}
187+
}
188+
}
189+
},
190+
"tags": ["run"],
191+
"security": [{ "bearerAuth": [] }],
192+
"operationId": "replay_run_v1",
193+
"x-codeSamples": [
194+
{
195+
"lang": "typescript",
196+
"source": "await cancelRun(existingRun.id);"
197+
},
198+
{
199+
"lang": "sh",
200+
"source": "curl --request POST \\\n\t--url https://api.trigger.dev/api/v1/runs/{run_id}/cancel \\\n\t--header 'Authorization: Bearer <token>'"
201+
}
202+
]
203+
}
109204
}
110205
},
111206
"components": {

docs/v3/rest-cancel-run.mdx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
---
2-
title: "REST API: Cancel run"
3-
sidebarTitle: "Cancel run"
4-
description: "Cancel a run using a run id."
2+
title: "Cancel run"
3+
openapi: "v3-openapi POST /api/v1/runs/{run_id}/cancel"
54
---
6-
7-
<Snippet file="incomplete-docs.mdx" />

packages/core/src/v3/apiClient/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
BatchTriggerTaskResponse,
1111
CreateUploadPayloadUrlResponseBody,
1212
ReplayRunResponse,
13+
CanceledRunResponse,
1314
} from "../schemas";
15+
import { z } from "zod";
1416

1517
export type TriggerOptions = {
1618
spanParentAsLink?: boolean;
@@ -101,6 +103,18 @@ export class ApiClient {
101103
);
102104
}
103105

106+
cancelRun(runId: string) {
107+
return zodfetch(
108+
CanceledRunResponse,
109+
`${this.baseUrl}/api/v2/runs/${runId}/cancel`,
110+
{
111+
method: "POST",
112+
headers: this.#getHeaders(false),
113+
},
114+
zodFetchOptions
115+
);
116+
}
117+
104118
#getHeaders(spanParentAsLink: boolean) {
105119
const headers: Record<string, string> = {
106120
"Content-Type": "application/json",

packages/core/src/v3/schemas/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,9 @@ export const ReplayRunResponse = z.object({
204204
});
205205

206206
export type ReplayRunResponse = z.infer<typeof ReplayRunResponse>;
207+
208+
export const CanceledRunResponse = z.object({
209+
message: z.string(),
210+
});
211+
212+
export type CanceledRunResponse = z.infer<typeof CanceledRunResponse>;

packages/trigger-sdk/src/v3/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export type { Context };
99

1010
export { logger, type LogLevel } from "@trigger.dev/core/v3";
1111

12-
export { replayRun } from "./management";
12+
export { replayRun, cancelRun } from "./management";

packages/trigger-sdk/src/v3/management.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReplayRunResponse, apiClientManager } from "@trigger.dev/core/v3";
1+
import { CanceledRunResponse, ReplayRunResponse, apiClientManager } from "@trigger.dev/core/v3";
22
import { apiClientMissingError } from "./shared";
33

44
export async function replayRun(runId: string): Promise<ReplayRunResponse> {
@@ -16,3 +16,19 @@ export async function replayRun(runId: string): Promise<ReplayRunResponse> {
1616

1717
return response.data;
1818
}
19+
20+
export async function cancelRun(runId: string): Promise<CanceledRunResponse> {
21+
const apiClient = apiClientManager.client;
22+
23+
if (!apiClient) {
24+
throw apiClientMissingError();
25+
}
26+
27+
const response = await apiClient.cancelRun(runId);
28+
29+
if (!response.ok) {
30+
throw new Error(response.error);
31+
}
32+
33+
return response.data;
34+
}

0 commit comments

Comments
 (0)