Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkgs/core/schemas/0058_function_mark_worker_stopped.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Mark Worker Stopped
-- Sets stopped_at timestamp on a worker row for graceful shutdown signaling

drop function if exists pgflow.mark_worker_stopped(uuid);

create or replace function pgflow.mark_worker_stopped(
worker_id uuid
) returns void
language sql
as $$
update pgflow.workers
set stopped_at = clock_timestamp()
where workers.worker_id = mark_worker_stopped.worker_id;
$$;

comment on function pgflow.mark_worker_stopped(uuid) is
'Marks a worker as stopped for graceful shutdown. Called by workers on beforeunload.';
1 change: 1 addition & 0 deletions pkgs/core/src/database-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export type Database = {
get_run_with_states: { Args: { run_id: string }; Returns: Json }
is_local: { Args: never; Returns: boolean }
is_valid_slug: { Args: { slug: string }; Returns: boolean }
mark_worker_stopped: { Args: { worker_id: string }; Returns: undefined }
maybe_complete_run: { Args: { run_id: string }; Returns: undefined }
poll_for_tasks: {
Args: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Create "mark_worker_stopped" function
CREATE FUNCTION "pgflow"."mark_worker_stopped" ("worker_id" uuid) RETURNS void LANGUAGE sql AS $$
update pgflow.workers
set stopped_at = clock_timestamp()
where workers.worker_id = mark_worker_stopped.worker_id;
$$;
-- Set comment to function: "mark_worker_stopped"
COMMENT ON FUNCTION "pgflow"."mark_worker_stopped" IS 'Marks a worker as stopped for graceful shutdown. Called by workers on beforeunload.';
3 changes: 2 additions & 1 deletion pkgs/core/supabase/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h1:Uh2fD3kgXbd4Emzw0ZL3tDr0KKpzWSFzI0tkl0XnytM=
h1:T5v0ssUXOiDF+iJT/GP9XN0ceXsT+9PnvHvkkRT3NT4=
20250429164909_pgflow_initial.sql h1:I3n/tQIg5Q5nLg7RDoU3BzqHvFVjmumQxVNbXTPG15s=
20250517072017_pgflow_fix_poll_for_tasks_to_use_separate_statement_for_polling.sql h1:wTuXuwMxVniCr3ONCpodpVWJcHktoQZIbqMZ3sUHKMY=
20250609105135_pgflow_add_start_tasks_and_started_status.sql h1:ggGanW4Wyt8Kv6TWjnZ00/qVb3sm+/eFVDjGfT8qyPg=
Expand All @@ -16,3 +16,4 @@ h1:Uh2fD3kgXbd4Emzw0ZL3tDr0KKpzWSFzI0tkl0XnytM=
20251204142050_pgflow_temp_ensure_flow_compiled_auto_detect.sql h1:9FsEd0iyaIv9X1alACBQCzyebt2/+m1rgL4ozXJWBcA=
20251204145037_pgflow_temp_worker_functions_schema.sql h1:mrLKtkM8aWHBY+LIxQrsXxE7aKuPJaQJO2qd0NnrJ74=
20251204164612_pgflow_temp_track_worker_function.sql h1:3Ht8wUx3saKPo98osuTG/nxD/tKR48qe8jHNVwuu2lY=
20251204165231_pgflow_temp_mark_worker_stopped.sql h1:zI2FijK429oae4OpLbjU4eSaZtYhlsbvN3buWH9FKLw=
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Test: mark_worker_stopped() sets stopped_at on the worker row
begin;
select plan(3);

-- Setup: Create a worker entry
insert into pgflow.workers (worker_id, queue_name, function_name, started_at, last_heartbeat_at)
values ('11111111-1111-1111-1111-111111111111', 'test_queue', 'test_function', now(), now());

-- Verify worker exists and stopped_at is NULL
select is(
(select stopped_at from pgflow.workers where worker_id = '11111111-1111-1111-1111-111111111111'),
null,
'Worker should have NULL stopped_at initially'
);

-- Execute: Mark worker as stopped
select pgflow.mark_worker_stopped('11111111-1111-1111-1111-111111111111'::uuid);

-- Test: stopped_at should be set
select isnt(
(select stopped_at from pgflow.workers where worker_id = '11111111-1111-1111-1111-111111111111'),
null,
'Worker should have stopped_at set after marking stopped'
);

-- Test: stopped_at should be recent (within last second)
select ok(
(select stopped_at > clock_timestamp() - interval '1 second'
from pgflow.workers
where worker_id = '11111111-1111-1111-1111-111111111111'),
'stopped_at should be recent (within last second)'
);

select finish();
rollback;
Loading