From 9c4b274ec22c9addad76f02a54d3c1bde618f834 Mon Sep 17 00:00:00 2001 From: SaintShit Date: Tue, 20 Feb 2024 21:31:50 +0330 Subject: [PATCH] fix: reset_user_data_usage job only for limited and active users --- app/jobs/reset_user_data_usage.py | 17 +++++++++++------ app/jobs/review_users.py | 5 ++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/jobs/reset_user_data_usage.py b/app/jobs/reset_user_data_usage.py index 6eff4d8a..5fb5f352 100644 --- a/app/jobs/reset_user_data_usage.py +++ b/app/jobs/reset_user_data_usage.py @@ -15,12 +15,17 @@ def reset_user_data_usage(): now = datetime.utcnow() with GetDB() as db: - for user in get_users(db, reset_strategy=[ - UserDataLimitResetStrategy.day.value, - UserDataLimitResetStrategy.week.value, - UserDataLimitResetStrategy.month.value, - UserDataLimitResetStrategy.year.value, - ]): + for user in get_users(db, + status=[ + UserStatus.active, + UserStatus.limited + ], + reset_strategy=[ + UserDataLimitResetStrategy.day.value, + UserDataLimitResetStrategy.week.value, + UserDataLimitResetStrategy.month.value, + UserDataLimitResetStrategy.year.value, + ]): last_reset_time = user.last_traffic_reset_time num_days_to_reset = reset_strategy_to_days[user.data_limit_reset_strategy] diff --git a/app/jobs/review_users.py b/app/jobs/review_users.py index 2c460344..501c77f7 100644 --- a/app/jobs/review_users.py +++ b/app/jobs/review_users.py @@ -16,6 +16,7 @@ if TYPE_CHECKING: from app.db.models import User +import time def add_notification_reminders(db: Session, user: "User", now: datetime = datetime.utcnow()) -> None: @@ -36,6 +37,8 @@ def add_notification_reminders(db: Session, user: "User", now: datetime = dateti def review(): + while True: + time.sleep(1) now = datetime.utcnow() now_ts = now.timestamp() with GetDB() as db, GetBG() as bg: @@ -90,4 +93,4 @@ def review(): logger.info(f"User \"{user.username}\" status changed to {status}") -scheduler.add_job(review, 'interval', seconds=5, coalesce=True, max_instances=1) +scheduler.add_job(review, 'interval', seconds=10, coalesce=True, max_instances=1)