Skip to content

Commit af8ddaf

Browse files
committed
feat: add environment variable to disable auto-respawn of Edge Workers
Implement a simple check in SupabasePlatformAdapter to prevent spawning new edge functions during shutdown when EDGE_WORKER_DISABLE_AUTO_RESPAWN is set to true. Include documentation explaining how to disable auto-respawn via environment variables and outline use cases. Also, add a new test file to verify the environment variable behavior. This minimal change provides temporary control over worker respawning with minimal code modifications and documentation updates.
1 parent ebfdee0 commit af8ddaf

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

pkgs/edge-worker/src/platform/SupabasePlatformAdapter.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface SupabaseEnv extends Record<string, string | undefined> {
2424
EDGE_WORKER_DB_URL: string;
2525
SB_EXECUTION_ID: string;
2626
EDGE_WORKER_LOG_LEVEL?: string;
27+
EDGE_WORKER_DISABLE_AUTO_RESPAWN?: string;
2728
}
2829

2930
/**
@@ -176,10 +177,21 @@ export class SupabasePlatformAdapter implements PlatformAdapter<SupabaseResource
176177
globalThis.onbeforeunload = async () => {
177178
this.logger.debug('Shutting down...');
178179

179-
if (this.worker) {
180-
await this.spawnNewEdgeFunction();
180+
// Early return if no worker to respawn
181+
if (!this.worker) {
182+
await this.stopWorker();
183+
return;
184+
}
185+
186+
// Check if auto-respawn is disabled
187+
if (this.validatedEnv.EDGE_WORKER_DISABLE_AUTO_RESPAWN === 'true') {
188+
this.logger.debug('Auto-respawn disabled via EDGE_WORKER_DISABLE_AUTO_RESPAWN');
189+
await this.stopWorker();
190+
return;
181191
}
182192

193+
// Default behavior: spawn new function before stopping
194+
await this.spawnNewEdgeFunction();
183195
await this.stopWorker();
184196
};
185197
}

pkgs/website/src/content/docs/deploy/supabase/keep-workers-running.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Set up a pg_cron safety net that checks worker status and only starts new worker
1919

2020
Edge Workers normally auto-respawn by sending their own HTTP requests. In rare cases during high load, workers may fail to respawn if they hit execution time limits before sending restart requests. Basic safety nets can cause Supabase's Edge Runtime to spawn multiple workers simultaneously, leading to unpredictable costs.
2121

22+
<Aside type="tip">
23+
You can [disable auto-respawn](/how-to/disable-worker-auto-respawn/) if you want full control over worker lifecycle through pg_cron.
24+
</Aside>
25+
2226
## Smart Safety Net Solution
2327

2428
This approach checks existing worker counts before spawning new ones:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Disable Worker Auto-Respawn
3+
description: How to disable automatic worker respawning in Supabase Edge Functions
4+
sidebar:
5+
order: 116
6+
---
7+
8+
import { Aside } from "@astrojs/starlight/components";
9+
10+
<Aside type="caution" title="Temporary Feature">
11+
This is a temporary solution using environment variables. In future versions, this may be replaced with a proper configuration option.
12+
</Aside>
13+
14+
By default, pgflow Edge Workers automatically spawn a new instance when shutting down to ensure continuous processing. You can disable this behavior if you're using external orchestration like pg_cron.
15+
16+
## Disabling Auto-Respawn
17+
18+
### For Local Development
19+
20+
Add to your `supabase/functions/.env` file:
21+
22+
```diff
23+
# supabase/functions/.env
24+
EDGE_WORKER_DB_URL=postgres://...
25+
EDGE_WORKER_LOG_LEVEL=info
26+
+EDGE_WORKER_DISABLE_AUTO_RESPAWN=true
27+
```
28+
29+
### For Production (Supabase Dashboard)
30+
31+
1. Go to your project's Edge Functions settings
32+
2. Find your worker function
33+
3. Add the environment variable:
34+
- Key: `EDGE_WORKER_DISABLE_AUTO_RESPAWN`
35+
- Value: `true`
36+
37+
## When to Use This
38+
39+
Disable auto-respawn when:
40+
41+
- You're using pg_cron to schedule worker restarts
42+
- You want manual control over worker lifecycle
43+
- You're debugging shutdown behavior
44+
- You need to prevent duplicate workers
45+
46+
## Example with pg_cron
47+
48+
If you're using pg_cron to restart workers periodically:
49+
50+
```sql
51+
-- Schedule worker restart every hour
52+
SELECT cron.schedule(
53+
'restart-edge-worker',
54+
'0 * * * *', -- Every hour
55+
$$
56+
-- Your restart logic here
57+
$$
58+
);
59+
```
60+
61+
With auto-respawn disabled, only pg_cron controls when new workers start.
62+
63+
<Aside type="note">
64+
Without auto-respawn, ensure you have another mechanism (like pg_cron) to restart workers, otherwise processing will stop when the worker shuts down.
65+
</Aside>

0 commit comments

Comments
 (0)