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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ CREATE FUNCTION app_jobs.add_scheduled_job(
queue_name text DEFAULT NULL,
max_attempts integer DEFAULT 25,
priority integer DEFAULT 0,
entity_id uuid DEFAULT NULL
entity_id uuid DEFAULT NULL,
db_id uuid DEFAULT NULL
)
RETURNS app_jobs.scheduled_jobs
AS $$
Expand All @@ -24,7 +25,9 @@ DECLARE
v_database_id uuid;
v_actor_id uuid;
BEGIN
v_database_id := jwt_private.current_database_id();
-- Callers that run outside a JWT context (e.g. provisioning triggers) pass
-- db_id explicitly; everyone else keeps the JWT-derived default.
v_database_id := coalesce(db_id, jwt_private.current_database_id());
v_actor_id := jwt_public.current_user_id();

IF job_key IS NOT NULL THEN
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Deploy schemas/app_jobs/procedures/grants/grant_execute_add_scheduled_job_to_authenticated to pg

-- requires: schemas/app_jobs/schema
-- requires: schemas/app_jobs/procedures/add_scheduled_job

BEGIN;

GRANT EXECUTE ON FUNCTION app_jobs.add_scheduled_job(text, json, json, text, text, integer, integer, uuid, uuid) TO authenticated;

COMMIT;
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ BEGIN
* INTO j;
-- update the scheduled job
UPDATE
app_jobs.scheduled_jobs s
app_jobs.scheduled_jobs s
SET
last_scheduled = NOW(),
last_scheduled_id = j.id
last_scheduled_id = COALESCE(j.id, s.last_scheduled_id)
WHERE
s.id = run_scheduled_job.id;
RETURN j;
Expand All @@ -79,4 +79,3 @@ $$
LANGUAGE 'plpgsql'
VOLATILE;
COMMIT;

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- Deploy schemas/app_jobs/procedures/schedule_min_interval_seconds to pg

-- requires: schemas/app_jobs/schema

BEGIN;

-- Conservative lower-bound estimate (in seconds) of how often a
-- scheduled_jobs.schedule_info spec can fire. Used by generated schedule
-- limit triggers to enforce a minimum schedule interval cap.
--
-- Supports the node-schedule shapes stored in schedule_info:
-- { "rule": "*/5 * * * *" } 5-field cron (minute resolution)
-- { "rule": "0 */5 * * * *" } 6-field cron (second resolution)
-- { "minute": [...], "hour": [...] } recurrence-object form
--
-- Returns NULL when no lower bound can be derived (callers should treat
-- NULL as "unknown" and skip enforcement).
CREATE FUNCTION app_jobs.schedule_min_interval_seconds (schedule_info json)
RETURNS integer
AS $$
DECLARE
v_rule text;
v_fields text[];
v_seconds_field text;
v_minutes_field text;
v_step int;
BEGIN
IF schedule_info IS NULL THEN
RETURN NULL;
END IF;

v_rule := schedule_info ->> 'rule';

IF v_rule IS NULL THEN
-- Recurrence-object form: second resolution when a "second" key is
-- present, otherwise minute resolution.
IF schedule_info -> 'second' IS NOT NULL THEN
RETURN 1;
ELSIF schedule_info -> 'minute' IS NOT NULL
OR schedule_info -> 'hour' IS NOT NULL
OR schedule_info -> 'dayOfWeek' IS NOT NULL
OR schedule_info -> 'date' IS NOT NULL
OR schedule_info -> 'month' IS NOT NULL THEN
RETURN 60;
END IF;
RETURN NULL;
END IF;

v_fields := regexp_split_to_array(trim(v_rule), '\s+');

IF array_length(v_fields, 1) = 6 THEN
v_seconds_field := v_fields[1];
v_minutes_field := v_fields[2];
ELSIF array_length(v_fields, 1) = 5 THEN
v_seconds_field := '0';
v_minutes_field := v_fields[1];
ELSE
RETURN NULL;
END IF;

-- Second-resolution rules
IF v_seconds_field = '*' THEN
RETURN 1;
END IF;
IF v_seconds_field ~ '^\*/\d+$' THEN
v_step := substring(v_seconds_field FROM 3)::int;
RETURN GREATEST(v_step, 1);
END IF;
IF position(',' IN v_seconds_field) > 0 OR position('-' IN v_seconds_field) > 0 THEN
-- Multiple seconds within each matching minute: could fire every second
RETURN 1;
END IF;

-- Fixed second: at most once per matching minute
IF v_minutes_field = '*' THEN
RETURN 60;
END IF;
IF v_minutes_field ~ '^\*/\d+$' THEN
v_step := substring(v_minutes_field FROM 3)::int;
RETURN GREATEST(v_step, 1) * 60;
END IF;
IF position(',' IN v_minutes_field) > 0 OR position('-' IN v_minutes_field) > 0 THEN
-- Multiple minutes within each matching hour: adjacent values can be
-- one minute apart
RETURN 60;
END IF;

-- Fixed minute: at most once per hour
RETURN 3600;
END;
$$
LANGUAGE 'plpgsql'
IMMUTABLE;

COMMIT;
2 changes: 2 additions & 0 deletions packages/database-jobs/pgpm.plan
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ schemas/app_jobs/procedures/fail_job [schemas/app_jobs/schema schemas/app_jobs/t
schemas/app_jobs/procedures/complete_jobs [schemas/app_jobs/schema schemas/app_jobs/tables/job_queues/table schemas/app_jobs/tables/jobs/table] 2025-08-26T23:57:41Z pgpm <pgpm@5b0c196eeb62> # add schemas/app_jobs/procedures/complete_jobs
schemas/app_jobs/procedures/complete_job [schemas/app_jobs/schema schemas/app_jobs/tables/jobs/table schemas/app_jobs/tables/job_queues/table] 2025-08-26T23:57:41Z pgpm <pgpm@5b0c196eeb62> # add schemas/app_jobs/procedures/complete_job
schemas/app_jobs/procedures/add_scheduled_job [schemas/app_jobs/schema schemas/app_jobs/tables/scheduled_jobs/table pgpm-jwt-claims:schemas/jwt_private/procedures/current_database_id] 2025-08-26T23:57:41Z pgpm <pgpm@5b0c196eeb62> # add schemas/app_jobs/procedures/add_scheduled_job
schemas/app_jobs/procedures/grants/grant_execute_add_scheduled_job_to_authenticated [schemas/app_jobs/schema schemas/app_jobs/procedures/add_scheduled_job] 2026-07-15T06:07:00Z pgpm <pgpm@localhost> # grant authenticated EXECUTE on add_scheduled_job for INVOKER trigger support
schemas/app_jobs/procedures/schedule_min_interval_seconds [schemas/app_jobs/schema] 2026-07-12T13:30:00Z pgpm <pgpm@localhost> # add schedule_min_interval_seconds estimator for schedule interval caps
schemas/app_jobs/procedures/add_job [schemas/app_jobs/schema schemas/app_jobs/tables/jobs/table schemas/app_jobs/tables/job_queues/table pgpm-jwt-claims:schemas/jwt_private/procedures/current_database_id pgpm-jwt-claims:schemas/jwt_public/procedures/current_user_id] 2025-08-26T23:57:41Z pgpm <pgpm@5b0c196eeb62> # add schemas/app_jobs/procedures/add_job
schemas/app_jobs/procedures/grants/grant_execute_add_job_to_authenticated [schemas/app_jobs/schema schemas/app_jobs/procedures/add_job] 2026-06-03T01:15:00Z pgpm <pgpm@localhost> # grant authenticated EXECUTE on add_job for INVOKER trigger support
schemas/app_jobs/procedures/remove_job [schemas/app_jobs/schema schemas/app_jobs/tables/jobs/table] 2025-08-26T23:57:41Z pgpm <pgpm@5b0c196eeb62> # add schemas/app_jobs/procedures/remove_job
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/app_jobs/procedures/grants/grant_execute_add_scheduled_job_to_authenticated from pg

BEGIN;

REVOKE EXECUTE ON FUNCTION app_jobs.add_scheduled_job(text, json, json, text, text, integer, integer, uuid, uuid) FROM authenticated;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/app_jobs/procedures/schedule_min_interval_seconds from pg

BEGIN;

DROP FUNCTION app_jobs.schedule_min_interval_seconds;

COMMIT;
88 changes: 84 additions & 4 deletions packages/database-jobs/sql/pgpm-database-jobs--0.22.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ BEGIN
* INTO j;
-- update the scheduled job
UPDATE
app_jobs.scheduled_jobs s
app_jobs.scheduled_jobs s
SET
last_scheduled = NOW(),
last_scheduled_id = j.id
last_scheduled_id = COALESCE(j.id, s.last_scheduled_id)
WHERE
s.id = run_scheduled_job.id;
RETURN j;
Expand Down Expand Up @@ -735,14 +735,17 @@ CREATE FUNCTION app_jobs.add_scheduled_job(
queue_name text DEFAULT NULL,
max_attempts int DEFAULT 25,
priority int DEFAULT 0,
entity_id uuid DEFAULT NULL
entity_id uuid DEFAULT NULL,
db_id uuid DEFAULT NULL
) RETURNS app_jobs.scheduled_jobs AS $EOFCODE$
DECLARE
v_job app_jobs.scheduled_jobs;
v_database_id uuid;
v_actor_id uuid;
BEGIN
v_database_id := jwt_private.current_database_id();
-- Callers that run outside a JWT context (e.g. provisioning triggers) pass
-- db_id explicitly; everyone else keeps the JWT-derived default.
v_database_id := coalesce(db_id, jwt_private.current_database_id());
v_actor_id := jwt_public.current_user_id();

IF job_key IS NOT NULL THEN
Expand Down Expand Up @@ -824,6 +827,83 @@ BEGIN
END;
$EOFCODE$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER;

GRANT EXECUTE ON FUNCTION app_jobs.add_scheduled_job(text, pg_catalog.json, pg_catalog.json, text, text, int, int, uuid, uuid) TO authenticated;

CREATE FUNCTION app_jobs.schedule_min_interval_seconds(
schedule_info pg_catalog.json
) RETURNS int AS $EOFCODE$
DECLARE
v_rule text;
v_fields text[];
v_seconds_field text;
v_minutes_field text;
v_step int;
BEGIN
IF schedule_info IS NULL THEN
RETURN NULL;
END IF;

v_rule := schedule_info ->> 'rule';

IF v_rule IS NULL THEN
-- Recurrence-object form: second resolution when a "second" key is
-- present, otherwise minute resolution.
IF schedule_info -> 'second' IS NOT NULL THEN
RETURN 1;
ELSIF schedule_info -> 'minute' IS NOT NULL
OR schedule_info -> 'hour' IS NOT NULL
OR schedule_info -> 'dayOfWeek' IS NOT NULL
OR schedule_info -> 'date' IS NOT NULL
OR schedule_info -> 'month' IS NOT NULL THEN
RETURN 60;
END IF;
RETURN NULL;
END IF;

v_fields := regexp_split_to_array(trim(v_rule), '\s+');

IF array_length(v_fields, 1) = 6 THEN
v_seconds_field := v_fields[1];
v_minutes_field := v_fields[2];
ELSIF array_length(v_fields, 1) = 5 THEN
v_seconds_field := '0';
v_minutes_field := v_fields[1];
ELSE
RETURN NULL;
END IF;

-- Second-resolution rules
IF v_seconds_field = '*' THEN
RETURN 1;
END IF;
IF v_seconds_field ~ '^\*/\d+$' THEN
v_step := substring(v_seconds_field FROM 3)::int;
RETURN GREATEST(v_step, 1);
END IF;
IF position(',' IN v_seconds_field) > 0 OR position('-' IN v_seconds_field) > 0 THEN
-- Multiple seconds within each matching minute: could fire every second
RETURN 1;
END IF;

-- Fixed second: at most once per matching minute
IF v_minutes_field = '*' THEN
RETURN 60;
END IF;
IF v_minutes_field ~ '^\*/\d+$' THEN
v_step := substring(v_minutes_field FROM 3)::int;
RETURN GREATEST(v_step, 1) * 60;
END IF;
IF position(',' IN v_minutes_field) > 0 OR position('-' IN v_minutes_field) > 0 THEN
-- Multiple minutes within each matching hour: adjacent values can be
-- one minute apart
RETURN 60;
END IF;

-- Fixed minute: at most once per hour
RETURN 3600;
END;
$EOFCODE$ LANGUAGE plpgsql IMMUTABLE;

CREATE FUNCTION app_jobs.add_job(
identifier text,
payload pg_catalog.json DEFAULT '{}'::json,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify schemas/app_jobs/procedures/grants/grant_execute_add_scheduled_job_to_authenticated on pg

BEGIN;

SELECT has_function_privilege('authenticated', 'app_jobs.add_scheduled_job(text, json, json, text, text, integer, integer, uuid, uuid)', 'EXECUTE');

ROLLBACK;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify schemas/app_jobs/procedures/schedule_min_interval_seconds on pg

BEGIN;

SELECT verify_function ('app_jobs.schedule_min_interval_seconds');

ROLLBACK;
Loading
Loading