From afb8c8e784b04672d811d199b11c5b6748f095f7 Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Mon, 9 Oct 2023 16:44:33 -0300 Subject: [PATCH] feat: add decorator to run functions only in prod --- basedosdados_api/api/v1/tasks.py | 2 ++ basedosdados_api/utils.py | 8 ++++++++ 2 files changed, 10 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..210f3b60 100644 --- a/basedosdados_api/utils.py +++ b/basedosdados_api/utils.py @@ -47,3 +47,11 @@ 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