You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`yourTask.trigger()`| Anywhere | Triggers a task and gets a handle you can use to monitor and manage the run. It does not wait for the result. |
11
11
|`yourTask.batchTrigger()`| Anywhere | Triggers a task multiple times and gets a handle you can use to monitor and manage the runs. It does not wait for the results. |
12
12
|`yourTask.triggerAndWait()`| Inside a task | Triggers a task and then waits until it's complete. You get the result data to continue with. |
13
13
|`yourTask.batchTriggerAndWait()`| Inside a task | Triggers a task multiple times in parallel and then waits until they're all complete. You get the resulting data to continue with. |
14
+
|`tasks.trigger()`| Outside of a task | Triggers a task and gets a handle you can use to fetch and manage the run. |
15
+
|`tasks.batchTrigger()`| Outside of a task | Triggers a task multiple times and gets a handle you can use to fetch and manage the runs. |
14
16
15
17
Additionally, [scheduled tasks](/v3/tasks-scheduled) get automatically triggered on their schedule and [webhooks](/v3/tasks-webhooks) when receiving a webhook.
16
18
17
19
## Scheduled tasks
18
20
19
21
You should attach one or more schedules to your `schedules.task()` to trigger it on a recurring schedule. [Read the scheduled tasks docs](/v3/tasks-scheduled).
20
22
21
-
## From outside of a task
23
+
## Authentication
22
24
23
-
You can trigger any task from your backend code, using either `trigger()` or `batchTrigger()`.
24
-
25
-
<Note>
26
-
Do not trigger tasks directly from your frontend. If you do, you will leak your private
27
-
Trigger.dev API key to the world.
28
-
</Note>
29
-
30
-
You can use Next.js Server Actions but [you need to be careful with bundling](#next-js-server-actions).
25
+
When you trigger a task from your backend code, you need to set the `TRIGGER_SECRET_KEY` environment variable. You can find the value on the API keys page in the Trigger.dev dashboard. [More info on API keys](/v3/apikeys).
31
26
32
-
### Authentication
27
+
##Task instance methods
33
28
34
-
When you trigger a task from your backend code, you need to set the `TRIGGER_SECRET_KEY` environment variable. You can find the value on the API keys page in the Trigger.dev dashboard. [More info on API keys](/v3/apikeys).
29
+
Task instance methods are available on the `Task` object you receive when you define a task. They can be called from your backend code or from inside another task.
35
30
36
-
### trigger()
31
+
### Task.trigger()
37
32
38
33
Triggers a single run of a task with the payload you pass in, and any options you specify. It does NOT wait for the result, you cannot do that from outside a task.
39
34
35
+
If called from within a task, you can use the `AndWait` version to pause execution until the triggered run is complete.
Triggers multiples runs of a task with the payloads you pass in, and any options you specify. It does NOT wait for the results, you cannot do that from outside a task.
91
+
Triggers multiples runs of a task with the payloads you pass in, and any options you specify.
You can trigger tasks from other tasks using `trigger()` or `batchTrigger()`. You can also trigger and wait for the result of triggered tasks using `triggerAndWait()` and `batchTriggerAndWait()`. This is a powerful way to build complex tasks.
129
-
130
-
### trigger()
131
-
132
-
This works the same as from outside a task. You call it and you get a handle back, but it does not wait for the result.
This is where it gets interesting. You can trigger a task and then wait for the result. This is useful when you need to call a different task and then use the result to continue with your task.
You can batch trigger a task and wait for all the results. This is useful for the fan-out pattern, where you need to call a task multiple times and then wait for all the results to continue with your task.
// run.output will be correctly typed as the return value of the task
468
+
returnResponse.json(run.output);
469
+
}
470
+
```
471
+
472
+
</CodeGroup>
473
+
287
474
## Next.js Server Actions
288
475
289
476
Server Actions allow you to call your backend code without creating API routes. This is very useful for triggering tasks but you need to be careful you don't accidentally bundle the Trigger.dev SDK into your frontend code.
290
477
291
-
If you see an error like this then you've bundled `@trigger.dev/sdk` into your frontend code:
478
+
If you see an error like this then you've bundled `@trigger.dev/sdk/v3` into your frontend code:
292
479
293
480
```bash
294
481
Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
@@ -297,7 +484,7 @@ Webpack supports "data:" and "file:" URIs by default.
297
484
You may need an additional plugin to handle "node:" URIs.
298
485
```
299
486
300
-
When you use server actions that use `@trigger.dev/sdk`:
487
+
When you use server actions that use `@trigger.dev/sdk/v3`:
301
488
302
489
- The file can't have any React components in it.
303
490
- The file should have `"use server"` on the first line.
0 commit comments