From 2d2a54bda63a16bf9b0f5b5f46c9b983db6ad3cf Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 23 Jan 2025 10:12:17 -0800 Subject: [PATCH] change the redis limit tracker to annual --- app/celery/tasks.py | 7 ++++++ app/notifications/validators.py | 11 ++++++-- .../0414_change_total_message_limit.py | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/0414_change_total_message_limit.py diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 331d95364..6fea63e1b 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -159,7 +159,14 @@ def process_row(row, template, job, service, sender_id=None): return notification_id +# TODO +# Originally this was checking a daily limit +# It is now checking an overall limit (annual?) for the free tier +# Is there any limit for the paid tier? +# Assuming the limit is annual, is it calendar year, fiscal year, MOU year? +# Do we need a command to run to clear the redis value, or should it happen automatically? def __total_sending_limits_for_job_exceeded(service, job, job_id): + try: total_sent = check_service_over_total_message_limit(KeyType.NORMAL, service) if total_sent + job.notification_count > service.total_message_limit: diff --git a/app/notifications/validators.py b/app/notifications/validators.py index f0a7f2a8f..48d35dfe9 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -45,10 +45,17 @@ def check_service_over_total_message_limit(key_type, service): cache_key = total_limit_cache_key(service.id) service_stats = redis_store.get(cache_key) + + ## Originally this was a daily limit check. It is now a free-tier limit check. + ## TODO is this annual or forever for each service? + ## TODO do we need a way to clear this out? How do we determine if it is + ## free-tier or paid? What are the limits for paid? Etc. + ## TODO + ## setting expiration to one year for now on the assume that the free tier + ## limit resets annually. if service_stats is None: - # first message of the day, set the cache to 0 and the expiry to 24 hours service_stats = 0 - redis_store.set(cache_key, service_stats, ex=86400) + redis_store.set(cache_key, service_stats, ex=365*24*60*60) return service_stats if int(service_stats) >= service.total_message_limit: current_app.logger.warning( diff --git a/migrations/versions/0414_change_total_message_limit.py b/migrations/versions/0414_change_total_message_limit.py new file mode 100644 index 000000000..1ee9adb08 --- /dev/null +++ b/migrations/versions/0414_change_total_message_limit.py @@ -0,0 +1,25 @@ +""" + +Revision ID: 0414_change_total_message_limit +Revises: 413_add_message_id +Create Date: 2025-01-23 11:35:22.873930 + +""" + +import sqlalchemy as sa +from alembic import op + +down_revision = "0413_add_message_id" +revision = "0414_change_total_message_limit" + + +def upgrade(): + """ + This limit is only used + """ + op.execute("UPDATE services set total_message_limit=100000") + + + +def downgrade(): + op.execute("UPDATE services set total_message_limit=250000")