Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Execute single batch inside expiring orders task (#12680)

Krzysztof Waliczeke3d01d9c602e
diff --git a/saleor/order/tasks.py b/saleor/order/tasks.py
index 6760fa1e11..d6d345070d 100644
--- a/saleor/order/tasks.py
+++ b/saleor/order/tasks.py
@@ -1,14 +1,11 @@
import logging
-from datetime import timedelta
from typing import List
-from celery.exceptions import SoftTimeLimitExceeded
from django.db.models import Exists, F, Func, OuterRef, Subquery, Value
from django.utils import timezone
from ..celeryconf import app
from ..channel.models import Channel
-from ..core.tasks import celery_task_lock
from ..core.tracing import traced_atomic_transaction
from ..core.utils.events import call_event
from ..discount.models import Voucher, VoucherCustomer
@@ -21,7 +18,7 @@ from .utils import invalidate_order_prices
logger = logging.getLogger(__name__)
-# Batch size of 100 is about ~87MB of memory usage in task
+# Batch size of 100 is about ~1MB of memory usage in task
ORDER_BATCH_SIZE = 100
@@ -42,18 +39,6 @@ def send_order_updated(order_ids):
manager.order_updated(order)
-def _batch_ids(iterable, batch_size=1):
- length = len(iterable)
- for index in range(0, length, batch_size):
- yield iterable[index : min(index + batch_size, length)]
-
-
-def _queryset_in_batches(queryset):
- ids = queryset.values_list("id", flat=True)
- for ids_batch in _batch_ids(ids, ORDER_BATCH_SIZE):
- yield ids_batch
-
-
def _bulk_release_voucher_usage(order_ids):
voucher_orders = Order.objects.filter(
voucher=OuterRef("pk"),
@@ -117,28 +102,18 @@ def _expire_orders(manager, now):
Exists(channels),
status=OrderStatus.UNCONFIRMED,
)
- for ids_batch in _queryset_in_batches(qs):
- with traced_atomic_transaction():
- Order.objects.filter(id__in=ids_batch).update(status=OrderStatus.EXPIRED)
+ ids_batch = list(qs.values_list("pk", flat=True)[:ORDER_BATCH_SIZE])
+ with traced_atomic_transaction():
+ Order.objects.filter(id__in=ids_batch).update(status=OrderStatus.EXPIRED)
- _bulk_release_voucher_usage(ids_batch)
- _order_expired_events(ids_batch)
- deallocate_stock_for_orders(ids_batch, manager)
- _call_expired_order_events(ids_batch, manager)
+ _bulk_release_voucher_usage(ids_batch)
+ _order_expired_events(ids_batch)
+ deallocate_stock_for_orders(ids_batch, manager)
+ _call_expired_order_events(ids_batch, manager)
[email protected](soft_time_limit=60 * 30)
def expire_orders_task():
now = timezone.now()
- task_name = "expire_orders"
manager = get_plugins_manager()
- try:
- with celery_task_lock(task_name) as (lock_obj, acquired):
- if not acquired:
- if lock_obj.created_at < now - timedelta(hours=1):
- logger.error("%s task exceeded 1h working time.", [task_name])
- else:
- _expire_orders(manager, now)
-
- except SoftTimeLimitExceeded as e:
- logger.error(e)
+ _expire_orders(manager, now)
diff --git a/saleor/order/tests/test_tasks.py b/saleor/order/tests/test_tasks.py
index 1403519434..8e0c592ca6 100644
--- a/saleor/order/tests/test_tasks.py
+++ b/saleor/order/tests/test_tasks.py
@@ -1,9 +1,6 @@
-from unittest.mock import patch
-
import pytest
from django.utils import timezone
-from ...core.models import CeleryTask
from ...discount.models import VoucherCustomer
from ...warehouse.models import Allocation
from .. import OrderEvents, OrderStatus
@@ -361,22 +358,3 @@ def test_expire_orders_task_after(order_list, allocations, channel_USD):
assert Allocation.objects.filter(
order_line__order=order_3, quantity_allocated__gt=0
).exists()
-
-
-@patch("logging.Logger.error")
-def test_expire_orders_task_locked_over_hour(logger_mock, order_list, allocations):
- # given
- task_name = "expire_orders"
- error_msg = "%s task exceeded 1h working time."
-
- now = timezone.now()
-
- lock = CeleryTask.objects.create(name=task_name)
- lock.created_at = now - timezone.timedelta(hours=2)
- lock.save()
-
- # when
- expire_orders_task()
-
- # then
- logger_mock.assert_called_once_with(error_msg, [task_name])