Skip to content

Commit cf9d466

Browse files
authored
Basic JSDocs for the v3 SDK (#963)
* task(), retry and queue * Added machine JSDocs * Add JSdocs for the run function params
1 parent 7b3ffe0 commit cf9d466

File tree

4 files changed

+150
-4
lines changed

4 files changed

+150
-4
lines changed

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

+47-2
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,21 @@ export const RateLimitOptions = z.discriminatedUnion("type", [
157157
]);
158158

159159
export const RetryOptions = z.object({
160+
/** The number of attempts before giving up */
160161
maxAttempts: z.number().int().optional(),
162+
/** The exponential factor to use when calculating the next retry time.
163+
*
164+
* Each subsequent retry will be calculated as `previousTimeout * factor`
165+
*/
161166
factor: z.number().optional(),
167+
/** The minimum time to wait before retrying */
162168
minTimeoutInMs: z.number().int().optional(),
169+
/** The maximum time to wait before retrying */
163170
maxTimeoutInMs: z.number().int().optional(),
171+
/** Randomize the timeout between retries.
172+
*
173+
* This can be useful to prevent the thundering herd problem where all retries happen at the same time.
174+
*/
164175
randomize: z.boolean().optional(),
165176
});
166177

@@ -169,10 +180,44 @@ export type RetryOptions = z.infer<typeof RetryOptions>;
169180
export type RateLimitOptions = z.infer<typeof RateLimitOptions>;
170181

171182
export const QueueOptions = z.object({
183+
/** You can define a shared queue and then pass the name in to your task.
184+
*
185+
* @example
186+
*
187+
* ```ts
188+
* const myQueue = queue({
189+
name: "my-queue",
190+
concurrencyLimit: 1,
191+
});
192+
193+
export const task1 = task({
194+
id: "task-1",
195+
queue: {
196+
name: "my-queue",
197+
},
198+
run: async (payload: { message: string }) => {
199+
// ...
200+
},
201+
});
202+
203+
export const task2 = task({
204+
id: "task-2",
205+
queue: {
206+
name: "my-queue",
207+
},
208+
run: async (payload: { message: string }) => {
209+
// ...
210+
},
211+
});
212+
* ```
213+
*/
214+
name: z.string().optional(),
215+
/** An optional property that specifies the maximum number of concurrent run executions.
216+
*
217+
* If this property is omitted, the task can potentially use up the full concurrency of an environment. */
218+
concurrencyLimit: z.number().int().min(1).max(1000).optional(),
172219
/** @deprecated This feature is coming soon */
173220
rateLimit: RateLimitOptions.optional(),
174-
concurrencyLimit: z.number().int().min(1).max(1000).optional(),
175-
name: z.string().optional(),
176221
});
177222

178223
export type QueueOptions = z.infer<typeof QueueOptions>;

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

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export * from "./config";
77
export type InitOutput = Record<string, any> | void | undefined;
88

99
export type RunFnParams<TInitOutput extends InitOutput> = Prettify<{
10+
/** Metadata about the task, run, attempt, queue, environment, organization, project and batch. */
1011
ctx: Context;
12+
/** If you use the `init` function, this will be whatever you returned. */
1113
init?: TInitOutput;
1214
}>;
1315

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

+83-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,94 @@ export function queue(options: { name: string } & QueueOptions): Queue {
3838
}
3939

4040
export type TaskOptions<TPayload, TOutput = any, TInitOutput extends InitOutput = any> = {
41+
/** An id for your task. This must be unique inside your project and not change between versions. */
4142
id: string;
43+
/** The retry settings when an uncaught error is thrown.
44+
*
45+
* If omitted it will use the values in your `trigger.config.ts` file.
46+
*
47+
* @example
48+
*
49+
* ```
50+
* export const taskWithRetries = task({
51+
id: "task-with-retries",
52+
retry: {
53+
maxAttempts: 10,
54+
factor: 1.8,
55+
minTimeoutInMs: 500,
56+
maxTimeoutInMs: 30_000,
57+
randomize: false,
58+
},
59+
run: async ({ payload, ctx }) => {
60+
//...
61+
},
62+
});
63+
* ```
64+
* */
4265
retry?: RetryOptions;
66+
/** Used to configure what should happen when more than one run is triggered at the same time.
67+
*
68+
* @example
69+
* one at a time execution
70+
*
71+
* ```ts
72+
* export const oneAtATime = task({
73+
id: "one-at-a-time",
74+
queue: {
75+
concurrencyLimit: 1,
76+
},
77+
run: async ({ payload, ctx }) => {
78+
//...
79+
},
80+
});
81+
* ```
82+
*/
4383
queue?: QueueOptions;
84+
/** Configure the spec of the machine you want your task to run on.
85+
*
86+
* @example
87+
*
88+
* ```ts
89+
* export const heavyTask = task({
90+
id: "heavy-task",
91+
machine: {
92+
cpu: 2,
93+
memory: 4,
94+
},
95+
run: async ({ payload, ctx }) => {
96+
//...
97+
},
98+
});
99+
* ```
100+
*/
44101
machine?: {
45-
cpu?: number;
46-
memory?: number;
102+
/** vCPUs. The default is 0.5.
103+
*
104+
* Possible values:
105+
* - 0.25
106+
* - 0.5
107+
* - 1
108+
* - 2
109+
* - 4
110+
*/
111+
cpu?: 0.25 | 0.5 | 1 | 2 | 4;
112+
/** In GBs of RAM. The default is 0.5.
113+
*
114+
* Possible values:
115+
* - 0.25
116+
* - 0.5
117+
* - 1
118+
* - 2
119+
* - 4
120+
* - 8
121+
*/
122+
memory?: 0.25 | 0.5 | 1 | 2 | 4 | 8;
47123
};
124+
/** This gets called when a task is triggered. It's where you put the code you want to execute.
125+
*
126+
* @param payload - The payload that is passed to your task when it's triggered. This must be JSON serializable.
127+
* @param params - Metadata about the run.
128+
*/
48129
run: (payload: TPayload, params: RunFnParams<TInitOutput>) => Promise<TOutput>;
49130
init?: (payload: TPayload, params: InitFnParams) => Promise<TInitOutput>;
50131
handleError?: (

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

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
import { InitOutput } from "@trigger.dev/core/v3";
22
import { TaskOptions, Task, createTask } from "./shared";
33

4+
/** Creates a task that can be triggered
5+
* @param options - Task options
6+
* @example
7+
*
8+
* ```ts
9+
* import { task } from "@trigger.dev/sdk/v3";
10+
*
11+
* export const helloWorld = task({
12+
id: "hello-world",
13+
* run: async (payload: { url: string }) => {
14+
* return { hello: "world" };
15+
* },
16+
* });
17+
*
18+
* ```
19+
*
20+
* @returns A task that can be triggered
21+
*/
422
export function task<TInput, TOutput = any, TInitOutput extends InitOutput = any>(
523
options: TaskOptions<TInput, TOutput, TInitOutput>
624
): Task<TInput, TOutput> {

0 commit comments

Comments
 (0)