Skip to content

Commit b236a73

Browse files
committed
Creating resume tokens is working
1 parent fde9918 commit b236a73

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { json } from "@remix-run/server-runtime";
2+
import { CreateWaitpointRequestBody, CreateWaitpointResponseBody } from "@trigger.dev/core/v3";
3+
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
4+
import { parseDelay } from "~/utils/delays";
5+
import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server";
6+
import { engine } from "~/v3/runEngine.server";
7+
8+
const { action } = createActionApiRoute(
9+
{
10+
body: CreateWaitpointRequestBody,
11+
maxContentLength: 1024 * 10, // 10KB
12+
method: "POST",
13+
},
14+
async ({ authentication, body }) => {
15+
const idempotencyKeyExpiresAt = body.idempotencyKeyTTL
16+
? resolveIdempotencyKeyTTL(body.idempotencyKeyTTL)
17+
: undefined;
18+
19+
const timeout = await parseDelay(body.timeout);
20+
21+
const waitpoint = await engine.createManualWaitpoint({
22+
environmentId: authentication.environment.id,
23+
projectId: authentication.environment.projectId,
24+
idempotencyKey: body.idempotencyKey,
25+
idempotencyKeyExpiresAt,
26+
timeout,
27+
});
28+
29+
return json<CreateWaitpointResponseBody>(
30+
{
31+
id: waitpoint.friendlyId,
32+
},
33+
{ status: 200 }
34+
);
35+
}
36+
);
37+
38+
export { action };

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
CreateEnvironmentVariableRequestBody,
1111
CreateScheduleOptions,
1212
CreateUploadPayloadUrlResponseBody,
13+
CreateWaitpointRequestBody,
14+
CreateWaitpointResponseBody,
1315
DeletedScheduleObject,
1416
EnvironmentVariableResponseBody,
1517
EnvironmentVariableValue,
@@ -635,6 +637,19 @@ export class ApiClient {
635637
);
636638
}
637639

640+
createResumeToken(options: CreateWaitpointRequestBody, requestOptions?: ZodFetchOptions) {
641+
return zodfetch(
642+
CreateWaitpointResponseBody,
643+
`${this.baseUrl}/api/v1/waitpoints`,
644+
{
645+
method: "POST",
646+
headers: this.#getHeaders(false),
647+
body: JSON.stringify(options),
648+
},
649+
mergeRequestOptions(this.defaultRequestOptions, requestOptions)
650+
);
651+
}
652+
638653
subscribeToRun<TRunTypes extends AnyRunTypes>(
639654
runId: string,
640655
options?: {

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,20 +887,35 @@ export type SubscribeRealtimeStreamChunkRawShape = z.infer<
887887
typeof SubscribeRealtimeStreamChunkRawShape
888888
>;
889889

890+
export const TimePeriod = z.string().or(z.coerce.date());
891+
export type TimePeriod = z.infer<typeof TimePeriod>;
892+
890893
export const CreateWaitpointRequestBody = z.object({
891-
/** An optional idempotency key for the waitpoint. If you provide one. */
894+
/**
895+
* An optional idempotency key for the waitpoint.
896+
* If you use the same key twice (and the key hasn't expired), you will get the original waitpoint back.
897+
*
898+
* Note: This waitpoint may already be complete, in which case when you wait for it, it will immediately continue.
899+
*/
892900
idempotencyKey: z.string().optional(),
893-
/** The TTL for the idempotency key. */
901+
/**
902+
* When set, this means the passed in idempotency key will expire after this time.
903+
* This means after that time if you pass the same idempotency key again, you will get a new waitpoint.
904+
*/
894905
idempotencyKeyTTL: z.string().optional(),
895-
/** If the waitpoint hasn't been completed by this timeout, it will fail with a timeout error. */
896-
timeout: z.string().or(z.coerce.date()).optional(),
906+
/** The resume token will timeout after this time.
907+
* If you are waiting for the token in a run, the token will return a result where `ok` is false.
908+
*
909+
* You can pass a `Date` object, or a string in this format: "30s", "1m", "2h", "3d", "4w".
910+
*/
911+
timeout: TimePeriod.optional(),
897912
});
898913
export type CreateWaitpointRequestBody = z.infer<typeof CreateWaitpointRequestBody>;
899914

900-
export const CreateWaitpointResponse = z.object({
915+
export const CreateWaitpointResponseBody = z.object({
901916
id: z.string(),
902917
});
903-
export type CreateWaitpointResponse = z.infer<typeof CreateWaitpointResponse>;
918+
export type CreateWaitpointResponseBody = z.infer<typeof CreateWaitpointResponseBody>;
904919

905920
export const WAITPOINT_TIMEOUT_ERROR_CODE = "TRIGGER_WAITPOINT_TIMEOUT";
906921

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from "./tags.js";
1212
export * from "./metadata.js";
1313
export * from "./timeout.js";
1414
export * from "./waitUntil.js";
15+
export * from "./resumeTokens.js";
1516
export type { Context };
1617

1718
import type { Context } from "./shared.js";
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
apiClientManager,
3+
ApiPromise,
4+
ApiRequestOptions,
5+
CreateWaitpointRequestBody,
6+
CreateWaitpointResponseBody,
7+
mergeRequestOptions,
8+
} from "@trigger.dev/core/v3";
9+
import { tracer } from "./tracer.js";
10+
11+
function createResumeToken(
12+
options?: CreateWaitpointRequestBody,
13+
requestOptions?: ApiRequestOptions
14+
): ApiPromise<CreateWaitpointResponseBody> {
15+
const apiClient = apiClientManager.clientOrThrow();
16+
17+
const $requestOptions = mergeRequestOptions(
18+
{
19+
tracer,
20+
name: "resumeTokens.create()",
21+
icon: "wait",
22+
},
23+
requestOptions
24+
);
25+
26+
return apiClient.createResumeToken(options ?? {}, $requestOptions);
27+
}
28+
29+
export const resumeTokens = {
30+
create: createResumeToken,
31+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { logger, resumeTokens, task } from "@trigger.dev/sdk/v3";
2+
3+
export const resumeToken = task({
4+
id: "resume-token",
5+
run: async (payload: any, { ctx }) => {
6+
logger.log("Hello, world", { payload });
7+
8+
const token = await resumeTokens.create();
9+
logger.log("Token", token);
10+
},
11+
});

0 commit comments

Comments
 (0)