From b88a34d3f516e3aa9d61d54f1913b4389a9d73ff Mon Sep 17 00:00:00 2001 From: Muhammad Zubair Date: Tue, 20 Jun 2023 19:49:47 +0500 Subject: [PATCH] feat: update logic and modified code --- .../commands/expire_and_create_entitlements.py | 16 +++++----------- common/djangoapps/entitlements/tasks.py | 9 +++------ 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py b/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py index f65be6ed67..5d6b489e69 100644 --- a/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py +++ b/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py @@ -19,14 +19,8 @@ class Command(BaseCommand): """ Management command for expiring old entitlements and issuing new one against them. - Most entitlements get expired as the user interacts with the platform, - because the LMS checks as it goes. This command is to expire entitlements older than one year and issue new one - against them. But if the learner has not logged in - for a while, we still want to reap these old entitlements. So this command - should be run every now and then (probably daily) to expire old - entitlements. - The command's goal is to pass a narrow subset of entitlements to an + The command's goal is expire a set of entitlements depending on the --count argument passed to an idempotent Celery task for further (parallelized) processing. """ help = dedent(__doc__).strip() @@ -69,9 +63,9 @@ class Command(BaseCommand): ) return - for batch_num in range(int(num_batches)): - start = batch_num * batch_size + 1 # ids are 1-based, so add 1 - end = min(start + batch_size, total + 1) - expire_and_create_entitlements.delay(start, end, logid=str(batch_num)) + while total > 0: + total = total - batch_size + no_of_entitlements = min(total, batch_size) + expire_and_create_entitlements.delay(no_of_entitlements) logger.info('Done. Successfully enqueued %d tasks.', num_batches) diff --git a/common/djangoapps/entitlements/tasks.py b/common/djangoapps/entitlements/tasks.py index ec49390b75..b7bf8c859f 100644 --- a/common/djangoapps/entitlements/tasks.py +++ b/common/djangoapps/entitlements/tasks.py @@ -76,7 +76,7 @@ def expire_old_entitlements(self, start, end, logid='...'): @shared_task(bind=True, ignore_result=True) @set_code_owner_attribute -def expire_and_create_entitlements(self): +def expire_and_create_entitlements(self, no_of_entitlements): """ This task is designed to be called to process and expire bundle of entitlements that are older than one year on in exceptional case 18 months. @@ -101,11 +101,8 @@ def expire_and_create_entitlements(self): try: - for entitlement in entitlements: - # This property request will update the expiration if necessary as - # a side effect. We could manually call update_expired_at(), but - # let's use the same API the rest of the LMS does, to mimic normal - # usage and allow the update call to be an internal detail. + for entitlement in entitlements[:no_of_entitlements]: + entitlement.expire_entitlement() LOGGER.info('Expired entitlement with id %d ', entitlement.id) entitlement.pk = None