Skip to content

Commit

Permalink
autorenew_accounts: add possibility to throttle between renewals
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Mar 7, 2025
1 parent 42ff10a commit 1601c5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ django-plans changelog
------------------
* Support for Python 3.9 - 3.13
* Remove support for Django 4.2 - 5.2
* Add ``--throttle`` parameter to the ``autorenew_accounts`` management command

1.2.0
------------------
Expand Down
11 changes: 10 additions & 1 deletion plans/management/commands/autorenew_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ def add_arguments(self, parser):
dest="providers",
help="Renew only accounts with this providers",
)
parser.add_argument(
"--throttle",
type=int,
dest="throttle",
help="Throttle seconds between renewals",
)

def handle(self, *args, **options): # pragma: no cover
providers = options.get("providers")
self.stdout.write("Starting renewal")
renewed_accounts = tasks.autorenew_account(providers)
throttle_seconds = options.get("throttle")
renewed_accounts = tasks.autorenew_account(
providers, throttle_seconds=throttle_seconds
)
if renewed_accounts:
self.stdout.write("Accounts submitted to renewal:")
for a in renewed_accounts:
Expand Down
5 changes: 4 additions & 1 deletion plans/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import time

from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -19,7 +20,7 @@ def get_active_plans():
)


def autorenew_account(providers=None):
def autorenew_account(providers=None, throttle_seconds=0):
logger.info("Started automatic account renewal")
PLANS_AUTORENEW_BEFORE_DAYS = getattr(settings, "PLANS_AUTORENEW_BEFORE_DAYS", 0)
PLANS_AUTORENEW_BEFORE_HOURS = getattr(settings, "PLANS_AUTORENEW_BEFORE_HOURS", 0)
Expand All @@ -41,6 +42,8 @@ def autorenew_account(providers=None):
logger.info(f"{len(accounts_for_renewal)} accounts to be renewed.")

for user in accounts_for_renewal.all():
if throttle_seconds:
time.sleep(throttle_seconds)
account_automatic_renewal.send(sender=None, user=user)
return accounts_for_renewal

Expand Down

0 comments on commit 1601c5c

Please sign in to comment.