Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move sale migration celery task. (#14711)
Piotr Zabieglik5b93c45ac13a
diff --git a/saleor/celeryconf.py b/saleor/celeryconf.pyindex 2bd58a9cd8..796bb47112 100644--- a/saleor/celeryconf.py+++ b/saleor/celeryconf.py@@ -27,11 +27,5 @@ app = Celery("saleor")app.config_from_object("django.conf:settings", namespace="CELERY")app.autodiscover_tasks()-app.autodiscover_tasks(- packages=[- "saleor.discount.migrations.tasks",- ],- related_name="saleor3_17",-)app.autodiscover_tasks(lambda: discover_plugins_modules(settings.PLUGINS)) # type: ignore[misc] # circular import # noqa: E501app.autodiscover_tasks(related_name="search_tasks")diff --git a/saleor/discount/migrations/0047_migrate_sales_to_promotions.py b/saleor/discount/migrations/0047_migrate_sales_to_promotions.pyindex a19791a1ca..e47fa1c52e 100644--- a/saleor/discount/migrations/0047_migrate_sales_to_promotions.py+++ b/saleor/discount/migrations/0047_migrate_sales_to_promotions.py@@ -9,7 +9,7 @@ from django.db import migrations, transactionfrom django.db.models import Exists, OuterReffrom django.db.models.signals import post_migrate-from .tasks.saleor3_17 import update_discounted_prices_task+from ..tasks import update_discounted_prices_task# The batch of size 100 takes ~0.9 second and consumes ~30MB memory at peakBATCH_SIZE = 100diff --git a/saleor/discount/migrations/tasks/saleor3_17.py b/saleor/discount/migrations/tasks/saleor3_17.pydeleted file mode 100644index ab31c37808..0000000000--- a/saleor/discount/migrations/tasks/saleor3_17.py+++ /dev/null@@ -1,40 +0,0 @@-from django.db.models import Exists, F, OuterRef--from ....celeryconf import app-from ....product.models import (- Product,- ProductVariant,- ProductVariantChannelListing,- VariantChannelListingPromotionRule,-)-from ....product.utils.variant_prices import update_discounted_prices_for_promotion--# For 100 rules, with 1000 variants for each rule it takes around 15s-BATCH_SIZE = 100---def update_discounted_prices_task():- variant_listing_qs = (- ProductVariantChannelListing.objects.annotate(- discount=F("price_amount") - F("discounted_price_amount")- )- .filter(discount__gt=0)- .filter(- ~Exists(- VariantChannelListingPromotionRule.objects.filter(- variant_channel_listing_id=OuterRef("id")- )- )- )- )- variant_qs = ProductVariant.objects.filter(- Exists(variant_listing_qs.filter(variant_id=OuterRef("id")))- )- products_ids = Product.objects.filter(- Exists(variant_qs.filter(product_id=OuterRef("id")))- ).values_list("id", flat=True)[:BATCH_SIZE]- if products_ids:- products = Product.objects.filter(id__in=products_ids)- update_discounted_prices_for_promotion(products)- update_discounted_prices_task.delay()diff --git a/saleor/discount/tasks.py b/saleor/discount/tasks.pyindex 352b3d21df..cbc5b3d187 100644--- a/saleor/discount/tasks.py+++ b/saleor/discount/tasks.py@@ -12,8 +12,14 @@ from ..graphql.discount.utils import get_variants_for_predicatefrom ..order import OrderStatusfrom ..order.models import Order, OrderLinefrom ..plugins.manager import get_plugins_manager-from ..product.models import Product, ProductVariant+from ..product.models import (+ Product,+ ProductVariant,+ ProductVariantChannelListing,+ VariantChannelListingPromotionRule,+)from ..product.tasks import update_products_discounted_prices_for_promotion_task+from ..product.utils.variant_prices import update_discounted_prices_for_promotionfrom . import DiscountTypefrom .models import (OrderDiscount,@@ -214,3 +220,40 @@ def disconnect_voucher_codes_from_draft_orders_task(order_ids):).filter(type=DiscountType.VOUCHER).delete()if remaining_ids := list(set(order_ids) - set(ids)):disconnect_voucher_codes_from_draft_orders_task.delay(remaining_ids)+++ name="saleor.discount.migrations.tasks.saleor3_17.update_discounted_prices_task"+)+def update_discounted_prices_task():+ """Recalculate discounted prices during sale to promotion migration."""+ # WARNING: this function is run during `0047_migrate_sales_to_promotions` migration,+ # so please be careful while updating.+ # This task can be deleted after we introduce a different process for calculating+ # discounted prices for promotions.++ # For 100 rules, with 1000 variants for each rule it takes around 15s+ BATCH_SIZE = 100+ variant_listing_qs = (+ ProductVariantChannelListing.objects.annotate(+ discount=F("price_amount") - F("discounted_price_amount")+ )+ .filter(discount__gt=0)+ .filter(+ ~Exists(+ VariantChannelListingPromotionRule.objects.filter(+ variant_channel_listing_id=OuterRef("id")+ )+ )+ )+ )+ variant_qs = ProductVariant.objects.filter(+ Exists(variant_listing_qs.filter(variant_id=OuterRef("id")))+ )+ products_ids = Product.objects.filter(+ Exists(variant_qs.filter(product_id=OuterRef("id")))+ ).values_list("id", flat=True)[:BATCH_SIZE]+ if products_ids:+ products = Product.objects.filter(id__in=products_ids)+ update_discounted_prices_for_promotion(products)+ update_discounted_prices_task.delay()