Skip to content

Commit

Permalink
feat: add decorator to run functions only in prod (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Oct 9, 2023
1 parent cb6f2f9 commit 837e86b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions basedosdados_api/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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"))
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()
10 changes: 10 additions & 0 deletions basedosdados_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 837e86b

Please sign in to comment.