Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Process `handle-promotion-toggle` task in batches. (#14982)
Piotr Zabieglik30cb9d338235
diff --git a/saleor/core/schedules.py b/saleor/core/schedules.pyindex dfae17bf80..d1e4083b8f 100644--- a/saleor/core/schedules.py+++ b/saleor/core/schedules.py@@ -38,6 +38,8 @@ class promotion_webhook_schedule(CustomSchedule):app=app,import_path="saleor.core.schedules.promotion_webhook_schedule",)+ # Seconds left to next batch processing+ self.NEXT_BATCH_RUN_TIME = 5def remaining_estimate(self, last_run_at):"""Estimate of next run time.@@ -58,7 +60,11 @@ class promotion_webhook_schedule(CustomSchedule):"""from ..discount.models import Promotion- from ..discount.tasks import get_ending_promotions, get_starting_promotions+ from ..discount.tasks import (+ PROMOTION_TOGGLE_BATCH_SIZE,+ get_ending_promotions,+ get_starting_promotions,+ )now = datetime.now(pytz.UTC)@@ -67,11 +73,17 @@ class promotion_webhook_schedule(CustomSchedule):rem_delta = self.remaining_estimate(last_run_at)remaining = max(rem_delta.total_seconds(), 0)+ staring_promotions = get_starting_promotions()+ ending_promotions = get_ending_promotions()++ # if task needs to be handled in batches, schedule next run with const value+ if len(staring_promotions | ending_promotions) > PROMOTION_TOGGLE_BATCH_SIZE:+ self.next_run = timedelta(seconds=self.NEXT_BATCH_RUN_TIME)+ is_due = remaining == 0+ return schedstate(is_due, self.NEXT_BATCH_RUN_TIME)+# is_due is True when there is at least one sale to notify about# and the remaining time from previous call is 0- staring_promotions = get_starting_promotions().order_by("start_date")- ending_promotions = get_ending_promotions().order_by("end_date")-is_due = remaining == 0 and (staring_promotions.exists() or ending_promotions.exists())diff --git a/saleor/discount/tasks.py b/saleor/discount/tasks.pyindex dd5272bb18..a71c0977a3 100644--- a/saleor/discount/tasks.py+++ b/saleor/discount/tasks.py@@ -33,6 +33,8 @@ if TYPE_CHECKING:from uuid import UUIDtask_logger = get_task_logger(__name__)+# Batch of size 100 takes ~1 sec and consumes ~3mb at peak+PROMOTION_TOGGLE_BATCH_SIZE = 100@app.task@@ -44,19 +46,24 @@ def handle_promotion_toggle():"""manager = get_plugins_manager(allow_replica=False)- staring_promotions = get_starting_promotions()- ending_promotions = get_ending_promotions()-- if not staring_promotions and not ending_promotions:- return-- promotions = staring_promotions | ending_promotions+ starting_promotions = get_starting_promotions(batch=True)+ ending_promotions = get_ending_promotions(batch=True)+ promotion_ids = [+ promotion.id for promotion in starting_promotions | ending_promotions+ ][:PROMOTION_TOGGLE_BATCH_SIZE]+ starting_promotions = [+ promotion for promotion in starting_promotions if promotion.id in promotion_ids+ ]+ ending_promotions = [+ promotion for promotion in ending_promotions if promotion.id in promotion_ids+ ]+ promotions = Promotion.objects.filter(id__in=promotion_ids).all()promotion_id_to_variants, product_ids = fetch_promotion_variants_and_product_ids(promotions)- for staring_promo in staring_promotions:- manager.promotion_started(staring_promo)+ for starting_promo in starting_promotions:+ manager.promotion_started(starting_promo)for ending_promo in ending_promotions:manager.promotion_ended(ending_promo)@@ -81,16 +88,16 @@ def handle_promotion_toggle():update_products_discounted_prices_for_promotion_task.delay(product_ids)starting_promotion_ids = ", ".join(- [str(staring_promo.id) for staring_promo in staring_promotions]+ [str(staring_promo.id) for staring_promo in starting_promotions])ending_promotions_ids = ", ".join([str(ending_promo.id) for ending_promo in ending_promotions])+# DEPRECATED: will be removed in Saleor 4.0.- promotion_ids = ", ".join([str(promo.id) for promo in promotions])+ promotion_ids_str = ", ".join([str(promo.id) for promo in promotions])promotions.update(last_notification_scheduled_at=datetime.now(pytz.UTC))-if starting_promotion_ids:task_logger.info("The promotion_started webhook sent for Promotions with ids: %s",@@ -104,11 +111,11 @@ def handle_promotion_toggle():# DEPRECATED: will be removed in Saleor 4.0.task_logger.info(- "The sale_toggle webhook sent for sales with ids: %s", promotion_ids+ "The sale_toggle webhook sent for sales with ids: %s", promotion_ids_str)-def get_starting_promotions():+def get_starting_promotions(batch=False):"""Return promotions for which the notify about starting should be sent.The notification should be sent for promotions for which the start date has passed@@ -123,10 +130,12 @@ def get_starting_promotions():)& Q(start_date__lte=now))+ if batch:+ return promotions.all()[:PROMOTION_TOGGLE_BATCH_SIZE]return promotions-def get_ending_promotions():+def get_ending_promotions(batch=False):"""Return promotions for which the notify about ending should be sent.The notification should be sent for promotions for which the end date has passed@@ -141,6 +150,8 @@ def get_ending_promotions():)& Q(end_date__lte=now))+ if batch:+ return promotions[:PROMOTION_TOGGLE_BATCH_SIZE]return promotions