From 837e86b402fde106153b14f5b0fa6f2950ac277f Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Mon, 9 Oct 2023 17:21:00 -0300 Subject: [PATCH] feat: add decorator to run functions only in prod (#436) --- basedosdados_api/api/v1/tasks.py | 2 ++ basedosdados_api/utils.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/basedosdados_api/api/v1/tasks.py b/basedosdados_api/api/v1/tasks.py index 6ab33ce0..aa244d48 100644 --- a/basedosdados_api/api/v1/tasks.py +++ b/basedosdados_api/api/v1/tasks.py @@ -4,6 +4,7 @@ from loguru import logger from basedosdados_api.api.v1.admin import update_table_metadata +from basedosdados_api.utils import prod_task @periodic_task(crontab(minute="*/10")) @@ -11,6 +12,7 @@ def every_ten_mins_task(): logger.info("Am I alive between these periods?") +@prod_task @periodic_task(crontab(day_of_week="0", hour="0", minute="0")) def update_table_metadata_task(): update_table_metadata() diff --git a/basedosdados_api/utils.py b/basedosdados_api/utils.py index 978aea72..eda3c76c 100644 --- a/basedosdados_api/utils.py +++ b/basedosdados_api/utils.py @@ -47,3 +47,13 @@ def is_prod(): if is_remote() and not is_dev() and not is_stag(): return True return False + + +def prod_task(func): + """Decorator that avoids function call if it isn't production""" + + def wrapper(*args, **kwargs): + if is_prod(): + return func(*args, **kwargs) + + return wrapper