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: apps/docs/content/guides/functions/background-tasks.mdx
+38-18Lines changed: 38 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,39 +3,59 @@ id: 'function-background-tasks'
3
3
title: 'Background Tasks'
4
4
description: 'How to run background tasks in an Edge Function outside of the request handler'
5
5
subtitle: 'How to run background tasks in an Edge Function outside of the request handler'
6
-
tocVideo: 'rSKBTJxG9VA'
7
6
---
8
7
9
8
Edge Function instances can process background tasks outside of the request handler. Background tasks are useful for asynchronous operations like uploading a file to Storage, updating a database, or sending events to a logging service. You can respond to the request immediately and leave the task running in the background.
10
9
11
-
### How long a background task can run
10
+
### How it works
12
11
13
-
A background task can run until the Edge Function instance hits its wall-clock limit or reaches CPU/Memory limit. Check [limits](/docs/guides/functions/limits) for current caps.
12
+
You can use `EdgeRuntime.waitUntil(promise)` to explicitly mark background tasks. The Function instance continues to run until the promise provided to `waitUntil` completes.
13
+
14
+
The maximum duration is capped based on the wall-clock, CPU, and memory limits. The Function will shutdown when it reaches one of these [limits](/docs/guides/functions/limits).
15
+
16
+
You can listen to the `beforeunload` event handler to be notified when Function invocation is about to be shut down.
14
17
15
18
### Example
16
19
17
-
Here's an example of defining a background task using a custom event.
20
+
Here's an example of using `EdgeRuntime.waitUntil` to run a background task and using `beforeunload`event to be notified when the instance is about to be shut down.
18
21
19
22
```ts
20
-
// Define a custom event type for the background task.
0 commit comments