-
Notifications
You must be signed in to change notification settings - Fork 4
feat: remove pg_cron dependency and related scheduling logic for improved flexibility #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,6 @@ | |
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
| REFRESH_FUNCTION_NAME = "refresh_pygeoapi_materialized_views" | ||
| REFRESH_JOB_NAME = "refresh_pygeoapi_matviews_nightly" | ||
| REFRESH_SCHEDULE = "0 3 * * *" | ||
|
|
||
| THING_COLLECTIONS = [ | ||
| ("water_wells", "water well"), | ||
|
|
@@ -231,68 +229,6 @@ def _create_refresh_function() -> str: | |
| """ | ||
|
|
||
|
|
||
| def _schedule_refresh_job() -> str: | ||
| return f""" | ||
| DO $do$ | ||
| BEGIN | ||
| 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 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.', | ||
| '{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$; | ||
| """ | ||
|
|
||
|
|
||
| def _unschedule_refresh_job() -> str: | ||
| return f""" | ||
| DO $do$ | ||
| BEGIN | ||
| BEGIN | ||
| PERFORM cron.unschedule('{REFRESH_JOB_NAME}'); | ||
| EXCEPTION | ||
| WHEN undefined_function THEN | ||
| 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.', | ||
| '{REFRESH_JOB_NAME}'; | ||
| END; | ||
| END | ||
| $do$; | ||
| """ | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| bind = op.get_bind() | ||
| inspector = inspect(bind) | ||
|
|
@@ -307,16 +243,6 @@ def upgrade() -> None: | |
| f"tables are missing: {missing_tables_str}" | ||
| ) | ||
|
|
||
| pg_cron_available = bind.execute( | ||
| text( | ||
| "SELECT EXISTS (" | ||
| "SELECT 1 FROM pg_available_extensions WHERE name = 'pg_cron'" | ||
| ")" | ||
| ) | ||
| ).scalar() | ||
| if pg_cron_available: | ||
| op.execute(text("CREATE EXTENSION IF NOT EXISTS pg_cron")) | ||
|
|
||
| for view_id, thing_type in THING_COLLECTIONS: | ||
| safe_view_id = _safe_view_id(view_id) | ||
| op.execute(text(f"DROP VIEW IF EXISTS ogc_{safe_view_id}")) | ||
|
|
@@ -360,21 +286,9 @@ def upgrade() -> None: | |
| _create_matview_indexes() | ||
|
|
||
| op.execute(text(_create_refresh_function())) | ||
| if pg_cron_available: | ||
| op.execute(text(_schedule_refresh_job())) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| bind = op.get_bind() | ||
| pg_cron_available = bind.execute( | ||
| text( | ||
| "SELECT EXISTS (" | ||
| "SELECT 1 FROM pg_available_extensions WHERE name = 'pg_cron'" | ||
| ")" | ||
| ) | ||
| ).scalar() | ||
| if pg_cron_available: | ||
| op.execute(text(_unschedule_refresh_job())) | ||
| op.execute(text(f"DROP FUNCTION IF EXISTS public.{REFRESH_FUNCTION_NAME}()")) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The updated Useful? React with 👍 / 👎. |
||
| _drop_view_or_materialized_view("ogc_avg_tds_wells") | ||
| _drop_view_or_materialized_view("ogc_latest_depth_to_water_wells") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1 @@ | ||
| FROM postgis/postgis:17-3.5 | ||
|
|
||
| RUN apt-get update \ | ||
| && apt-get install -y --no-install-recommends postgresql-17-cron \ | ||
| && rm -rf /var/lib/apt/lists/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This migration previously scheduled a pg_cron job. Editing an existing Alembic revision to remove that logic won’t clean up pg_cron jobs/extensions already present in databases where this revision has been applied, and it changes downgrade behavior for this revision. Prefer adding a new migration that (best-effort) unschedules the old job and drops
pg_cronif present, handling privilege errors safely.