Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Recalculate prices with celery beat - improvements and compatibility between 3.18-3.19-main (#15844)
Maciej Korycinskia6c09d5579d6
diff --git a/saleor/discount/migrations/0072_promotionrule_variants_dirty.py b/saleor/discount/migrations/0072_promotionrule_variants_dirty.pynew file mode 100644index 0000000000..b2b136cac3--- /dev/null+++ b/saleor/discount/migrations/0072_promotionrule_variants_dirty.py@@ -0,0 +1,32 @@+# Generated by Django 3.2.23 on 2024-01-09 08:41++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("discount", "0071_merge_20240307_1156"),+ ]+ operations = [+ migrations.SeparateDatabaseAndState(+ database_operations=[+ migrations.RunSQL(+ """+ ALTER TABLE discount_promotionrule+ ADD COLUMN IF NOT EXISTS variants_dirty boolean+ DEFAULT false;+ """,+ reverse_sql="""+ ALTER TABLE discount_promotionrule DROP COLUMN variants_dirty+ """,+ )+ ],+ state_operations=[+ migrations.AddField(+ model_name="promotionrule",+ name="variants_dirty",+ field=models.BooleanField(default=False),+ )+ ],+ ),+ ]diff --git a/saleor/discount/migrations/0076_promotionrule_variants_dirty.py b/saleor/discount/migrations/0076_promotionrule_variants_dirty.pyindex a0cafc6ad1..6723fe531b 100644--- a/saleor/discount/migrations/0076_promotionrule_variants_dirty.py+++ b/saleor/discount/migrations/0076_promotionrule_variants_dirty.py@@ -1,6 +1,6 @@# Generated by Django 3.2.23 on 2024-01-09 08:41-from django.db import migrations, models+from django.db import migrationsclass Migration(migrations.Migration):@@ -8,18 +8,6 @@ class Migration(migrations.Migration):("discount", "0075_promotionrule_gifts"),]- operations = [- migrations.AddField(- model_name="promotionrule",- name="variants_dirty",- field=models.BooleanField(default=False),- ),- migrations.RunSQL(- """- ALTER TABLE discount_promotionrule- ALTER COLUMN variants_dirty- SET DEFAULT false;- """,- migrations.RunSQL.noop,- ),- ]+ # left to keep a compatible migration. The logic moved to+ # 0072_promotionrule_variants_dirty.py+ operations = []diff --git a/saleor/discount/migrations/0081_merge_20240422_1207.py b/saleor/discount/migrations/0081_merge_20240422_1207.pynew file mode 100644index 0000000000..061bf9337d--- /dev/null+++ b/saleor/discount/migrations/0081_merge_20240422_1207.py@@ -0,0 +1,12 @@+# Generated by Django 3.2.23 on 2024-04-22 12:07++from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ("discount", "0072_promotionrule_variants_dirty"),+ ("discount", "0080_add_unique_type_constraint"),+ ]++ operations = []diff --git a/saleor/graphql/product/mutations/product/product_update.py b/saleor/graphql/product/mutations/product/product_update.pyindex d53134e147..a7a7afdca1 100644--- a/saleor/graphql/product/mutations/product/product_update.py+++ b/saleor/graphql/product/mutations/product/product_update.py@@ -52,8 +52,9 @@ class ProductUpdate(ProductCreate, ModelWithExtRefMutation):product = models.Product.objects.prefetched_for_webhook(single_object=True).get(pk=instance.pk)- listings = product.channel_listings.all()- channel_ids = [listing.channel_id for listing in listings]+ channel_ids = set(+ product.channel_listings.all().values_list("channel_id", flat=True)+ )cls.call_event(mark_active_catalogue_promotion_rules_as_dirty, channel_ids)manager = get_plugin_manager_promise(info.context).get()diff --git a/saleor/product/tasks.py b/saleor/product/tasks.pyindex 5de5ee0a63..7107c74d76 100644--- a/saleor/product/tasks.py+++ b/saleor/product/tasks.py@@ -15,7 +15,6 @@ from ..celeryconf import appfrom ..core.exceptions import PreorderAllocationErrorfrom ..discount import PromotionTypefrom ..discount.models import Promotion, PromotionRule-from ..discount.utils import mark_active_catalogue_promotion_rules_as_dirtyfrom ..plugins.manager import get_plugins_managerfrom ..warehouse.management import deactivate_preorder_for_variantfrom ..webhook.event_types import WebhookEventAsyncType@@ -37,8 +36,8 @@ PRODUCTS_BATCH_SIZE = 300VARIANTS_UPDATE_BATCH = 500# Results in update time ~0.2sDISCOUNTED_PRODUCT_BATCH = 2000-# Results in update time ~1.5s-PROMOTION_RULE_BATCH_SIZE = 250+# Results in update time ~2s when 600 channels exist+PROMOTION_RULE_BATCH_SIZE = 100def _variants_in_batches(variants_qs):@@ -117,15 +116,15 @@ def _get_channel_to_products_map(rule_to_variant_list):[rule_to_variant.promotionrule_id for rule_to_variant in rule_to_variant_list])PromotionChannel = PromotionRule.channels.through- promotion_channel_qs = PromotionChannel.objects.using(- settings.DATABASE_CONNECTION_REPLICA_NAME- ).filter(promotionrule_id__in=rule_ids)- rule_to_channels_map = defaultdict(set)- for promotion_channel in promotion_channel_qs:- rule_to_channels_map[promotion_channel.promotionrule_id].add(- promotion_channel.channel_id- )+ promotion_channel_qs = (+ PromotionChannel.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME)+ .filter(promotionrule_id__in=rule_ids)+ .values_list("promotionrule_id", "channel_id")+ )+ rule_to_channels_map = defaultdict(set)+ for promotionrule_id, channel_id in promotion_channel_qs.iterator():+ rule_to_channels_map[promotionrule_id].add(channel_id)channel_to_products_map = defaultdict(set)for rule_to_variant in rule_to_variant_list:channel_ids = rule_to_channels_map[rule_to_variant.promotionrule_id]@@ -203,11 +202,7 @@ def update_products_discounted_prices_for_promotion_task(# In case of triggered the task by old server worker, mark all active promotions as# dirty. This will make the same re-calculation as the old task.- from ..channel.models import Channel-- mark_active_catalogue_promotion_rules_as_dirty(- Channel.objects.all().values_list("id", flat=True)- )+ PromotionRule.objects.filter(variants_dirty=False).update(variants_dirty=True)@app.task