Skip to content

Commit 90276ce

Browse files
authored
feat: add automatic worker restart via cron job and worker health tracking (#517)
# Add automatic worker restart system for edge functions This PR implements a comprehensive worker management system that automatically restarts edge functions when they stop running. Key features include: - Added `ensure_workers()` cron job that monitors and restarts edge functions - Created `worker_functions` table to track registered edge functions and their health status - Added `stopped_at` column to workers table for detecting graceful shutdowns - Integrated `trackWorkerFunction` and `markWorkerStopped` functions into edge worker lifecycle - Consolidated temporary migrations into a single migration file - Removed manual `drop function` statements from SQL schema files - Removed redundant test setup code as worker restart is now automatic The system works by having edge functions register themselves on startup, then monitoring their health via heartbeats. When a worker stops (either gracefully or by crashing), the cron job automatically restarts it. This improves reliability by ensuring workers stay running without manual intervention.
1 parent e4cd7bb commit 90276ce

20 files changed

+270
-302
lines changed

.changeset/worker-management.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@pgflow/core': minor
3+
'@pgflow/edge-worker': minor
4+
---
5+
6+
Add automatic worker restart via `ensure_workers()` cron job that keeps edge functions running. Add `worker_functions` table for tracking registered edge functions and their health status. Add `stopped_at` column to workers table for graceful shutdown detection. Integrate `trackWorkerFunction` and `markWorkerStopped` into edge worker lifecycle for automatic registration and shutdown signaling.

pkgs/core/schemas/0057_function_track_worker_function.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- Track Worker Function
22
-- Registers an edge function for monitoring by ensure_workers() cron
33

4-
drop function if exists pgflow.track_worker_function(text);
5-
64
create or replace function pgflow.track_worker_function(
75
function_name text
86
) returns void

pkgs/core/schemas/0058_function_mark_worker_stopped.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- Mark Worker Stopped
22
-- Sets stopped_at timestamp on a worker row for graceful shutdown signaling
33

4-
drop function if exists pgflow.mark_worker_stopped(uuid);
5-
64
create or replace function pgflow.mark_worker_stopped(
75
worker_id uuid
86
) returns void

pkgs/core/schemas/0059_function_ensure_workers.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
-- Returns which worker functions should be invoked, makes HTTP requests, and updates last_invoked_at
33
-- Called by cron job to keep workers running
44

5-
drop function if exists pgflow.ensure_workers();
6-
75
create or replace function pgflow.ensure_workers()
86
returns table (function_name text, invoked boolean, request_id bigint)
97
language sql

pkgs/core/schemas/0060_function_cleanup_ensure_workers_logs.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
-- Cleans up old cron job run details to prevent the table from growing indefinitely.
33
-- Note: net._http_response is automatically cleaned by pg_net (6 hour TTL), so we only clean cron logs.
44

5-
drop function if exists pgflow.cleanup_ensure_workers_logs(integer);
6-
75
create or replace function pgflow.cleanup_ensure_workers_logs(
86
retention_hours integer default 24
97
)

pkgs/core/schemas/0061_function_setup_ensure_workers_cron.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- Setup Ensure Workers Cron
22
-- Sets up cron jobs for worker management: ensure_workers (configurable interval) and cleanup_logs (hourly)
33

4-
drop function if exists pgflow.setup_ensure_workers_cron(text);
5-
64
create or replace function pgflow.setup_ensure_workers_cron(
75
cron_interval text default '1 second'
86
)

pkgs/core/supabase/migrations/20251204115929_pgflow_temp_is_local.sql

Lines changed: 0 additions & 8 deletions
This file was deleted.

pkgs/core/supabase/migrations/20251204142050_pgflow_temp_ensure_flow_compiled_auto_detect.sql

Lines changed: 0 additions & 53 deletions
This file was deleted.

pkgs/core/supabase/migrations/20251204145037_pgflow_temp_worker_functions_schema.sql

Lines changed: 0 additions & 22 deletions
This file was deleted.

pkgs/core/supabase/migrations/20251204164612_pgflow_temp_track_worker_function.sql

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)