Skip to content

Commit af3548f

Browse files
laktekcharislam
andauthored
Update background tasks guide (supabase#30814)
* fix wording * chore: update background tasks guide * Apply suggestions from code review Co-authored-by: Charis <[email protected]> --------- Co-authored-by: Charis <[email protected]>
1 parent 36b6d85 commit af3548f

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

apps/docs/content/guides/functions/background-tasks.mdx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,59 @@ id: 'function-background-tasks'
33
title: 'Background Tasks'
44
description: 'How to run background tasks in an Edge Function outside of the request handler'
55
subtitle: 'How to run background tasks in an Edge Function outside of the request handler'
6-
tocVideo: 'rSKBTJxG9VA'
76
---
87

98
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.
109

11-
### How long a background task can run
10+
### How it works
1211

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.
1417

1518
### Example
1619

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.
1821

1922
```ts
20-
// Define a custom event type for the background task.
21-
class MyBackgroundTaskEvent extends Event {
22-
readonly taskPromise: Promise<Response>
23-
24-
constructor(taskPromise: Promise<Response>) {
25-
super('myBackgroundTask')
26-
this.taskPromise = taskPromise
27-
}
23+
async function longRunningTask() {
24+
// do work here
2825
}
2926

30-
globalThis.addEventListener('myBackgroundTask', async (event) => {
31-
const res = await (event as MyBackgroundTaskEvent).taskPromise
32-
console.log(await res.json())
27+
// Mark the longRunningTask's returned promise as a background task.
28+
// note: we are not using await because we don't want it to block.
29+
EdgeRuntime.waitUntil(longRunningTask())
30+
31+
// Use beforeunload event handler to be notified when function is about to shutdown
32+
addEventListener('beforeunload', (ev) => {
33+
console.log('Function will be shutdown due to', ev.detail?.reason)
34+
35+
// save state or log the current progress
36+
})
37+
38+
// Invoke the function using a HTTP request.
39+
// This will start the background task
40+
Deno.serve(async (req) => {
41+
return new Response('ok')
3342
})
43+
```
44+
45+
### Starting a background task in the request handler
46+
47+
You can call `EdgeRuntime.waitUntil` in the request handler too. This will not block the request.
48+
49+
```ts
50+
async function fetchAndLog(url: string) {
51+
const response = await fetch('https://httpbin.org/json')
52+
console.log(response)
53+
}
3454

3555
Deno.serve(async (req) => {
36-
const fetchPromise = fetch('https://httpbin.org/json')
37-
const event = new MyBackgroundTaskEvent(fetchPromise)
38-
globalThis.dispatchEvent(event)
56+
// this will not block the request,
57+
// instead it will run in the background
58+
EdgeRuntime.waitUntil(fetchAndLog('https://httpbin.org/json'))
3959

4060
return new Response('ok')
4161
})

apps/docs/content/guides/functions/ephemeral-storage.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ You can use [Deno File System APIs](https://docs.deno.com/api/deno/file-system)
2424

2525
### Example
2626

27-
Here is an example of how to write a user-uploaded zip file into a temporary file for further processing.
27+
Here is an example of how to write a user-uploaded zip file into temporary storage for further processing.
2828

2929
```js
3030
Deno.serve(async (req) => {

0 commit comments

Comments
 (0)