-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
backend/gn_module_zh/migrations/76e89c793961_create_vm_taxon_refresh_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""create_vm_taxon_refresh_function | ||
Revision ID: 76e89c793961 | ||
Revises: c0c4748a597a | ||
Create Date: 2024-04-16 08:12:41.346540 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '76e89c793961' | ||
down_revision = 'c0c4748a597a' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
""" | ||
CREATE OR REPLACE FUNCTION pr_zh.refresh_taxon_materialized_views() | ||
RETURNS void | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
REFRESH MATERIALIZED VIEW pr_zh.vm_vertebrates; | ||
REFRESH MATERIALIZED VIEW pr_zh.vm_invertebrates; | ||
REFRESH MATERIALIZED VIEW pr_zh.vm_flora; | ||
END; | ||
$function$ | ||
; | ||
""" | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.execute( | ||
""" | ||
DROP FUNCTION pr_zh.refresh_taxon_materialized_views(); | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
|
||
from datetime import datetime, timedelta | ||
from pathlib import Path | ||
|
||
from sqlalchemy import func | ||
from celery.utils.log import get_task_logger | ||
from celery.schedules import crontab | ||
|
||
from flask import current_app | ||
from geonature.utils.celery import celery_app | ||
|
||
from geonature.utils.env import db | ||
from geonature.utils.config import config | ||
|
||
logger = get_task_logger(__name__) | ||
|
||
|
||
@celery_app.on_after_finalize.connect | ||
def setup_periodic_tasks(sender, **kwargs): | ||
ct = config["ZONES_HUMIDES"]["TAXON_VM_CRONTAB"] | ||
minute, hour, day_of_month, month_of_year, day_of_week = ct.split(" ") | ||
sender.add_periodic_task( | ||
crontab( | ||
minute=minute, | ||
hour=hour, | ||
day_of_week=day_of_week, | ||
day_of_month=day_of_month, | ||
month_of_year=month_of_year, | ||
), | ||
refresh_taxon_vm.s(), | ||
name="Refresh taxon vms", | ||
) | ||
|
||
|
||
@celery_app.task(bind=True) | ||
def refresh_taxon_vm(self): | ||
logger.info("Refresh taxon vms...") | ||
db.session.execute(func.pr_zh.refresh_taxon_materialized_views()) | ||
db.session.commit() | ||
logger.info("Taxon vms refreshed.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters