From c307cf506916d60c24091f9bd1e1e09fc2ce4670 Mon Sep 17 00:00:00 2001 From: Piotr Zabieglik <55899043+zedzior@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:21:19 +0000 Subject: [PATCH] Add task queue section (#1417) * Add task queue section * Add links * Explain celery beat setup * Apply review changes --- docs/developer/checkout/lifecycle.mdx | 10 ++++ docs/developer/checkout/order-expiration.mdx | 5 ++ docs/developer/community/contributing.mdx | 5 ++ docs/developer/discounts/promotions.mdx | 4 +- docs/developer/running-saleor/task-queue.mdx | 57 ++++++++++++++++++++ sidebars/self-hosting.js | 1 + vercel.json | 4 ++ 7 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 docs/developer/running-saleor/task-queue.mdx diff --git a/docs/developer/checkout/lifecycle.mdx b/docs/developer/checkout/lifecycle.mdx index 867bf499a..589e7829c 100644 --- a/docs/developer/checkout/lifecycle.mdx +++ b/docs/developer/checkout/lifecycle.mdx @@ -41,6 +41,11 @@ To avoid overloading the database, unfinished and unpaid checkouts are automatic - anonymous checkouts (neither user nor email is set) with lines after 30 days, - user checkouts (either user or email is set) with lines after 90 days. +:::note +The checkout deletion task is triggered by [celery beat scheduler](developer/running-saleor/task-queue.mdx#periodic-tasks). +This feature will not work without task queue configuration. +::: + ## Releasing Funds for Abandoned Checkouts Payments for items left in the cart by customers who did not complete the purchase will be refunded to the customer's account. @@ -58,6 +63,11 @@ The release action is triggered only once. If a subscription for a release event To fetch paid checkouts, use the below query: +:::note +The releasing funds for abandoned checkouts task is triggered by [celery beat scheduler](developer/running-saleor/task-queue.mdx#periodic-tasks). +This feature will not work without task queue configuration. +::: + ```graphql {4} { checkouts(first: 10, filter: { authorizeStatus: [PARTIAL, FULL] }) { diff --git a/docs/developer/checkout/order-expiration.mdx b/docs/developer/checkout/order-expiration.mdx index 2895e2163..029099d07 100644 --- a/docs/developer/checkout/order-expiration.mdx +++ b/docs/developer/checkout/order-expiration.mdx @@ -68,6 +68,11 @@ In the above example, orders will expire after 360 minutes (6 hours), and after Once an unpaid order is created (via [`checkoutComplete`](api-reference/checkout/mutations/checkout-complete.mdx) or [`orderCreateFromCheckout`](api-reference/orders/mutations/order-create-from-checkout.mdx)), it remains in `UNCONFIRMED` status. If the customer does not complete the payment within `expireOrdersAfter` minutes after that, the status will change to `EXPIRED` and stock allocation will be released. After `deleteExpiredOrdersAfter` days orders in `EXPIRED` status will be deleted and disappear from the order list. +:::note +The order expiration and order deletion tasks are triggered by [celery beat scheduler](developer/running-saleor/task-queue.mdx#periodic-tasks). +This feature will not work without task queue configuration. +::: + ## How to disable order expiration To disable order expiration, set `expireOrdersAfter` to 0. diff --git a/docs/developer/community/contributing.mdx b/docs/developer/community/contributing.mdx index 186b53970..6388060d6 100644 --- a/docs/developer/community/contributing.mdx +++ b/docs/developer/community/contributing.mdx @@ -301,6 +301,11 @@ Also, do not forget about the docstring, especially in a complicated function. So far, we have mainly used the `GinIndex` and `ilike` operators for searching, but currently, we are testing a new solution with the use of `SearchVector` and `SearchRank`. You can find it in this PR [#9344](https://github.com/saleor/saleor/pull/9344). +:::note +The search vector update task is triggered by [celery beat scheduler](developer/running-saleor/task-queue.mdx#periodic-tasks). +This feature will not work without task queue configuration. +::: + ### API - Use `id` for mutation inputs instead of `model_name_id`. diff --git a/docs/developer/discounts/promotions.mdx b/docs/developer/discounts/promotions.mdx index f12765f5e..faef9264f 100644 --- a/docs/developer/discounts/promotions.mdx +++ b/docs/developer/discounts/promotions.mdx @@ -41,8 +41,8 @@ When multiple promotions apply to one product, only the discount from the promot The discounts from rules within the single promotion are not summed up. :::note -The catalogue discounts from promotions are calculated in the background. So the discounted -price will not be instantly visible after promotion creation. +The catalogue discounts from promotions are calculated in the background, triggered by [celery beat scheduler](developer/running-saleor/task-queue.mdx#periodic-tasks). +So the discounted price will not be instantly visible after promotion creation (default interval is set to 30 seconds). ::: ### Examples of the use cases diff --git a/docs/developer/running-saleor/task-queue.mdx b/docs/developer/running-saleor/task-queue.mdx new file mode 100644 index 000000000..8a3c84ea4 --- /dev/null +++ b/docs/developer/running-saleor/task-queue.mdx @@ -0,0 +1,57 @@ +--- +title: Task Queue +sidebar_label: Task Queue +--- + +Some of the Saleor’s functionality depends on a task queue system to manage asynchronous and periodic tasks. We recommend using Celery with Redis as the message broker. +For more information, please visit [official Celery documentation](https://docs.celeryq.dev/en/stable/index.html). + + +## Periodic tasks + +Celery Beat is a scheduler that triggers background tasks at regular intervals. +Some of the Saleor's features can't be used without proper task scheduler setup. In order to start Celery Beat server: + +1. Specify `CELERY_BROKER_URL` environmental variable which points to task broker (we recommend using Redis as a broker database): + +```shell +set CELERY_BROKER_URL="" +``` +An example of the Redis broker url, running on localhost and port 6379, looks like: `redis://localhost:6379/0`. + +2. Run Celery worker: + +```shell +celery --app saleor.celeryconf:app worker -E --loglevel=info +``` + +3. As a separate process run Celery Beat: + +```shell +celery --app saleor.celeryconf:app beat --scheduler saleor.schedulers.schedulers.DatabaseScheduler +``` + +Below is a list of Saleor features fired up by celery beat with default schedules: + +#### Tasks run in intervals: +- Deactivate preorder for variants; 1 hour +- Delete empty allocations; 1 hour +- Delete expired reservations; 1 day +- Delete outdated event data; 1 day +- [Expire orders](developer/checkout/order-expiration.mdx); 5 minutes +- [Recalculate price for catalog promotions](developer/discounts/promotions.mdx#catalogue-promotions); 30 seconds +- [Release funds for abandoned checkouts](developer/checkout/lifecycle.mdx#releasing-funds-for-abandoned-checkouts); 10 minutes +- Remove apps marked as removed; 3 hours +- [Update search vectors](developer/community/contributing.mdx#searching); 20 seconds + +#### Tasks run at specific time: +- Deactivate expired gift cards; at 0:00 AM +- [Delete expired checkouts](developer/checkout/lifecycle.mdx#checkout-expiration-and-deletion); at 0:00 AM +- [Delete expired orders](developer/checkout/order-expiration.mdx); at 2:00 AM base on +- Delete old exports files; once per day at 1:00 AM +- Update stocks quantity allocated; once per day at 0:00 AM +- [Promotion toggle](developer/discounts/promotions.mdx); based on promotion's start date and end date + +:::important +It is important to understand that without proper task scheduler setup, the actions above will not be triggered. +::: diff --git a/sidebars/self-hosting.js b/sidebars/self-hosting.js index 76ac8b85a..5f065ea21 100644 --- a/sidebars/self-hosting.js +++ b/sidebars/self-hosting.js @@ -13,6 +13,7 @@ export const selfHosting = [ "developer/running-saleor/debugging-emails", "developer/running-saleor/exposing-instance", + "developer/running-saleor/task-queue", title("Deploying"), diff --git a/vercel.json b/vercel.json index 6782419bf..0c2606ede 100644 --- a/vercel.json +++ b/vercel.json @@ -524,6 +524,10 @@ "source": "/docs(/3.x|/next)?/category/development", "destination": "/developer/running-saleor/debugging-emails" }, + { + "source": "/docs(/3.x|/next)?/category/development", + "destination": "/developer/running-saleor/task-queue" + }, { "source": "/docs(/3.x|/next)?/category/saleor-app-store", "destination": "/developer/app-store/overview"