From 43852fefb058ffdf536efc0bc1f4d3063e612663 Mon Sep 17 00:00:00 2001 From: jakeross Date: Fri, 27 Feb 2026 08:23:10 -0700 Subject: [PATCH 1/2] feat: enhance refresh job scheduling with improved privilege handling and error management --- ...a8b9c0_create_pygeoapi_supporting_views.py | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py b/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py index afb70171..7ef17912 100644 --- a/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py +++ b/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py @@ -234,22 +234,33 @@ def _create_refresh_function() -> str: def _schedule_refresh_job() -> str: return f""" DO $do$ - DECLARE - existing_job_id bigint; BEGIN - SELECT jobid INTO existing_job_id - FROM cron.job - WHERE jobname = '{REFRESH_JOB_NAME}'; - - IF existing_job_id IS NOT NULL THEN - PERFORM cron.unschedule(existing_job_id); - END IF; + BEGIN + -- Avoid direct SELECT on cron.job because managed Postgres + -- environments may deny access to the cron schema table. + PERFORM cron.unschedule('{REFRESH_JOB_NAME}'); + EXCEPTION + WHEN undefined_function THEN + NULL; + WHEN invalid_parameter_value THEN + NULL; + WHEN insufficient_privilege THEN + RAISE NOTICE + 'Skipping pg_cron unschedule for % due to insufficient privileges.', + '{REFRESH_JOB_NAME}'; + RETURN; + END; PERFORM cron.schedule( '{REFRESH_JOB_NAME}', '{REFRESH_SCHEDULE}', $cmd$SELECT public.{REFRESH_FUNCTION_NAME}();$cmd$ ); + EXCEPTION + WHEN insufficient_privilege THEN + RAISE NOTICE + 'Skipping pg_cron schedule for % due to insufficient privileges.', + '{REFRESH_JOB_NAME}'; END $do$; """ @@ -258,20 +269,19 @@ def _schedule_refresh_job() -> str: def _unschedule_refresh_job() -> str: return f""" DO $do$ - DECLARE - existing_job_id bigint; BEGIN - IF to_regclass('cron.job') IS NULL THEN - RETURN; - END IF; - - SELECT jobid INTO existing_job_id - FROM cron.job - WHERE jobname = '{REFRESH_JOB_NAME}'; - - IF existing_job_id IS NOT NULL THEN - PERFORM cron.unschedule(existing_job_id); - END IF; + BEGIN + PERFORM cron.unschedule('{REFRESH_JOB_NAME}'); + EXCEPTION + WHEN undefined_function THEN + NULL; + WHEN invalid_parameter_value THEN + NULL; + WHEN insufficient_privilege THEN + RAISE NOTICE + 'Skipping pg_cron unschedule for % due to insufficient privileges.', + '{REFRESH_JOB_NAME}'; + END; END $do$; """ From cf51d489138f815b86b1650fee3e35fae6b6f9ac Mon Sep 17 00:00:00 2001 From: jakeross Date: Fri, 27 Feb 2026 08:25:45 -0700 Subject: [PATCH 2/2] feat: handle internal error in pg_cron job unscheduling for better robustness --- .../d5e6f7a8b9c0_create_pygeoapi_supporting_views.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py b/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py index 7ef17912..f68836e7 100644 --- a/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py +++ b/alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py @@ -244,6 +244,10 @@ def _schedule_refresh_job() -> str: NULL; WHEN invalid_parameter_value THEN NULL; + WHEN internal_error THEN + -- Some pg_cron builds raise internal_error when the named + -- job does not exist. Treat this as already-unscheduled. + NULL; WHEN insufficient_privilege THEN RAISE NOTICE 'Skipping pg_cron unschedule for % due to insufficient privileges.', @@ -277,6 +281,8 @@ def _unschedule_refresh_job() -> str: NULL; WHEN invalid_parameter_value THEN NULL; + WHEN internal_error THEN + NULL; WHEN insufficient_privilege THEN RAISE NOTICE 'Skipping pg_cron unschedule for % due to insufficient privileges.',