|
1 | 1 | --- |
2 | 2 | title: "Resumability" |
3 | 3 | sidebarTitle: "Keys and Resumability" |
4 | | -description: "Keys and Resumability" |
| 4 | +description: "Understand how keys work to ensure resumability of long-running workflows in Trigger.dev" |
5 | 5 | --- |
6 | 6 |
|
7 | | -## Coming soon |
| 7 | +## Intro |
| 8 | + |
| 9 | +In this article, we'll cover how Trigger.dev handles resumability of long-running workflows. We'll also cover how to use keys to ensure that your workflows are resumable. |
| 10 | + |
| 11 | +## What is resumability? |
| 12 | + |
| 13 | +Resumability is the ability of a workflow run to continue from where it left off after a failure or interruption. For example, if a workflow run is interrupted due to a network failure, it should be able to resume from where it left off when the network is back up. |
| 14 | + |
| 15 | +We accomplish this by storing the state of the workflow run in a database, and when a workflow run is resumed we will call the `Trigger.run` function again. |
| 16 | + |
| 17 | +We use step "keys" to determine which steps have already been executed. If a step has already been executed, we will skip it and move on to the next step. |
| 18 | + |
| 19 | +## How to use keys |
| 20 | + |
| 21 | +Like we mentioned above, we use step keys to determine which steps have already been executed. They are defined by you inside your `Trigger.run` function, for example when you call `slack.postMessage`: |
| 22 | + |
| 23 | +```ts |
| 24 | +await slack.postMessage("⭐️ New Star", { |
| 25 | + channelName: "github-stars", |
| 26 | + text: `@${starredBy} just starred ${repoName}!`, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +In this example, the key is the string `"⭐️ New Star"`. This means that if the workflow is interrupted and then resumed, the `slack.postMessage` step will be skipped because it has already been executed. |
| 31 | + |
| 32 | +If you make multiple calls to `slack.postMessage`, you should use different keys for each call. For example: |
| 33 | + |
| 34 | +```ts |
| 35 | +await slack.postMessage("⭐️ New Star", { |
| 36 | + channelName: "github-stars", |
| 37 | + text: `@${starredBy} just starred ${repoName}!`, |
| 38 | +}); |
| 39 | + |
| 40 | +await slack.postMessage("🚨 Critical Issue", { |
| 41 | + channelName: "critical-issues", |
| 42 | + text: `@${assignee} just opened a critical issue in ${repoName}!`, |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +If you are calling a step multiple times with the same key, it will only be executed once. For example, if you call `slack.postMessage` with the key `"⭐️ New Star"` twice, it will only be executed once. |
| 47 | + |
| 48 | +## How to use keys with loops |
| 49 | + |
| 50 | +If you are using a loop, you should use the loop index as the key. For example: |
| 51 | + |
| 52 | +```ts |
| 53 | +for (let i = 0; i < 10; i++) { |
| 54 | + await ctx.waitFor(`Wait ${i}`, { seconds: 30 }); |
| 55 | + |
| 56 | + await slack.postMessage(`⭐️ New Star ${i}`, { |
| 57 | + channelName: "github-stars", |
| 58 | + text: `@${starredBy} just starred ${repoName}!`, |
| 59 | + }); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## When to use keys |
| 64 | + |
| 65 | +The following functions in Trigger.dev require a `key` parameter: |
| 66 | + |
| 67 | +- Integration actions (e.g. `slack.postMessage`) |
| 68 | +- [delays](/functions/delays) |
| 69 | +- [sendEvent](/functions/send-event) |
| 70 | +- [fetch](/functions/fetch) |
| 71 | + |
| 72 | +[Logging](/functions/logging) does not require a key, as we are currently using the log message as the key. |
0 commit comments