Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use cutomized celery broker trasport for SQS fair queues (#18375)
Cezary Miącz41d8815706c7
diff --git a/pyproject.toml b/pyproject.tomlindex a690a59474..7cc64e01b1 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -81,6 +81,7 @@ dependencies = ["opentelemetry-semantic-conventions>=0.53b1,<0.54","opentelemetry-distro[otlp]>=0.53b1,<0.54","celery[redis, sqs]>=4.4.5,<6.0.0",+ "kombu[sqs]>=4.6.11,<6.0.0","django-storages[google]~=1.11","python-magic>=0.4.27,<0.5 ; sys_platform != 'win32'","python-magic-bin>=0.4.14,<0.5 ; sys_platform == 'win32'",diff --git a/saleor/core/sqs.py b/saleor/core/sqs.pynew file mode 100644index 0000000000..dd772522ef--- /dev/null+++ b/saleor/core/sqs.py@@ -0,0 +1,57 @@+import uuid++from kombu.asynchronous.aws.sqs.message import AsyncMessage+from kombu.transport.SQS import Channel as SqsChannel+from kombu.transport.SQS import Transport as SqsTransport+from kombu.utils.json import dumps+++class Channel(SqsChannel):+ def _put(self, queue, message, **kwargs):+ """Put message onto queue."""+ q_url = self._new_queue(queue)+ kwargs = {"QueueUrl": q_url}+ if "properties" in message:+ if "message_attributes" in message["properties"]:+ # we don't want to want to have the attribute in the body+ kwargs["MessageAttributes"] = message["properties"].pop(+ "message_attributes"+ )++ # Mitigation of https://github.com/celery/kombu/issues/2400+ # Allows passing MessageGroupId for non-FIFO queues+ if "MessageGroupId" in message["properties"]:+ kwargs["MessageGroupId"] = message["properties"]["MessageGroupId"]++ if queue.endswith(".fifo"):+ if "MessageGroupId" not in kwargs:+ kwargs["MessageGroupId"] = "default"+ if "MessageDeduplicationId" in message["properties"]:+ kwargs["MessageDeduplicationId"] = message["properties"][+ "MessageDeduplicationId"+ ]+ else:+ kwargs["MessageDeduplicationId"] = str(uuid.uuid4())+ else:+ if "DelaySeconds" in message["properties"]:+ kwargs["DelaySeconds"] = message["properties"]["DelaySeconds"]++ if self.sqs_base64_encoding:+ body = AsyncMessage().encode(dumps(message))+ else:+ body = dumps(message)+ kwargs["MessageBody"] = body++ c = self.sqs(queue=self.canonical_queue_name(queue))+ if message.get("redelivered"):+ c.change_message_visibility(+ QueueUrl=q_url,+ ReceiptHandle=message["properties"]["delivery_tag"],+ VisibilityTimeout=self.wait_time_seconds,+ )+ else:+ c.send_message(**kwargs)+++class Transport(SqsTransport):+ Channel = Channeldiff --git a/saleor/settings.py b/saleor/settings.pyindex 6fa52ebd5d..ec944e4e31 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -615,6 +615,12 @@ CELERY_ACCEPT_CONTENT = ["json"]CELERY_BROKER_URL = (os.environ.get("CELERY_BROKER_URL", os.environ.get("CLOUDAMQP_URL")) or "")++# Mitigation of https://github.com/celery/kombu/issues/2400+# Allows passing MessageGroupId for non-FIFO queues+if CELERY_BROKER_URL.startswith("sqs://"):+ CELERY_BROKER_TRANSPORT = "saleor.core.sqs.Transport"+CELERY_RESULT_BACKEND = os.environ.get("CELERY_RESULT_BACKEND", None)CELERY_RESULT_SERIALIZER = "json"CELERY_TASK_ALWAYS_EAGER = not CELERY_BROKER_URLdiff --git a/uv.lock b/uv.lockindex 60dad70331..fc77d2f1bc 100644--- a/uv.lock+++ b/uv.lock@@ -2558,6 +2558,7 @@ dependencies = [{ name = "graphql-core", marker = "platform_python_implementation != 'PyPy'" },{ name = "graphql-relay", marker = "platform_python_implementation != 'PyPy'" },{ name = "html-to-draftjs", marker = "platform_python_implementation != 'PyPy'" },+ { name = "kombu", extra = ["sqs"], marker = "platform_python_implementation != 'PyPy'" },{ name = "lxml", marker = "platform_python_implementation != 'PyPy'" },{ name = "markdown", marker = "platform_python_implementation != 'PyPy'" },{ name = "measurement", marker = "platform_python_implementation != 'PyPy'" },@@ -2671,6 +2672,7 @@ requires-dist = [{ name = "graphql-core", specifier = ">=2.3.2,<3" },{ name = "graphql-relay", specifier = ">=2.0.1,<3" },{ name = "html-to-draftjs", specifier = ">=1.0.1,<2" },+ { name = "kombu", extras = ["sqs"], specifier = ">=4.6.11,<6.0.0" },{ name = "lxml", specifier = ">=4.9.3,<5" },{ name = "markdown", specifier = ">=3.1.1,<4" },{ name = "measurement", specifier = ">=3.2.2,<4" },