Skip to content

Commit

Permalink
chore: incorporate main decorator in production decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Feb 4, 2024
1 parent 89fa238 commit f99433a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
37 changes: 17 additions & 20 deletions bd_api/apps/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,33 @@

from bd_api.apps.api.v1.models import Table
from bd_api.custom.client import get_gbq_client, get_gcs_client, send_discord_message
from bd_api.utils import main_task, production_task
from bd_api.utils import production_task

logger = logger.bind(module="api.v1")

header = (
"Verifique a atualização de metadados "
"via [grafana](https://grafana.basedosdados.org/d/_Lq-p0DIk/metadados-de-tabelas?orgId=1) dos conjuntos:"
)

@periodic_task(crontab(day_of_week="1-6", hour="5", minute="0"))
@production_task
def update_search_index_task():
call_command("update_index", batchsize=100)


@periodic_task(crontab(day_of_week="0", hour="5", minute="0"))
@production_task
def rebuild_search_index_task():
call_command("rebuild_index", interactive=False, batchsize=100)


@periodic_task(crontab(day_of_week="0", hour="3", minute="0"))
@main_task
@production_task
def update_table_metadata_task(table_pks: list[str] = None):
"""Update the metadata of selected tables in the database"""

header = (
"Verifique a atualização de metadados "
"via [grafana](https://grafana.basedosdados.org/d/_Lq-p0DIk/metadados-de-tabelas?orgId=1) dos conjuntos:"
)

def get_number_of_rows(table: Table, bq_table: GBQTable) -> int | None:
"""Get number of rows from big query"""

Expand Down Expand Up @@ -110,17 +121,3 @@ def format_msg(msg: list[str]) -> str:

if msg := format_msg(msg):
send_discord_message(msg)


@periodic_task(crontab(day_of_week="1-6", hour="5", minute="0"))
@main_task
@production_task
def update_search_index_task():
call_command("update_index", batchsize=100)


@periodic_task(crontab(day_of_week="0", hour="5", minute="0"))
@main_task
@production_task
def rebuild_search_index_task():
call_command("rebuild_index", interactive=False, batchsize=100)
17 changes: 4 additions & 13 deletions bd_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,14 @@ def get_frontend_url():
return "localhost:3000"


def main_task(func):
"""Decorator that avoids function call if it isn't main"""

@wraps(func)
def wrapper(*args, **kwargs):
if is_main():
return func(*args, **kwargs)

return wrapper


def production_task(func):
"""Decorator that avoids function call if it isn't production"""
"""Decorator that avoids function call if it isn't production.
Run the task only in workers flagged as main
"""

@wraps(func)
def wrapper(*args, **kwargs):
if is_prd():
if is_prd() and is_main():
return func(*args, **kwargs)

return wrapper

0 comments on commit f99433a

Please sign in to comment.