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
Copy file name to clipboardExpand all lines: docs/upgrade-to-v4.mdx
+101-2Lines changed: 101 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -170,6 +170,105 @@ tasks.onComplete(({ ctx, result }) => {
170
170
});
171
171
```
172
172
173
+
### onCancel
174
+
175
+
You can now define an `onCancel` hook that is called when a run is cancelled. This is useful if you want to clean up any resources that were allocated for the run.
176
+
177
+
```ts
178
+
tasks.onCancel(({ ctx, signal }) => {
179
+
console.log("Run cancelled", signal);
180
+
});
181
+
```
182
+
183
+
You can use the `onCancel` hook along with the `signal` passed into the run function to interrupt a call to an external service, for example using the [streamText](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) function from the AI SDK:
// We pass the signal to setTimeout to abort the timeout if the task is cancelled
249
+
awaitsetTimeout(10_000, undefined, { signal });
250
+
} catch (error) {
251
+
// Ignore the abort error
252
+
}
253
+
254
+
// Do some more work here
255
+
256
+
return {
257
+
message: "Hello, world!",
258
+
};
259
+
},
260
+
onCancel: async ({ runPromise }) => {
261
+
// You can await the runPromise to get the output of the task
262
+
const output =awaitrunPromise;
263
+
},
264
+
});
265
+
```
266
+
267
+
<Note>
268
+
You will have up to 30 seconds to complete the `runPromise` in the `onCancel` hook. After that
269
+
point the process will be killed.
270
+
</Note>
271
+
173
272
### Improved middleware and locals
174
273
175
274
Our task middleware system is now much more useful. Previously it only ran "around" the `run` function, but now we've hoisted it to the top level and it now runs before/after all the other hooks.
@@ -704,7 +803,7 @@ export const myTask = task({
704
803
id: "my-task",
705
804
onStart: ({ payload, ctx }) => {},
706
805
// The run function still uses separate parameters
0 commit comments